BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

remove logging
timfa wsadhelios · pushed by timfa · 4 years ago
88af4bbe History
frogScene.js 10.8 KiB Download Raw
1class FrogScene extends Scene
2{
3
4 fruitSpawnRateMin = 1;
5 fruitSpawnRateMax = 2;
6 fruitSpawnTimer = 0;
7
8 fruitSpawnChance = .2;
9
10 fruits = [];
11 fruitPool = [];
12 eatenFruit = [];
13
14 gameSpeed = 1;
15
16 start = false;
17 paused = false;
18
19 closeButton = null;
20
21 closeBtnX = 0;
22 closeBtnY = -140;
23
24 posScaleY = 0;
25 text_1_target_y;
26 text_2_target_y;
27
28 image_1_target_y;
29 image_1_rotation = -1;
30
31 spaceBarTween = null;
32 screenTween = null;
33
34 destructing = false;
35
36 externalFruitCurrentPos = null;
37
38 windowDestroyTweens = [];
39
40 constructor (x, y, width, height, displayMode = 0)
41 {
42 super(x, y, width, height, displayMode);
43
44 this.fruitSpawnTimer = this.fruitSpawnRateMax;
45
46 this.text_1_target_y = this.real_size.y * .3;
47 this.text_2_target_y = this.real_size.y * .62;
48 this.image_1_target_y = this.real_size.y * .45;
49 this.closeBtnX = this.real_size.x - 50;
50 }
51
52 onLoad ()
53 {
54 let pos = new vector(this.closeBtnX, this.closeBtnY);
55 this.closeButton = this.addObject(new SubBtn(pos.x, pos.y, this.game.images.closeButton, 1));
56
57 this.closeButton.isClicked.add(function ()
58 {
59 this.paused = false;
60 this.posScaleY = 2.5;
61 this.gameOver();
62 }, this);
63
64 this.externalFruitCurrentPos = new vector(this.real_position.x - this.game.images.fruit.width / 2, this.real_position.y - this.game.images.fruit.height / 2);
65
66 this.maxPerXAxis = Math.ceil(this.real_size.x / this.game.images.fruit.width) + 2;
67 this.maxPerYAxis = Math.ceil(this.real_size.y / this.game.images.fruit.height) + 2;
68
69 this.doMenuScreenTween();
70
71 this.doSpaceBarTweenIn();
72 }
73
74 update (deltaTime)
75 {
76 if (!this.paused)
77 {
78 super.update(deltaTime);
79 }
80 else
81 {
82 this.closeButton.update(deltaTime);
83 }
84
85 if (this.start && !this.destructing)
86 {
87 this.checkEscape();
88 if (!this.paused)
89 this.spawnFruits(deltaTime);
90
91 }
92 else if (!this.start && !this.destructing)
93 {
94 this.checkSpacebar();
95 }
96
97 if (this.destructing)
98 {
99 if (this.activeObjects.length > 0)
100 {
101 return;
102 }
103 if (this.windowDestroyTweens.length > 0)
104 {
105 let gameButton = MainMenu.Instance.getChildButton("btnGames").getChildButton("btnFrog");
106 let tween = gameButton.moveToActiveState(800, () => gameButton.moveToInactiveState(800));
107
108 tween.easing(TWEEN.Easing.Back.Out);
109
110 this.windowDestroyTweens.forEach(t => t.start());
111 this.windowDestroyTweens[0].onComplete(() => this.destroy());
112
113 this.windowDestroyTweens = [];
114 }
115 }
116 }
117
118 gameOver ()
119 {
120 if (this.spaceBarTween)
121 TWEEN.remove(this.spaceBarTween);
122 if (this.screenTween)
123 TWEEN.remove(this.screenTween);
124 this.activeObjects.forEach(ao => ao.destroy());
125
126 // pass over fruits array to parent scene
127
128 this.destructing = true; // finish animations first
129 }
130
131 increaseDifficulty ()
132 {
133 this.gameSpeed += .1;
134
135 if (this.fruitSpawnRateMax > .1)
136 {
137 this.fruitSpawnRateMax -= .2;
138 }
139 else if (this.fruitSpawnRateMax == .2)
140 {
141 this.fruitSpawnRateMax = .1;
142 }
143 if (this.fruitSpawnRateMin > 0)
144 {
145 this.fruitSpawnRateMin -= .2;
146 }
147 }
148
149 startGame ()
150 {
151 if (this.spaceBarTween)
152 {
153 this.spaceBarTween.stop();
154 }
155
156 this.doAwayMenuScreenTween();
157
158 let frog = new PlayerFrog(60, this.real_size.y - 50, this.game.images.frog_basic, 0);
159
160 let tileWidth = this.game.images.tile_floor_brown.width - 2;
161 let tileWidthHalf = tileWidth / 2;
162
163 for (let tx = tileWidthHalf - 4; tx < this.real_size.x; tx += tileWidth)
164 {
165 this.addObject(new FrogFloorTile(tx, this.real_size.y - 30, this.game.images.tile_floor_brown, 0));
166 }
167 this.addObject(frog);
168
169 this.start = true;
170 }
171
172 pauseGame ()
173 {
174 this.paused = true;
175
176 this.doMenuScreenTween();
177 }
178
179 unPauseGame ()
180 {
181 this.doAwayMenuScreenTween();
182 }
183
184 spawnFruits (deltaTime)
185 {
186 this.fruitSpawnTimer -= deltaTime;
187 if (this.fruitSpawnTimer < this.fruitSpawnRateMin)
188 {
189 if (this.shouldSpawn() || this.fruitSpawnTimer < 0)
190 {
191 let fruit = this.getFromFruitPoolOrCreate();
192 fruit.collider.enabled = true;
193 fruit.speed = Math.randomRange(10, 20);
194 fruit.transform.position = this.getRandomVectorPosition();
195 this.setRandomSubImage(fruit);
196 this.fruits.push(fruit);
197
198 this.fruitSpawnTimer = this.fruitSpawnRateMax;
199 }
200 }
201 }
202
203 consumeFruit (fruit)
204 {
205 let currFruit = { position: this.getNextExternalFruitPosition(), scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
206 this.eatenFruit.push(currFruit);
207
208 new TWEEN.Tween(currFruit)
209 .to({ scaleX: 1, scaleY: 1 }, 400)
210 .easing(TWEEN.Easing.Elastic.Out)
211 .start();
212
213 this.destroyFruit(fruit);
214
215 this.increaseDifficulty();
216
217 FruitInventory.Instance.addToInventory(fruit.renderer.subImage);
218 }
219
220 getNextExternalFruitPosition ()
221 {
222 if (this.eatenFruit.length == 0)
223 {
224 return this.externalFruitCurrentPos;
225 }
226
227 if (this.eatenFruit.length < this.maxPerXAxis)
228 {
229 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(this.game.images.fruit.width, 0));
230 }
231 else if (this.eatenFruit.length - this.maxPerXAxis < this.maxPerYAxis)
232 {
233 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(0, this.game.images.fruit.height));
234 }
235 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerXAxis * 2)
236 {
237 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(this.game.images.fruit.width, 0));
238 }
239 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerYAxis * 2)
240 {
241 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(0, this.game.images.fruit.height));
242 }
243 }
244
245 destroyFruit (fruit)
246 {
247 if (this.fruits.contains(fruit))
248 {
249 this.fruits.remove(fruit);
250 this.fruitPool.push(fruit);
251 fruit.transform.position.x = 50000;
252 }
253 }
254
255 getFromFruitPoolOrCreate ()
256 {
257 return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new FrogFruit(0, 0, this.game.images.fruit, 0));
258 }
259
260 setRandomSubImage (fruit)
261 {
262 fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages));
263 }
264
265 draw ()
266 {
267 super.draw();
268
269 this.drawExternalFruit();
270 this.drawMenu();
271 }
272
273 getRandomVectorPosition ()
274 {
275 return new vector
276 (
277 Math.floor(Math.randomRange(0, this.real_size.x)),
278 0
279 );
280 }
281
282 shouldSpawn ()
283 {
284 return Math.random() < this.fruitSpawnChance;
285 }
286
287 checkSpacebar ()
288 {
289 if (GameInput.isHeld(GameInput.keys[" "]))
290 {
291 this.startGame();
292 }
293 }
294
295 checkEscape ()
296 {
297 if (GameInput.isPressed(GameInput.keys.Escape))
298 {
299
300 this.paused ? this.unPauseGame() : this.pauseGame();
301 }
302 }
303
304 drawExternalFruit ()
305 {
306 if (this.eatenFruit.length > 0)
307 {
308 for (let i = 0; i < this.eatenFruit.length; i++)
309 {
310 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
311
312 this.game.images.fruit.draw(this.parentScene,
313 this.eatenFruit[i].position.x, this.eatenFruit[i].position.y,
314 this.game.images.fruit.width * this.eatenFruit[i].scaleX, this.game.images.fruit.height * this.eatenFruit[i].scaleY,
315 0, 0, 0, 1, this.eatenFruit[i].image);
316
317
318 }
319 }
320 }
321
322 drawMenu ()
323 {
324 if (!this.paused)
325 {
326 if (this.posScaleY < 2)
327 {
328 this.context.font = "20px Monospace";
329 this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
330
331 let textData = this.context.measureText("Press");
332 this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);
333
334 textData = this.context.measureText("To Start");
335 this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);
336
337 let space = this.game.images.spacebar;
338 space.draw(this, this.real_size.x * .5, this.image_1_target_y * this.posScaleY, space.width, space.height, this.image_1_rotation * 3, 0, 0, 1, 0);
339 }
340 }
341 else
342 {
343 this.context.font = "20px Monospace";
344 this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
345
346 let textData = this.context.measureText("Paused");
347 this.context.fillText("Paused", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY * 2, 72);
348 }
349 }
350
351 doMenuScreenTween ()
352 {
353 this.screenTween = new TWEEN.Tween(this)
354 .to({ posScaleY: 1 }, 800)
355 .easing(TWEEN.Easing.Elastic.Out)
356 .start();
357 this.doCloseButtonTweenIn();
358 }
359
360 doCloseButtonTweenIn ()
361 {
362 new TWEEN.Tween(this.closeButton.transform.position)
363 .to({ y: 40 }, 800)
364 .easing(TWEEN.Easing.Elastic.Out)
365 .start();
366 }
367
368 doCloseButtonTweenOut ()
369 {
370 new TWEEN.Tween(this.closeButton.transform.position)
371 .to({ y: -40 }, 800)
372 .easing(TWEEN.Easing.Elastic.Out)
373 .start();
374 }
375
376 doSpaceBarTweenIn ()
377 {
378 this.spaceBarTween = new TWEEN.Tween(this)
379 .to({ image_1_rotation: 1 }, 500)
380 .easing(TWEEN.Easing.Sinusoidal.InOut)
381 .yoyo(true)
382 .repeat(Infinity)
383 .start();
384 }
385
386 doAwayMenuScreenTween ()
387 {
388 this.doCloseButtonTweenOut();
389
390 this.screenTween = new TWEEN.Tween(this)
391 .to({ posScaleY: 2.5 }, 800)
392 .easing(TWEEN.Easing.Elastic.InOut)
393 .start().onComplete(() => this.paused = false);
394
395 }
396}