BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

fix fruit menu
timfa Tim · pushed by timfa · 4 years ago
65325845 History
frogScene.js 8.3 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 FruitInventory.Instance.addToInventory(fruit.renderer.subImage);
167 }
168
169 getNextExternalFruitPosition ()
170 {
171 if (this.eatenFruit.length == 0)
172 {
173 return this.externalFruitCurrentPos;
174 }
175
176 if (this.eatenFruit.length < this.maxPerXAxis)
177 {
178 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(this.game.images.fruit.width, 0));
179 }
180 else if (this.eatenFruit.length - this.maxPerXAxis < this.maxPerYAxis)
181 {
182 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(0, this.game.images.fruit.height));
183 }
184 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerXAxis * 2)
185 {
186 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(this.game.images.fruit.width, 0));
187 }
188 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerYAxis * 2)
189 {
190 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(0, this.game.images.fruit.height));
191 }
192 }
193
194 destroyFruit (fruit)
195 {
196 if (this.fruits.contains(fruit))
197 {
198 this.fruits.remove(fruit);
199 this.fruitPool.push(fruit);
200 fruit.transform.position.x = 50000;
201 }
202 }
203
204 getFromFruitPoolOrCreate ()
205 {
206 return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new FrogFruit(0, 0, this.game.images.fruit, 0));
207 }
208
209 setRandomSubImage (fruit)
210 {
211 fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages));
212 }
213
214 draw ()
215 {
216 super.draw();
217
218 this.drawExternalFruit();
219 this.drawMenu();
220 }
221
222 getRandomVectorPosition ()
223 {
224 return new vector
225 (
226 Math.floor(Math.randomRange(0, this.real_size.x)),
227 0
228 );
229 }
230
231 shouldSpawn ()
232 {
233 return Math.random() < this.fruitSpawnChance;
234 }
235
236 checkSpacebar ()
237 {
238 if (GameInput.isHeld(GameInput.keys[" "]))
239 {
240 this.startGame();
241 }
242 }
243
244 drawExternalFruit ()
245 {
246 if (this.eatenFruit.length > 0)
247 {
248 for (let i = 0; i < this.eatenFruit.length; i++)
249 {
250 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
251
252 this.game.images.fruit.draw(this.parentScene,
253 this.eatenFruit[i].position.x, this.eatenFruit[i].position.y,
254 this.game.images.fruit.width * this.eatenFruit[i].scaleX, this.game.images.fruit.height * this.eatenFruit[i].scaleY,
255 0, 0, 0, 1, this.eatenFruit[i].image);
256
257
258 }
259 }
260 }
261
262 drawMenu ()
263 {
264 if (this.posScaleY < 2)
265 {
266 this.context.font = "20px Monospace";
267 this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
268
269
270 let textData = this.context.measureText("Press");
271 this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);
272
273 textData = this.context.measureText("To Start");
274 this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);
275
276 let space = this.game.images.spacebar;
277 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);
278 }
279 }
280
281 doMenuScreenTween ()
282 {
283 this.screenTween = new TWEEN.Tween(this)
284 .to({ posScaleY: 1 }, 800)
285 .easing(TWEEN.Easing.Elastic.Out)
286 .start();
287 }
288
289 doSpaceBarTweenIn ()
290 {
291 this.spaceBarTween = new TWEEN.Tween(this)
292 .to({ image_1_rotation: 1 }, 500)
293 .easing(TWEEN.Easing.Sinusoidal.InOut)
294 .yoyo(true)
295 .repeat(Infinity)
296 .start();
297 }
298
299 doAwayMenuScreenTween ()
300 {
301 this.screenTween
302 .to({ posScaleY: 2.5 }, 800)
303 .easing(TWEEN.Easing.Elastic.InOut)
304 .start();
305 }
306}