BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

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