BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

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