BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

frogScene.js 7.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
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 TWEEN.removeAll();
102 }
103
104 this.doAwayMenuScreenTween();
105
106 let frog = new Frog(60, this.real_size.y - 50, this.game.images.frog_basic, 0);
107
108 let tileWidth = this.game.images.tile_floor_brown.width - 2;
109 let tileWidthHalf = tileWidth / 2;
110
111 for (let tx = tileWidthHalf - 4; tx < this.real_size.x; tx += tileWidth)
112 {
113 this.addObject(new FloorTile(tx, this.real_size.y - 30, this.game.images.tile_floor_brown, 0));
114 }
115 this.addObject(frog);
116
117 this.start = true;
118 }
119
120 spawnFruits (deltaTime)
121 {
122 this.fruitSpawnTimer -= deltaTime;
123 if (this.fruitSpawnTimer < this.fruitSpawnRateMin)
124 {
125 if (this.shouldSpawn() || this.fruitSpawnTimer < 0)
126 {
127 let fruit = this.getFromFruitPoolOrCreate();
128 fruit.collider.enabled = true;
129 fruit.speed = Math.randomRange(10, 20);
130 fruit.transform.position = this.getRandomVectorPosition();
131 this.setRandomSubImage(fruit);
132 this.fruits.push(fruit);
133
134 this.fruitSpawnTimer = this.fruitSpawnRateMax;
135 }
136 }
137 }
138
139 consumeFruit (fruit)
140 {
141 let currFruit = { scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
142 this.eatenFruit.push(currFruit);
143
144 new TWEEN.Tween(currFruit)
145 .to({ scaleX: 1, scaleY: 1 }, 400)
146 .easing(TWEEN.Easing.Elastic.Out)
147 .start();
148
149 this.destroyFruit(fruit);
150
151 this.increaseDifficulty();
152 }
153
154 destroyFruit (fruit)
155 {
156 if (this.fruits.contains(fruit))
157 {
158 this.fruits.remove(fruit);
159 this.fruitPool.push(fruit);
160 fruit.transform.position.x = 50000;
161 }
162 }
163
164 getFromFruitPoolOrCreate ()
165 {
166 return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new Fruit(0, 0, this.game.images.fruit, 0));
167 }
168
169 setRandomSubImage (fruit)
170 {
171 fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages));
172 }
173
174 draw ()
175 {
176 super.draw();
177
178 this.drawExternalFruit();
179 this.drawMenu();
180 }
181
182 getRandomVectorPosition ()
183 {
184 return new vector
185 (
186 Math.floor(Math.randomRange(0, this.real_size.x)),
187 0
188 );
189 }
190
191 shouldSpawn ()
192 {
193 return Math.random() < this.fruitSpawnChance;
194 }
195
196 checkSpacebar ()
197 {
198 if (GameInput.isHeld(GameInput.keys[" "]))
199 {
200 this.startGame();
201 }
202 }
203
204 drawExternalFruit ()
205 {
206
207 if (this.eatenFruit.length > 0)
208 {
209 let offX = this.game.images.fruit.width;
210 let offY = this.game.images.fruit.height;
211
212 let startingX = this.real_position.x - offX / 2;
213 let startingY = this.real_position.y - offY / 2;
214
215 let maxPerXAxis = Math.ceil(this.real_size.x / offX) + 1;
216 let xAxisCount = 0;
217
218 let maxPerYAxis = Math.ceil(this.real_size.y / offY) + 1;
219 let yAxisCount = 0;
220
221 let currentPos = new vector(startingX, startingY);
222
223 for (let i = 0; i < this.eatenFruit.length; i++)
224 {
225 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
226
227 this.game.images.fruit.draw(this.parentScene,
228 currentPos.x, currentPos.y,
229 offX * this.eatenFruit[i].scaleX, offY * this.eatenFruit[i].scaleY,
230 0, 0, 0, 1, this.eatenFruit[i].image);
231
232 if (xAxisCount < maxPerXAxis)
233 {
234 xAxisCount++;
235 currentPos = currentPos.add(new vector(offX, 0));
236 }
237 else if (yAxisCount < maxPerYAxis)
238 {
239 yAxisCount++;
240 currentPos = currentPos.add(new vector(0, offY));
241 }
242 else if (xAxisCount < maxPerXAxis * 2)
243 {
244 xAxisCount++;
245 currentPos = currentPos.subtract(new vector(offX, 0));
246 }
247 else if (yAxisCount < maxPerYAxis * 2)
248 {
249 yAxisCount++;
250 currentPos = currentPos.subtract(new vector(0, offY));
251 }
252 }
253 }
254 }
255
256 drawMenu ()
257 {
258 if (this.posScaleY < 2)
259 {
260 this.context.font = "20px Monospace";
261 this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
262
263
264 let textData = this.context.measureText("Press");
265 this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);
266
267 textData = this.context.measureText("To Start");
268 this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);
269
270 let space = this.game.images.spacebar;
271 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);
272 }
273 }
274
275 doMenuScreenTween ()
276 {
277 this.screenTween = new TWEEN.Tween(this)
278 .to({ posScaleY: 1 }, 800)
279 .easing(TWEEN.Easing.Elastic.Out)
280 .start();
281 }
282
283 doSpaceBarTweenIn ()
284 {
285 this.spaceBarTween = new TWEEN.Tween(this)
286 .to({ image_1_rotation: 1 }, 500)
287 .easing(TWEEN.Easing.Sinusoidal.InOut)
288 .yoyo(true)
289 .repeat(Infinity)
290 .start();
291 }
292
293 doAwayMenuScreenTween ()
294 {
295 this.screenTween
296 .to({ posScaleY: 2.5 }, 800)
297 .easing(TWEEN.Easing.Elastic.InOut)
298 .start();
299 }
300}