BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

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