BlueGit

blue-star speeltuin

Merge remote-tracking branch 'origin/master'

timfa Tim · pushed by timfa

Authored 2022-10-21 · 4 years ago

Hash
fe9c78e1e962d7a63b93396e48554a39efe18420
2 files changed +116 −50
src/minigame/frog/frogScene.js +43 −38
@@ -27,6 +27,8 @@ class FrogScene extends Scene
27 27
28 28 destructing = false;
29 29
30 externalFruitCurrentPos = null;
31
30 32 constructor (x, y, width, height, displayMode = 0)
31 33 {
32 34 super(x, y, width, height, displayMode);
@@ -42,6 +44,14 @@ class FrogScene extends Scene
42 44 this.doSpaceBarTweenIn();
43 45 }
44 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
45 55 update (deltaTime)
46 56 {
47 57 super.update(deltaTime);
@@ -67,6 +77,10 @@ class FrogScene extends Scene
67 77
68 78 gameOver ()
69 79 {
80 if (this.spaceBarTween)
81 TWEEN.remove(this.spaceBarTween);
82 if (this.screenTween)
83 TWEEN.remove(this.screenTween);
70 84 this.start = false;
71 85 this.activeObjects.forEach(ao => ao.destroy());
72 86
@@ -137,7 +151,7 @@ class FrogScene extends Scene
137 151
138 152 consumeFruit (fruit)
139 153 {
140 let currFruit = { scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
154 let currFruit = { position: this.getNextExternalFruitPosition(), scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
141 155 this.eatenFruit.push(currFruit);
142 156
143 157 new TWEEN.Tween(currFruit)
@@ -150,6 +164,31 @@ class FrogScene extends Scene
150 164 this.increaseDifficulty();
151 165 }
152 166
167 getNextExternalFruitPosition ()
168 {
169 if (this.eatenFruit.length == 0)
170 {
171 return this.externalFruitCurrentPos;
172 }
173
174 if (this.eatenFruit.length < this.maxPerXAxis)
175 {
176 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(this.game.images.fruit.width, 0));
177 }
178 else if (this.eatenFruit.length - this.maxPerXAxis < this.maxPerYAxis)
179 {
180 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(0, this.game.images.fruit.height));
181 }
182 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerXAxis * 2)
183 {
184 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(this.game.images.fruit.width, 0));
185 }
186 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerYAxis * 2)
187 {
188 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(0, this.game.images.fruit.height));
189 }
190 }
191
153 192 destroyFruit (fruit)
154 193 {
155 194 if (this.fruits.contains(fruit))
@@ -202,52 +241,18 @@ class FrogScene extends Scene
202 241
203 242 drawExternalFruit ()
204 243 {
205
206 244 if (this.eatenFruit.length > 0)
207 245 {
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 246 for (let i = 0; i < this.eatenFruit.length; i++)
223 247 {
224 248 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
225 249
226 250 this.game.images.fruit.draw(this.parentScene,
227 currentPos.x, currentPos.y,
228 offX * this.eatenFruit[i].scaleX, offY * this.eatenFruit[i].scaleY,
251 this.eatenFruit[i].position.x, this.eatenFruit[i].position.y,
252 this.game.images.fruit.width * this.eatenFruit[i].scaleX, this.game.images.fruit.height * this.eatenFruit[i].scaleY,
229 253 0, 0, 0, 1, this.eatenFruit[i].image);
230 254
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 }
255
251 256 }
252 257 }
253 258 }
@@ -27,6 +27,8 @@ class FrogScene extends Scene
27
27
28 destructing = false;
28 destructing = false;
29
29
30 externalFruitCurrentPos = null;
31
30 constructor (x, y, width, height, displayMode = 0)
32 constructor (x, y, width, height, displayMode = 0)
31 {
33 {
32 super(x, y, width, height, displayMode);
34 super(x, y, width, height, displayMode);
@@ -42,6 +44,14 @@ class FrogScene extends Scene
42 this.doSpaceBarTweenIn();
44 this.doSpaceBarTweenIn();
43 }
45 }
44
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
45 update (deltaTime)
55 update (deltaTime)
46 {
56 {
47 super.update(deltaTime);
57 super.update(deltaTime);
@@ -67,6 +77,10 @@ class FrogScene extends Scene
67
77
68 gameOver ()
78 gameOver ()
69 {
79 {
80 if (this.spaceBarTween)
81 TWEEN.remove(this.spaceBarTween);
82 if (this.screenTween)
83 TWEEN.remove(this.screenTween);
70 this.start = false;
84 this.start = false;
71 this.activeObjects.forEach(ao => ao.destroy());
85 this.activeObjects.forEach(ao => ao.destroy());
72
86
@@ -137,7 +151,7 @@ class FrogScene extends Scene
137
151
138 consumeFruit (fruit)
152 consumeFruit (fruit)
139 {
153 {
140 let currFruit = { scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
154 let currFruit = { position: this.getNextExternalFruitPosition(), scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
141 this.eatenFruit.push(currFruit);
155 this.eatenFruit.push(currFruit);
142
156
143 new TWEEN.Tween(currFruit)
157 new TWEEN.Tween(currFruit)
@@ -150,6 +164,31 @@ class FrogScene extends Scene
150 this.increaseDifficulty();
164 this.increaseDifficulty();
151 }
165 }
152
166
167 getNextExternalFruitPosition ()
168 {
169 if (this.eatenFruit.length == 0)
170 {
171 return this.externalFruitCurrentPos;
172 }
173
174 if (this.eatenFruit.length < this.maxPerXAxis)
175 {
176 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(this.game.images.fruit.width, 0));
177 }
178 else if (this.eatenFruit.length - this.maxPerXAxis < this.maxPerYAxis)
179 {
180 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(0, this.game.images.fruit.height));
181 }
182 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerXAxis * 2)
183 {
184 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(this.game.images.fruit.width, 0));
185 }
186 else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerYAxis * 2)
187 {
188 return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(0, this.game.images.fruit.height));
189 }
190 }
191
153 destroyFruit (fruit)
192 destroyFruit (fruit)
154 {
193 {
155 if (this.fruits.contains(fruit))
194 if (this.fruits.contains(fruit))
@@ -202,52 +241,18 @@ class FrogScene extends Scene
202
241
203 drawExternalFruit ()
242 drawExternalFruit ()
204 {
243 {
205
206 if (this.eatenFruit.length > 0)
244 if (this.eatenFruit.length > 0)
207 {
245 {
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++)
246 for (let i = 0; i < this.eatenFruit.length; i++)
223 {
247 {
224 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
248 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
225
249
226 this.game.images.fruit.draw(this.parentScene,
250 this.game.images.fruit.draw(this.parentScene,
227 currentPos.x, currentPos.y,
251 this.eatenFruit[i].position.x, this.eatenFruit[i].position.y,
228 offX * this.eatenFruit[i].scaleX, offY * this.eatenFruit[i].scaleY,
252 this.game.images.fruit.width * this.eatenFruit[i].scaleX, this.game.images.fruit.height * this.eatenFruit[i].scaleY,
229 0, 0, 0, 1, this.eatenFruit[i].image);
253 0, 0, 0, 1, this.eatenFruit[i].image);
230
254
231 if (xAxisCount < maxPerXAxis)
255
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 }
256 }
252 }
257 }
253 }
258 }
src/objects/menus/menuManager.js +73 −12
@@ -6,6 +6,16 @@ class MenuManager extends GameObject
6 6 buttonPadding = 10;
7 7 buttonXOffset = -32;
8 8 state = 0;
9 prevState = 0;
10
11 openTween = null;
12 openTweenTime = 800;
13
14 closeTween = null;
15 closeTweenTime = 1400;
16
17 tweenTimer = 0;
18 timeDiffPercent = 1;
9 19
10 20 static States = { closed: 0, open: 1, moving: 2 };
11 21
@@ -13,11 +23,14 @@ class MenuManager extends GameObject
13 23 {
14 24 super(x, y, sprite, layer);
15 25 MenuManager.Instance = this;
26
27 this.openTween = this.createOpenTween(this.transform.position, { x: 60 }, this.openTweenTime);
28 this.closeTween = this.createCloseTween(this.transform.position, { x: -15 }, this.closeTweenTime);
16 29 }
17 30
18 31 addedToScene ()
19 32 {
20 this.moveToOpen(this.moveToClose);
33 this.moveToOpen(() => this.moveToClose());
21 34 this.buttons =
22 35 [
23 36 this.scene.addObject(new BtnFruit(0, 0, this.scene.game.images.btnFruit)),
@@ -29,43 +42,91 @@ class MenuManager extends GameObject
29 42
30 43 onClick (btn)
31 44 {
32 this.state == MenuManager.States.open ? this.moveToClose() : this.moveToOpen();
45 this.prevState == MenuManager.States.closed ?
46 this.moveToClose() :
47 this.moveToOpen();
33 48 }
34 49
35 50 moveToOpen (onDone)
36 51 {
52 this.prevState = MenuManager.States.closed;
53
37 54 if (this.state == MenuManager.States.moving)
38 return;
55 this.closeTween.stop();
56
57 this.openTween.to({ x: 60 }, this.openTweenTime * this.timeDiffPercent);
58 console.log("open " + this.openTweenTime * this.timeDiffPercent + " time diff : " + this.timeDiffPercent);
39 59
40 60 this.state = MenuManager.States.moving;
41 61
42 62 let context = this;
43 new TWEEN.Tween(this.transform.position)
44 .to({ x: 60 }, 800)
45 .easing(TWEEN.Easing.Bounce.Out)
46 .onComplete(() => { this.state = MenuManager.States.open; onDone && onDone.call(context); })
63 this.openTween
64 .onComplete(() => { this.state = MenuManager.States.open; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); })
47 65 .start();
48 66 }
49 67
50 68 moveToClose (onDone)
51 69 {
70 this.prevState = MenuManager.States.open;
71
52 72 if (this.state == MenuManager.States.moving)
53 return;
73 this.openTween.stop();
74
75 this.closeTween.to({ x: -15 }, this.closeTweenTime * this.timeDiffPercent);
76
77 console.log("close " + this.closeTweenTime * this.timeDiffPercent + " time diff : " + this.timeDiffPercent);
54 78
55 79 this.state = MenuManager.States.moving;
56 80
57 81 let context = this;
58 new TWEEN.Tween(this.transform.position)
59 .to({ x: -15 }, 1500)
60 .easing(TWEEN.Easing.Elastic.Out)
61 .onComplete(() => { this.state = MenuManager.States.closed; onDone && onDone.call(context); })
82 this.closeTween
83 .onComplete(() => { this.state = MenuManager.States.closed; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); })
62 84 .start();
63 85 }
64 86
87 createOpenTween (target, newTarget, length)
88 {
89 return new TWEEN.Tween(target)
90 .to(newTarget, length)
91 .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.openTweenTime); this.tweenTimer = 0; })
92 .easing(TWEEN.Easing.Bounce.Out);
93 }
94
95 createCloseTween (target, newTarget, length)
96 {
97 return new TWEEN.Tween(target)
98 .to(newTarget, length)
99 .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.closeTweenTime); this.tweenTimer = 0; })
100 .easing(TWEEN.Easing.Elastic.Out);
101 }
102
103 calculateTimeLeftPercent (expectedTimeLength)
104 {
105 let t = Math.round(this.tweenTimer * 1000);
106 if (t == 0)
107 return 1;
108 else if (t < expectedTimeLength)
109 return Math.max(t / expectedTimeLength, .5);
110 return 1;
111 }
112
65 113 update (deltaTime)
66 114 {
67 115 super.update(deltaTime);
68 116
117 if (this.state == MenuManager.States.moving)
118 {
119 this.tweenTimer += deltaTime;
120 }
121 else
122 {
123 if (this.tweenTimer > 0)
124 {
125 this.tweenTimer = 0;
126 this.timeDiffPercent = 1;
127 }
128 }
129
69 130 for (let i = 0; i < this.buttons.length; i++)
70 131 {
71 132 if (i == 0)
@@ -6,6 +6,16 @@ class MenuManager extends GameObject
6 buttonPadding = 10;
6 buttonPadding = 10;
7 buttonXOffset = -32;
7 buttonXOffset = -32;
8 state = 0;
8 state = 0;
9 prevState = 0;
10
11 openTween = null;
12 openTweenTime = 800;
13
14 closeTween = null;
15 closeTweenTime = 1400;
16
17 tweenTimer = 0;
18 timeDiffPercent = 1;
9
19
10 static States = { closed: 0, open: 1, moving: 2 };
20 static States = { closed: 0, open: 1, moving: 2 };
11
21
@@ -13,11 +23,14 @@ class MenuManager extends GameObject
13 {
23 {
14 super(x, y, sprite, layer);
24 super(x, y, sprite, layer);
15 MenuManager.Instance = this;
25 MenuManager.Instance = this;
26
27 this.openTween = this.createOpenTween(this.transform.position, { x: 60 }, this.openTweenTime);
28 this.closeTween = this.createCloseTween(this.transform.position, { x: -15 }, this.closeTweenTime);
16 }
29 }
17
30
18 addedToScene ()
31 addedToScene ()
19 {
32 {
20 this.moveToOpen(this.moveToClose);
33 this.moveToOpen(() => this.moveToClose());
21 this.buttons =
34 this.buttons =
22 [
35 [
23 this.scene.addObject(new BtnFruit(0, 0, this.scene.game.images.btnFruit)),
36 this.scene.addObject(new BtnFruit(0, 0, this.scene.game.images.btnFruit)),
@@ -29,43 +42,91 @@ class MenuManager extends GameObject
29
42
30 onClick (btn)
43 onClick (btn)
31 {
44 {
32 this.state == MenuManager.States.open ? this.moveToClose() : this.moveToOpen();
45 this.prevState == MenuManager.States.closed ?
46 this.moveToClose() :
47 this.moveToOpen();
33 }
48 }
34
49
35 moveToOpen (onDone)
50 moveToOpen (onDone)
36 {
51 {
52 this.prevState = MenuManager.States.closed;
53
37 if (this.state == MenuManager.States.moving)
54 if (this.state == MenuManager.States.moving)
38 return;
55 this.closeTween.stop();
56
57 this.openTween.to({ x: 60 }, this.openTweenTime * this.timeDiffPercent);
58 console.log("open " + this.openTweenTime * this.timeDiffPercent + " time diff : " + this.timeDiffPercent);
39
59
40 this.state = MenuManager.States.moving;
60 this.state = MenuManager.States.moving;
41
61
42 let context = this;
62 let context = this;
43 new TWEEN.Tween(this.transform.position)
63 this.openTween
44 .to({ x: 60 }, 800)
64 .onComplete(() => { this.state = MenuManager.States.open; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); })
45 .easing(TWEEN.Easing.Bounce.Out)
46 .onComplete(() => { this.state = MenuManager.States.open; onDone && onDone.call(context); })
47 .start();
65 .start();
48 }
66 }
49
67
50 moveToClose (onDone)
68 moveToClose (onDone)
51 {
69 {
70 this.prevState = MenuManager.States.open;
71
52 if (this.state == MenuManager.States.moving)
72 if (this.state == MenuManager.States.moving)
53 return;
73 this.openTween.stop();
74
75 this.closeTween.to({ x: -15 }, this.closeTweenTime * this.timeDiffPercent);
76
77 console.log("close " + this.closeTweenTime * this.timeDiffPercent + " time diff : " + this.timeDiffPercent);
54
78
55 this.state = MenuManager.States.moving;
79 this.state = MenuManager.States.moving;
56
80
57 let context = this;
81 let context = this;
58 new TWEEN.Tween(this.transform.position)
82 this.closeTween
59 .to({ x: -15 }, 1500)
83 .onComplete(() => { this.state = MenuManager.States.closed; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); })
60 .easing(TWEEN.Easing.Elastic.Out)
61 .onComplete(() => { this.state = MenuManager.States.closed; onDone && onDone.call(context); })
62 .start();
84 .start();
63 }
85 }
64
86
87 createOpenTween (target, newTarget, length)
88 {
89 return new TWEEN.Tween(target)
90 .to(newTarget, length)
91 .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.openTweenTime); this.tweenTimer = 0; })
92 .easing(TWEEN.Easing.Bounce.Out);
93 }
94
95 createCloseTween (target, newTarget, length)
96 {
97 return new TWEEN.Tween(target)
98 .to(newTarget, length)
99 .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.closeTweenTime); this.tweenTimer = 0; })
100 .easing(TWEEN.Easing.Elastic.Out);
101 }
102
103 calculateTimeLeftPercent (expectedTimeLength)
104 {
105 let t = Math.round(this.tweenTimer * 1000);
106 if (t == 0)
107 return 1;
108 else if (t < expectedTimeLength)
109 return Math.max(t / expectedTimeLength, .5);
110 return 1;
111 }
112
65 update (deltaTime)
113 update (deltaTime)
66 {
114 {
67 super.update(deltaTime);
115 super.update(deltaTime);
68
116
117 if (this.state == MenuManager.States.moving)
118 {
119 this.tweenTimer += deltaTime;
120 }
121 else
122 {
123 if (this.tweenTimer > 0)
124 {
125 this.tweenTimer = 0;
126 this.timeDiffPercent = 1;
127 }
128 }
129
69 for (let i = 0; i < this.buttons.length; i++)
130 for (let i = 0; i < this.buttons.length; i++)
70 {
131 {
71 if (i == 0)
132 if (i == 0)