BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

a743c0b9 History
frogScene.js 8.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
18 posScaleY = 0;
19 text_1_target_y;
20 text_2_target_y;
21
22 image_1_target_y;
23 image_1_rotation = -1;
24
25 spaceBarTween = null;
26 screenTween = null;
27
28 destructing = false;
29
30 externalFruitCurrentPos = null;
31
32 windowDestroyTweens = [];
33
34 constructor (x, y, width, height, displayMode = 0)
35 {
36 super(x, y, width, height, displayMode);
37
38 this.fruitSpawnTimer = this.fruitSpawnRateMax;
39
40 this.text_1_target_y = this.real_size.y * .3;
41 this.text_2_target_y = this.real_size.y * .62;
42 this.image_1_target_y = this.real_size.y * .45;
43
44 this.doMenuScreenTween();
45
46 this.doSpaceBarTweenIn();
47 }
48
49 onLoad ()
50 {
51 this.externalFruitCurrentPos = new vector(this.real_position.x - this.game.images.fruit.width / 2, this.real_position.y - this.game.images.fruit.height / 2);
52
53 this.maxPerXAxis = Math.ceil(this.real_size.x / this.game.images.fruit.width) + 2;
54 this.maxPerYAxis = Math.ceil(this.real_size.y / this.game.images.fruit.height) + 2;
55 }
56
57 update (deltaTime)
58 {
59 super.update(deltaTime);
60
61 if (this.start && !this.destructing)
62 {
63 this.spawnFruits(deltaTime);
64 }
65 else if (!this.start && !this.destructing)
66 {
67 this.checkSpacebar();
68 }
69
70 if (this.destructing)
71 {
72 if (this.activeObjects.length > 0)
73 {
74 return;
75 }
76 if (this.windowDestroyTweens.length > 0)
77 {
78 let gameButton = MenuManager.Instance.getSubButton("btnGames");
79
80 gameButton.moveToOpen(() => gameButton.moveToClose());
81
82 this.windowDestroyTweens.forEach(t => t.start());
83 this.windowDestroyTweens[0].onComplete(() => this.destroy());
84
85 this.windowDestroyTweens = [];
86 }
87 }
88 }
89
90 gameOver ()
91 {
92 if (this.spaceBarTween)
93 TWEEN.remove(this.spaceBarTween);
94 if (this.screenTween)
95 TWEEN.remove(this.screenTween);
96 this.start = false;
97 this.activeObjects.forEach(ao => ao.destroy());
98
99 // pass over fruits array to parent scene
100
101 this.destructing = true; // finish animations first
102 }
103
104 increaseDifficulty ()
105 {
106 this.gameSpeed += .1;
107
108 if (this.fruitSpawnRateMax > .1)
109 {
110 this.fruitSpawnRateMax -= .2;
111 }
112 else if (this.fruitSpawnRateMax == .2)
113 {
114 this.fruitSpawnRateMax = .1;
115 }
116 if (this.fruitSpawnRateMin > 0)
117 {
118 this.fruitSpawnRateMin -= .2;
119 }
120 }
121
122 startGame ()
123 {
124 if (this.spaceBarTween)
125 {
126 this.spaceBarTween.stop();
127 }
128
129 this.doAwayMenuScreenTween();
130
131 let frog = new PlayerFrog(60, this.real_size.y - 50, this.game.images.frog_basic, 0);
132
133 let tileWidth = this.game.images.tile_floor_brown.width - 2;
134 let tileWidthHalf = tileWidth / 2;
135
136 for (let tx = tileWidthHalf - 4; tx < this.real_size.x; tx += tileWidth)
137 {
138 this.addObject(new FrogFloorTile(tx, this.real_size.y - 30, this.game.images.tile_floor_brown, 0));
139 }
140 this.addObject(frog);
141
142 this.start = true;
143 }
144
145 spawnFruits (deltaTime)
146 {
147 this.fruitSpawnTimer -= deltaTime;
148 if (this.fruitSpawnTimer < this.fruitSpawnRateMin)
149 {
150 if (this.shouldSpawn() || this.fruitSpawnTimer < 0)
151 {
152 let fruit = this.getFromFruitPoolOrCreate();
153 fruit.collider.enabled = true;
154 fruit.speed = Math.randomRange(10, 20);
155 fruit.transform.position = this.getRandomVectorPosition();
156 this.setRandomSubImage(fruit);
157 this.fruits.push(fruit);
158
159 this.fruitSpawnTimer = this.fruitSpawnRateMax;
160 }
161 }
162 }
163
164 consumeFruit (fruit)
165 {
166 let currFruit = { position: this.getNextExternalFruitPosition(), scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
167 this.eatenFruit.push(currFruit);
168
169 new TWEEN.Tween(currFruit)
170 .to({ scaleX: 1, scaleY: 1 }, 400)
171 .easing(TWEEN.Easing.Elastic.Out)
172 .start();
173
174 this.destroyFruit(fruit);
175
176 this.increaseDifficulty();
177
178 FruitInventory.Instance.addToInventory(fruit.renderer.subImage);
179 }
180
181 getNextExternalFruitPosition ()
182 {
183 if (this.eatenFruit.length == 0)
184 {
185 return this.externalFruitCurrentPos;
186 }
187
188 if (this.eatenFruit.length < this.maxPerXAxis)
189 {
190 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(this.game.images.fruit.width, 0));
191 }
192 else if (this.eatenFruit.length - this.maxPerXAxis < this.maxPerYAxis)
193 {
194 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(0, this.game.images.fruit.height));
195 }
196 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerXAxis * 2)
197 {
198 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(this.game.images.fruit.width, 0));
199 }
200 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerYAxis * 2)
201 {
202 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(0, this.game.images.fruit.height));
203 }
204 }
205
206 destroyFruit (fruit)
207 {
208 if (this.fruits.contains(fruit))
209 {
210 this.fruits.remove(fruit);
211 this.fruitPool.push(fruit);
212 fruit.transform.position.x = 50000;
213 }
214 }
215
216 getFromFruitPoolOrCreate ()
217 {
218 return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new FrogFruit(0, 0, this.game.images.fruit, 0));
219 }
220
221 setRandomSubImage (fruit)
222 {
223 fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages));
224 }
225
226 draw ()
227 {
228 super.draw();
229
230 this.drawExternalFruit();
231 this.drawMenu();
232 }
233
234 getRandomVectorPosition ()
235 {
236 return new vector
237 (
238 Math.floor(Math.randomRange(0, this.real_size.x)),
239 0
240 );
241 }
242
243 shouldSpawn ()
244 {
245 return Math.random() < this.fruitSpawnChance;
246 }
247
248 checkSpacebar ()
249 {
250 if (GameInput.isHeld(GameInput.keys[" "]))
251 {
252 this.startGame();
253 }
254 }
255
256 drawExternalFruit ()
257 {
258 if (this.eatenFruit.length > 0)
259 {
260 for (let i = 0; i < this.eatenFruit.length; i++)
261 {
262 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
263
264 this.game.images.fruit.draw(this.parentScene,
265 this.eatenFruit[i].position.x, this.eatenFruit[i].position.y,
266 this.game.images.fruit.width * this.eatenFruit[i].scaleX, this.game.images.fruit.height * this.eatenFruit[i].scaleY,
267 0, 0, 0, 1, this.eatenFruit[i].image);
268
269
270 }
271 }
272 }
273
274 drawMenu ()
275 {
276 if (this.posScaleY < 2)
277 {
278 this.context.font = "20px Monospace";
279 this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
280
281
282 let textData = this.context.measureText("Press");
283 this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);
284
285 textData = this.context.measureText("To Start");
286 this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);
287
288 let space = this.game.images.spacebar;
289 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);
290 }
291 }
292
293 doMenuScreenTween ()
294 {
295 this.screenTween = new TWEEN.Tween(this)
296 .to({ posScaleY: 1 }, 800)
297 .easing(TWEEN.Easing.Elastic.Out)
298 .start();
299 }
300
301 doSpaceBarTweenIn ()
302 {
303 this.spaceBarTween = new TWEEN.Tween(this)
304 .to({ image_1_rotation: 1 }, 500)
305 .easing(TWEEN.Easing.Sinusoidal.InOut)
306 .yoyo(true)
307 .repeat(Infinity)
308 .start();
309 }
310
311 doAwayMenuScreenTween ()
312 {
313 this.screenTween
314 .to({ posScaleY: 2.5 }, 800)
315 .easing(TWEEN.Easing.Elastic.InOut)
316 .start();
317 }
318}