BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

frogScene.js 10.7 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 frogButton = MainMenu.Instance.getChildButton("btnGames").getChildButton("btnFrog");
106 frogButton.moveToActiveState(800, () => frogButton.moveToInactiveState(800));
107
108 this.windowDestroyTweens.forEach(t => t.start());
109 this.windowDestroyTweens[0].onComplete(() => this.destroy());
110
111 this.windowDestroyTweens = [];
112 }
113 }
114 }
115
116 gameOver ()
117 {
118 if (this.spaceBarTween)
119 TWEEN.remove(this.spaceBarTween);
120 if (this.screenTween)
121 TWEEN.remove(this.screenTween);
122 this.activeObjects.forEach(ao => ao.destroy());
123
124 // pass over fruits array to parent scene
125
126 this.destructing = true; // finish animations first
127 }
128
129 increaseDifficulty ()
130 {
131 this.gameSpeed += .1;
132
133 if (this.fruitSpawnRateMax > .1)
134 {
135 this.fruitSpawnRateMax -= .2;
136 }
137 else if (this.fruitSpawnRateMax == .2)
138 {
139 this.fruitSpawnRateMax = .1;
140 }
141 if (this.fruitSpawnRateMin > 0)
142 {
143 this.fruitSpawnRateMin -= .2;
144 }
145 }
146
147 startGame ()
148 {
149 if (this.spaceBarTween)
150 {
151 this.spaceBarTween.stop();
152 }
153
154 this.doAwayMenuScreenTween();
155
156 let frog = new PlayerFrog(60, this.real_size.y - 50, this.game.images.frog_basic, 0);
157
158 let tileWidth = this.game.images.tile_floor_brown.width - 2;
159 let tileWidthHalf = tileWidth / 2;
160
161 for (let tx = tileWidthHalf - 4; tx < this.real_size.x; tx += tileWidth)
162 {
163 this.addObject(new FrogFloorTile(tx, this.real_size.y - 30, this.game.images.tile_floor_brown, 0));
164 }
165 this.addObject(frog);
166
167 this.start = true;
168 }
169
170 pauseGame ()
171 {
172 this.paused = true;
173
174 this.doMenuScreenTween();
175 }
176
177 unPauseGame ()
178 {
179 this.doAwayMenuScreenTween();
180 }
181
182 spawnFruits (deltaTime)
183 {
184 this.fruitSpawnTimer -= deltaTime;
185 if (this.fruitSpawnTimer < this.fruitSpawnRateMin)
186 {
187 if (this.shouldSpawn() || this.fruitSpawnTimer < 0)
188 {
189 let fruit = this.getFromFruitPoolOrCreate();
190 fruit.collider.enabled = true;
191 fruit.speed = Math.randomRange(10, 20);
192 fruit.transform.position = this.getRandomVectorPosition();
193 this.setRandomSubImage(fruit);
194 this.fruits.push(fruit);
195
196 this.fruitSpawnTimer = this.fruitSpawnRateMax;
197 }
198 }
199 }
200
201 consumeFruit (fruit)
202 {
203 let currFruit = { position: this.getNextExternalFruitPosition(), scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
204 this.eatenFruit.push(currFruit);
205
206 new TWEEN.Tween(currFruit)
207 .to({ scaleX: 1, scaleY: 1 }, 400)
208 .easing(TWEEN.Easing.Elastic.Out)
209 .start();
210
211 this.destroyFruit(fruit);
212
213 this.increaseDifficulty();
214
215 FruitInventory.Instance.addToInventory(fruit.renderer.subImage);
216 }
217
218 getNextExternalFruitPosition ()
219 {
220 if (this.eatenFruit.length == 0)
221 {
222 return this.externalFruitCurrentPos;
223 }
224
225 if (this.eatenFruit.length < this.maxPerXAxis)
226 {
227 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(this.game.images.fruit.width, 0));
228 }
229 else if (this.eatenFruit.length - this.maxPerXAxis < this.maxPerYAxis)
230 {
231 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(0, this.game.images.fruit.height));
232 }
233 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerXAxis * 2)
234 {
235 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(this.game.images.fruit.width, 0));
236 }
237 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerYAxis * 2)
238 {
239 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(0, this.game.images.fruit.height));
240 }
241 }
242
243 destroyFruit (fruit)
244 {
245 if (this.fruits.contains(fruit))
246 {
247 this.fruits.remove(fruit);
248 this.fruitPool.push(fruit);
249 fruit.transform.position.x = 50000;
250 }
251 }
252
253 getFromFruitPoolOrCreate ()
254 {
255 return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new FrogFruit(0, 0, this.game.images.fruit, 0));
256 }
257
258 setRandomSubImage (fruit)
259 {
260 fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages));
261 }
262
263 draw ()
264 {
265 super.draw();
266
267 this.drawExternalFruit();
268 this.drawMenu();
269 }
270
271 getRandomVectorPosition ()
272 {
273 return new vector
274 (
275 Math.floor(Math.randomRange(0, this.real_size.x)),
276 0
277 );
278 }
279
280 shouldSpawn ()
281 {
282 return Math.random() < this.fruitSpawnChance;
283 }
284
285 checkSpacebar ()
286 {
287 if (GameInput.isHeld(GameInput.keys[" "]))
288 {
289 this.startGame();
290 }
291 }
292
293 checkEscape ()
294 {
295 if (GameInput.isPressed(GameInput.keys.Escape))
296 {
297
298 this.paused ? this.unPauseGame() : this.pauseGame();
299 }
300 }
301
302 drawExternalFruit ()
303 {
304 if (this.eatenFruit.length > 0)
305 {
306 for (let i = 0; i < this.eatenFruit.length; i++)
307 {
308 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
309
310 this.game.images.fruit.draw(this.parentScene,
311 this.eatenFruit[i].position.x, this.eatenFruit[i].position.y,
312 this.game.images.fruit.width * this.eatenFruit[i].scaleX, this.game.images.fruit.height * this.eatenFruit[i].scaleY,
313 0, 0, 0, 1, this.eatenFruit[i].image);
314
315
316 }
317 }
318 }
319
320 drawMenu ()
321 {
322 if (!this.paused)
323 {
324 if (this.posScaleY < 2)
325 {
326 this.context.font = "20px Monospace";
327 this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
328
329 let textData = this.context.measureText("Press");
330 this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);
331
332 textData = this.context.measureText("To Start");
333 this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);
334
335 let space = this.game.images.spacebar;
336 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);
337 }
338 }
339 else
340 {
341 this.context.font = "20px Monospace";
342 this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
343
344 let textData = this.context.measureText("Paused");
345 this.context.fillText("Paused", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY * 2, 72);
346 }
347 }
348
349 doMenuScreenTween ()
350 {
351 this.screenTween = new TWEEN.Tween(this)
352 .to({ posScaleY: 1 }, 800)
353 .easing(TWEEN.Easing.Elastic.Out)
354 .start();
355 this.doCloseButtonTweenIn();
356 }
357
358 doCloseButtonTweenIn ()
359 {
360 new TWEEN.Tween(this.closeButton.transform.position)
361 .to({ y: 40 }, 800)
362 .easing(TWEEN.Easing.Elastic.Out)
363 .start();
364 }
365
366 doCloseButtonTweenOut ()
367 {
368 new TWEEN.Tween(this.closeButton.transform.position)
369 .to({ y: -40 }, 800)
370 .easing(TWEEN.Easing.Elastic.Out)
371 .start();
372 }
373
374 doSpaceBarTweenIn ()
375 {
376 this.spaceBarTween = new TWEEN.Tween(this)
377 .to({ image_1_rotation: 1 }, 500)
378 .easing(TWEEN.Easing.Sinusoidal.InOut)
379 .yoyo(true)
380 .repeat(Infinity)
381 .start();
382 }
383
384 doAwayMenuScreenTween ()
385 {
386 this.doCloseButtonTweenOut();
387
388 this.screenTween = new TWEEN.Tween(this)
389 .to({ posScaleY: 2.5 }, 800)
390 .easing(TWEEN.Easing.Elastic.InOut)
391 .start().onComplete(() => this.paused = false);
392
393 }
394}