BlueGit

blue-star speeltuin

frog gameloop & things

timfa wsadhelios · pushed by timfa

Authored 2022-10-21 · 4 years ago

Hash
da29ae09c62a48e698d26384e388184d0e19ac96
Parent
fa489339
6 files changed +185 −26
build/framework.js +3 −5
@@ -413,11 +413,9 @@ class Collider
413 413 this.gameObject.transform.position.y + hh
414 414 ];
415 415
416 if (points[0] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw ||
417 points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw)
416 if (points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw)
418 417 {
419 if (points[2] > o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh ||
420 points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh)
418 if (points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh)
421 419 {
422 420 hit = true;
423 421 }
@@ -817,7 +815,7 @@ class Scene
817 815
818 816 if (this.pendingRemoval.length)
819 817 {
820 this.pendingRemoval.sort();
818 this.pendingRemoval.sort((a, b) => a - b);
821 819
822 820 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
823 821 {
@@ -413,11 +413,9 @@ class Collider
413 this.gameObject.transform.position.y + hh
413 this.gameObject.transform.position.y + hh
414 ];
414 ];
415
415
416 if (points[0] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw ||
416 if (points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw)
417 points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw)
418 {
417 {
419 if (points[2] > o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh ||
418 if (points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh)
420 points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh)
421 {
419 {
422 hit = true;
420 hit = true;
423 }
421 }
@@ -817,7 +815,7 @@ class Scene
817
815
818 if (this.pendingRemoval.length)
816 if (this.pendingRemoval.length)
819 {
817 {
820 this.pendingRemoval.sort();
818 this.pendingRemoval.sort((a, b) => a - b);
821
819
822 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
820 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
823 {
821 {
build/images/spacebar.png added Binary file +0 −0

The contents of this binary file are not shown.

src/game.js +2 −8
@@ -31,7 +31,8 @@ window.onload = function ()
31 31 { name: "chao_yellow", url: "images/character/chao/yellow.png", subImgTotal: 67, perRow: 9 },
32 32 { name: "tile_floor_brown", url: "images/tile/tile_floor_brown.png" },
33 33 { name: "tile_floor_explosion", url: "images/tile/tile_floor_explosion_anim.png", subImgTotal: 10, perRow: 5 },
34 { name: "fruit", url: "images/object/fruit.png", subImgTotal: 15, perRow: 8 }
34 { name: "fruit", url: "images/object/fruit.png", subImgTotal: 15, perRow: 8 },
35 { name: "spacebar", url: "images/spacebar.png" }
35 36 ]
36 37 , function ()
37 38 {
@@ -129,11 +130,4 @@ createSubScene = function (scene)
129 130 subScene.gameSpeed = 1;
130 131
131 132 scene.loadChildScene(subScene);
132
133 frog = new Frog(60, subScene.real_size.y - 50, game.images.frog_basic, 0);
134 for (tx = 10; tx < subScene.real_size.x - 10; tx += 15)
135 {
136 subScene.addObject(new FloorTile(tx, subScene.real_size.y - 30, game.images.tile_floor_brown, 0));
137 }
138 subScene.addObject(frog);
139 133 };
@@ -31,7 +31,8 @@ window.onload = function ()
31 { name: "chao_yellow", url: "images/character/chao/yellow.png", subImgTotal: 67, perRow: 9 },
31 { name: "chao_yellow", url: "images/character/chao/yellow.png", subImgTotal: 67, perRow: 9 },
32 { name: "tile_floor_brown", url: "images/tile/tile_floor_brown.png" },
32 { name: "tile_floor_brown", url: "images/tile/tile_floor_brown.png" },
33 { name: "tile_floor_explosion", url: "images/tile/tile_floor_explosion_anim.png", subImgTotal: 10, perRow: 5 },
33 { name: "tile_floor_explosion", url: "images/tile/tile_floor_explosion_anim.png", subImgTotal: 10, perRow: 5 },
34 { name: "fruit", url: "images/object/fruit.png", subImgTotal: 15, perRow: 8 }
34 { name: "fruit", url: "images/object/fruit.png", subImgTotal: 15, perRow: 8 },
35 { name: "spacebar", url: "images/spacebar.png" }
35 ]
36 ]
36 , function ()
37 , function ()
37 {
38 {
@@ -129,11 +130,4 @@ createSubScene = function (scene)
129 subScene.gameSpeed = 1;
130 subScene.gameSpeed = 1;
130
131
131 scene.loadChildScene(subScene);
132 scene.loadChildScene(subScene);
132
133 frog = new Frog(60, subScene.real_size.y - 50, game.images.frog_basic, 0);
134 for (tx = 10; tx < subScene.real_size.x - 10; tx += 15)
135 {
136 subScene.addObject(new FloorTile(tx, subScene.real_size.y - 30, game.images.tile_floor_brown, 0));
137 }
138 subScene.addObject(frog);
139};
133};
src/minigame/frog/floorTile.js +27 −1
@@ -1,14 +1,40 @@
1 1 class FloorTile extends GameObject
2 2 {
3 g_target_y = 0;
3 4
4 5 constructor (x, y, sprite, layer)
5 6 {
6 7 super(x, y, sprite, layer);
7 this.collider.enabled = true;
8 this.collider.enabled = false;
9
10 this.transform.position.y = 0;
11 this.g_target_y = y;
12 this.posScaleY = 0;
13
14 new TWEEN.Tween(this)
15 .to({ posScaleY: 1 }, 800)
16 .easing(TWEEN.Easing.Elastic.Out)
17 .start()
18 .onComplete(() => this.collider.enabled = true);
19 }
20
21 update (deltaTime)
22 {
23 super.update(deltaTime);
24
25 this.animate();
8 26 }
9 27
10 28 onDestroy ()
11 29 {
12 30 this.scene.addObject(new FloorTileDestroyer(this.transform.position.x, this.transform.position.y, this.scene.game.images.tile_floor_explosion, 0, .05, true));
13 31 }
32
33 animate ()
34 {
35 if (this.transform.position.y != this.g_target_y)
36 {
37 this.transform.position.y = this.g_target_y * this.posScaleY;
38 }
39 }
14 40 }
@@ -1,14 +1,40 @@
1class FloorTile extends GameObject
1class FloorTile extends GameObject
2{
2{
3 g_target_y = 0;
3
4
4 constructor (x, y, sprite, layer)
5 constructor (x, y, sprite, layer)
5 {
6 {
6 super(x, y, sprite, layer);
7 super(x, y, sprite, layer);
7 this.collider.enabled = true;
8 this.collider.enabled = false;
9
10 this.transform.position.y = 0;
11 this.g_target_y = y;
12 this.posScaleY = 0;
13
14 new TWEEN.Tween(this)
15 .to({ posScaleY: 1 }, 800)
16 .easing(TWEEN.Easing.Elastic.Out)
17 .start()
18 .onComplete(() => this.collider.enabled = true);
19 }
20
21 update (deltaTime)
22 {
23 super.update(deltaTime);
24
25 this.animate();
8 }
26 }
9
27
10 onDestroy ()
28 onDestroy ()
11 {
29 {
12 this.scene.addObject(new FloorTileDestroyer(this.transform.position.x, this.transform.position.y, this.scene.game.images.tile_floor_explosion, 0, .05, true));
30 this.scene.addObject(new FloorTileDestroyer(this.transform.position.x, this.transform.position.y, this.scene.game.images.tile_floor_explosion, 0, .05, true));
13 }
31 }
32
33 animate ()
34 {
35 if (this.transform.position.y != this.g_target_y)
36 {
37 this.transform.position.y = this.g_target_y * this.posScaleY;
38 }
39 }
14}
40}
src/minigame/frog/frog.js +26 −2
@@ -28,10 +28,22 @@ class Frog extends GameObject
28 28
29 29 tongue_obj_ref = null;
30 30
31 g_target_y = 0;
32
31 33 constructor (x, y, sprite, layer)
32 34 {
33 35 super(x, y, sprite, layer);
34 this.collider.enabled = true;
36 this.collider.enabled = false;
37
38 this.transform.position.y = 0;
39 this.g_target_y = y;
40 this.posScaleY = 0;
41
42 new TWEEN.Tween(this)
43 .to({ posScaleY: 1 }, 800)
44 .easing(TWEEN.Easing.Elastic.Out)
45 .start()
46 .onComplete(() => this.collider.enabled = true);
35 47 }
36 48
37 49 addedToScene ()
@@ -42,7 +54,10 @@ class Frog extends GameObject
42 54
43 55 onCollision (obj)
44 56 {
45 // todo: todo
57 if (obj instanceof Fruit)
58 {
59 this.scene.gameOver();
60 }
46 61 }
47 62
48 63 isFloor ()
@@ -59,6 +74,7 @@ class Frog extends GameObject
59 74 {
60 75 super.update(deltaTime);
61 76
77 this.animate();
62 78 this.idle = true;
63 79
64 80 if (this.tongue_retracted)
@@ -322,4 +338,12 @@ class Frog extends GameObject
322 338 dir.toAngle() - 90, 0, 0, 1, 0);
323 339 }
324 340 }
341
342 animate ()
343 {
344 if (this.transform.position.y != this.g_target_y)
345 {
346 this.transform.position.y = this.g_target_y * this.posScaleY;
347 }
348 }
325 349 }
@@ -28,10 +28,22 @@ class Frog extends GameObject
28
28
29 tongue_obj_ref = null;
29 tongue_obj_ref = null;
30
30
31 g_target_y = 0;
32
31 constructor (x, y, sprite, layer)
33 constructor (x, y, sprite, layer)
32 {
34 {
33 super(x, y, sprite, layer);
35 super(x, y, sprite, layer);
34 this.collider.enabled = true;
36 this.collider.enabled = false;
37
38 this.transform.position.y = 0;
39 this.g_target_y = y;
40 this.posScaleY = 0;
41
42 new TWEEN.Tween(this)
43 .to({ posScaleY: 1 }, 800)
44 .easing(TWEEN.Easing.Elastic.Out)
45 .start()
46 .onComplete(() => this.collider.enabled = true);
35 }
47 }
36
48
37 addedToScene ()
49 addedToScene ()
@@ -42,7 +54,10 @@ class Frog extends GameObject
42
54
43 onCollision (obj)
55 onCollision (obj)
44 {
56 {
45 // todo: todo
57 if (obj instanceof Fruit)
58 {
59 this.scene.gameOver();
60 }
46 }
61 }
47
62
48 isFloor ()
63 isFloor ()
@@ -59,6 +74,7 @@ class Frog extends GameObject
59 {
74 {
60 super.update(deltaTime);
75 super.update(deltaTime);
61
76
77 this.animate();
62 this.idle = true;
78 this.idle = true;
63
79
64 if (this.tongue_retracted)
80 if (this.tongue_retracted)
@@ -322,4 +338,12 @@ class Frog extends GameObject
322 dir.toAngle() - 90, 0, 0, 1, 0);
338 dir.toAngle() - 90, 0, 0, 1, 0);
323 }
339 }
324 }
340 }
341
342 animate ()
343 {
344 if (this.transform.position.y != this.g_target_y)
345 {
346 this.transform.position.y = this.g_target_y * this.posScaleY;
347 }
348 }
325}
349}
src/minigame/frog/frogScene.js +127 −10
@@ -13,18 +13,54 @@ class FrogScene extends Scene
13 13
14 14 gameSpeed = 1;
15 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
16 28 constructor (x, y, width, height, displayMode = 0)
17 29 {
18 30 super(x, y, width, height, displayMode);
19 31
20 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();
21 41 }
22 42
23 43 update (deltaTime)
24 44 {
25 45 super.update(deltaTime);
26 46
27 this.spawnFruits(deltaTime);
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
28 64 }
29 65
30 66 increaseDifficulty ()
@@ -45,6 +81,29 @@ class FrogScene extends Scene
45 81 }
46 82 }
47 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 }
48 107
49 108 spawnFruits (deltaTime)
50 109 {
@@ -104,6 +163,35 @@ class FrogScene extends Scene
104 163 {
105 164 super.draw();
106 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
107 195 if (this.eatenFruit.length > 0)
108 196 {
109 197 let offX = this.game.images.fruit.width;
@@ -151,23 +239,52 @@ class FrogScene extends Scene
151 239 yAxisCount++;
152 240 currentPos = currentPos.subtract(new vector(0, offY));
153 241 }
242 }
243 }
244 }
154 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
155 252
156 }
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);
157 262 }
158 263 }
159 264
160 getRandomVectorPosition ()
265 doMenuScreenTween ()
161 266 {
162 return new vector
163 (
164 Math.floor(Math.randomRange(0, this.real_size.x)),
165 0
166 );
267 this.screenTween = new TWEEN.Tween(this)
268 .to({ posScaleY: 1 }, 800)
269 .easing(TWEEN.Easing.Elastic.Out)
270 .start();
167 271 }
168 272
169 shouldSpawn ()
273 doSpaceBarTweenIn ()
170 274 {
171 return Math.random() < this.fruitSpawnChance;
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();
172 289 }
173 290 }
@@ -13,18 +13,54 @@ class FrogScene extends Scene
13
13
14 gameSpeed = 1;
14 gameSpeed = 1;
15
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
16 constructor (x, y, width, height, displayMode = 0)
28 constructor (x, y, width, height, displayMode = 0)
17 {
29 {
18 super(x, y, width, height, displayMode);
30 super(x, y, width, height, displayMode);
19
31
20 this.fruitSpawnTimer = this.fruitSpawnRateMax;
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();
21 }
41 }
22
42
23 update (deltaTime)
43 update (deltaTime)
24 {
44 {
25 super.update(deltaTime);
45 super.update(deltaTime);
26
46
27 this.spawnFruits(deltaTime);
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
28 }
64 }
29
65
30 increaseDifficulty ()
66 increaseDifficulty ()
@@ -45,6 +81,29 @@ class FrogScene extends Scene
45 }
81 }
46 }
82 }
47
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 }
48
107
49 spawnFruits (deltaTime)
108 spawnFruits (deltaTime)
50 {
109 {
@@ -104,6 +163,35 @@ class FrogScene extends Scene
104 {
163 {
105 super.draw();
164 super.draw();
106
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
107 if (this.eatenFruit.length > 0)
195 if (this.eatenFruit.length > 0)
108 {
196 {
109 let offX = this.game.images.fruit.width;
197 let offX = this.game.images.fruit.width;
@@ -151,23 +239,52 @@ class FrogScene extends Scene
151 yAxisCount++;
239 yAxisCount++;
152 currentPos = currentPos.subtract(new vector(0, offY));
240 currentPos = currentPos.subtract(new vector(0, offY));
153 }
241 }
242 }
243 }
244 }
154
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
155
252
156 }
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);
157 }
262 }
158 }
263 }
159
264
160 getRandomVectorPosition ()
265 doMenuScreenTween ()
161 {
266 {
162 return new vector
267 this.screenTween = new TWEEN.Tween(this)
163 (
268 .to({ posScaleY: 1 }, 800)
164 Math.floor(Math.randomRange(0, this.real_size.x)),
269 .easing(TWEEN.Easing.Elastic.Out)
165 0
270 .start();
166 );
167 }
271 }
168
272
169 shouldSpawn ()
273 doSpaceBarTweenIn ()
170 {
274 {
171 return Math.random() < this.fruitSpawnChance;
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();
172 }
289 }
173}
290}