BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

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