BlueGit

blue-star speeltuin

finally fruit animation + layering on fruitInventory

timfa wsadhelios · pushed by timfa

Authored 2022-10-24 · 4 years ago

Hash
ec2d9da386bcba3f9e80d381d1b029807b0d85ef
Parent
a5888833
5 files changed +111 −26
src/game.js +2 −3
@@ -129,9 +129,8 @@ createMainScene = function (game)
129 129 chao = new Chao(Math.randomRange(0, 704), Math.randomRange(0, 640), game.images.chao_default, 0);
130 130 scene.addObject(chao);
131 131
132 let menu = new MainMenu(0, 50, 60, 50, game.images.menuButton, 5);
133
134 scene.addObject(menu);
132 scene.addObject(new MainMenu(0, 50, 60, 50, game.images.menuButton, 5));
133 scene.addObject(new SimpleImagesRenderTween(-100, -100, game.images.arrow, 1));
135 134
136 135 WorldObjectTracker.Load(scene);
137 136 };
@@ -129,9 +129,8 @@ createMainScene = function (game)
129 chao = new Chao(Math.randomRange(0, 704), Math.randomRange(0, 640), game.images.chao_default, 0);
129 chao = new Chao(Math.randomRange(0, 704), Math.randomRange(0, 640), game.images.chao_default, 0);
130 scene.addObject(chao);
130 scene.addObject(chao);
131
131
132 let menu = new MainMenu(0, 50, 60, 50, game.images.menuButton, 5);
132 scene.addObject(new MainMenu(0, 50, 60, 50, game.images.menuButton, 5));
133
133 scene.addObject(new SimpleImagesRenderTween(-100, -100, game.images.arrow, 1));
134 scene.addObject(menu);
135
134
136 WorldObjectTracker.Load(scene);
135 WorldObjectTracker.Load(scene);
137};
136};
src/minigame/frog/frogScene.js +31 −18
@@ -121,11 +121,32 @@ class FrogScene extends Scene
121 121 TWEEN.remove(this.screenTween);
122 122 this.activeObjects.forEach(ao => ao.destroy());
123 123
124 // pass over fruits array to parent scene
124 this.passEatenFruitsToSimpleImagesRenderer();
125 125
126 126 this.destructing = true; // finish animations first
127 127 }
128 128
129 passEatenFruitsToSimpleImagesRenderer ()
130 {
131
132 let targetButton = MainMenu.Instance.children.btnFruit;
133 let targetVector = targetButton.targetPosition;
134
135 this.eatenFruit.forEach(e =>
136 {
137 e.targetPosition = targetVector;
138 e.targetSize = new vector(.5, .5);
139 e.time = Math.floor(Math.randomRange(500, 700));
140 });
141
142 if (this.eatenFruit.length > 0)
143 {
144 SimpleImagesRenderTween.Instance.add(this.eatenFruit, () => targetButton.moveToActiveState(800), () => targetButton.moveToInactiveState(800));
145 }
146
147 this.eatenFruit = [];
148 }
149
129 150 increaseDifficulty ()
130 151 {
131 152 this.gameSpeed += .1;
@@ -200,11 +221,11 @@ class FrogScene extends Scene
200 221
201 222 consumeFruit (fruit)
202 223 {
203 let currFruit = { position: this.getNextExternalFruitPosition(), scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
224 let currFruit = { position: this.getNextExternalFruitPosition(), size: new vector(.5, .5), image: game.images.fruit, subImage: fruit.renderer.subImage };
204 225 this.eatenFruit.push(currFruit);
205 226
206 new TWEEN.Tween(currFruit)
207 .to({ scaleX: 1, scaleY: 1 }, 400)
227 new TWEEN.Tween(currFruit.size)
228 .to({ x: 1, y: 1 }, 400)
208 229 .easing(TWEEN.Easing.Elastic.Out)
209 230 .start();
210 231
@@ -212,7 +233,7 @@ class FrogScene extends Scene
212 233
213 234 this.increaseDifficulty();
214 235
215 FruitInventory.Instance.addToInventory(fruit.renderer.subImage);
236 FruitInventory.Instance.addToInventory(currFruit.subImage);
216 237 }
217 238
218 239 getNextExternalFruitPosition ()
@@ -301,20 +322,12 @@ class FrogScene extends Scene
301 322
302 323 drawExternalFruit ()
303 324 {
304 if (this.eatenFruit.length > 0)
305 {
306 for (let i = 0; i < this.eatenFruit.length; i++)
307 {
308 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
325 this.eatenFruit.forEach(e =>
326 this.game.images.fruit.draw(this.parentScene,
327 e.position.x, e.position.y,
328 this.game.images.fruit.width * e.size.x, this.game.images.fruit.height * e.size.y,
329 0, 0, 0, 1, e.subImage));
309 330
310 this.game.images.fruit.draw(this.parentScene,
311 this.eatenFruit[i].position.x, this.eatenFruit[i].position.y,
312 this.game.images.fruit.width * this.eatenFruit[i].scaleX, this.game.images.fruit.height * this.eatenFruit[i].scaleY,
313 0, 0, 0, 1, this.eatenFruit[i].image);
314
315
316 }
317 }
318 331 }
319 332
320 333 drawMenu ()
@@ -121,11 +121,32 @@ class FrogScene extends Scene
121 TWEEN.remove(this.screenTween);
121 TWEEN.remove(this.screenTween);
122 this.activeObjects.forEach(ao => ao.destroy());
122 this.activeObjects.forEach(ao => ao.destroy());
123
123
124 // pass over fruits array to parent scene
124 this.passEatenFruitsToSimpleImagesRenderer();
125
125
126 this.destructing = true; // finish animations first
126 this.destructing = true; // finish animations first
127 }
127 }
128
128
129 passEatenFruitsToSimpleImagesRenderer ()
130 {
131
132 let targetButton = MainMenu.Instance.children.btnFruit;
133 let targetVector = targetButton.targetPosition;
134
135 this.eatenFruit.forEach(e =>
136 {
137 e.targetPosition = targetVector;
138 e.targetSize = new vector(.5, .5);
139 e.time = Math.floor(Math.randomRange(500, 700));
140 });
141
142 if (this.eatenFruit.length > 0)
143 {
144 SimpleImagesRenderTween.Instance.add(this.eatenFruit, () => targetButton.moveToActiveState(800), () => targetButton.moveToInactiveState(800));
145 }
146
147 this.eatenFruit = [];
148 }
149
129 increaseDifficulty ()
150 increaseDifficulty ()
130 {
151 {
131 this.gameSpeed += .1;
152 this.gameSpeed += .1;
@@ -200,11 +221,11 @@ class FrogScene extends Scene
200
221
201 consumeFruit (fruit)
222 consumeFruit (fruit)
202 {
223 {
203 let currFruit = { position: this.getNextExternalFruitPosition(), scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
224 let currFruit = { position: this.getNextExternalFruitPosition(), size: new vector(.5, .5), image: game.images.fruit, subImage: fruit.renderer.subImage };
204 this.eatenFruit.push(currFruit);
225 this.eatenFruit.push(currFruit);
205
226
206 new TWEEN.Tween(currFruit)
227 new TWEEN.Tween(currFruit.size)
207 .to({ scaleX: 1, scaleY: 1 }, 400)
228 .to({ x: 1, y: 1 }, 400)
208 .easing(TWEEN.Easing.Elastic.Out)
229 .easing(TWEEN.Easing.Elastic.Out)
209 .start();
230 .start();
210
231
@@ -212,7 +233,7 @@ class FrogScene extends Scene
212
233
213 this.increaseDifficulty();
234 this.increaseDifficulty();
214
235
215 FruitInventory.Instance.addToInventory(fruit.renderer.subImage);
236 FruitInventory.Instance.addToInventory(currFruit.subImage);
216 }
237 }
217
238
218 getNextExternalFruitPosition ()
239 getNextExternalFruitPosition ()
@@ -301,20 +322,12 @@ class FrogScene extends Scene
301
322
302 drawExternalFruit ()
323 drawExternalFruit ()
303 {
324 {
304 if (this.eatenFruit.length > 0)
325 this.eatenFruit.forEach(e =>
305 {
326 this.game.images.fruit.draw(this.parentScene,
306 for (let i = 0; i < this.eatenFruit.length; i++)
327 e.position.x, e.position.y,
307 {
328 this.game.images.fruit.width * e.size.x, this.game.images.fruit.height * e.size.y,
308 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
329 0, 0, 0, 1, e.subImage));
309
330
310 this.game.images.fruit.draw(this.parentScene,
311 this.eatenFruit[i].position.x, this.eatenFruit[i].position.y,
312 this.game.images.fruit.width * this.eatenFruit[i].scaleX, this.game.images.fruit.height * this.eatenFruit[i].scaleY,
313 0, 0, 0, 1, this.eatenFruit[i].image);
314
315
316 }
317 }
318 }
331 }
319
332
320 drawMenu ()
333 drawMenu ()
src/minigame/simpleImagesRenderTween.js added +76 −0
@@ -0,0 +1,76 @@
1 class SimpleImagesRenderTween extends GameObject
2 {
3 static Instance;
4
5 targetPosition = null;
6 targetSize = null;
7
8 imageDataPerRequest = []; //[{position, size, targetPosition, targetSize,image, subimage, time}]
9
10 time = 400;
11
12 constructor (x, y, sprite, layer)
13 {
14 super(x, y, sprite, layer);
15
16 SimpleImagesRenderTween.Instance = this;
17 }
18
19 add (arr, onStart, onDone)
20 {
21 this.imageDataPerRequest.push({ data: arr, onStart: onStart, onDone: onDone });
22 let current = this.imageDataPerRequest[this.imageDataPerRequest.length - 1];
23 arr.forEach(e => this.createTween(e, current));
24 }
25
26 update (deltaTime)
27 {
28 super.update(deltaTime);
29 this.clean();
30 }
31
32 draw (context)
33 {
34 super.draw(context);
35 this.drawImages();
36 }
37
38 clean ()
39 {
40 this.imageDataPerRequest = this.imageDataPerRequest.filter(e => !e.done);
41 }
42
43 drawImages ()
44 {
45 let context = this;
46 this.imageDataPerRequest.forEach(arr => arr.data.forEach(e =>
47 context.scene.game.images.fruit.draw(context.scene,
48 e.position.x, e.position.y,
49 e.image.width * e.size.x, game.images.fruit.height * e.size.y,
50 0, 0, 0, 1, e.subImage)));
51 }
52
53 createTween (context, currentRequest)
54 {
55 new TWEEN.Tween(context.position)
56 .to({ x: context.targetPosition.x, y: context.targetPosition.y }, context.time)
57 .easing(TWEEN.Easing.Quadratic.In)
58 .start()
59 .onStart(() => { currentRequest.onStart && currentRequest.onStart.call(this); })
60 .onComplete(() => { context.done = true, this.checkArrayStatus(currentRequest), currentRequest.onDone && currentRequest.onDone.call(this); });
61
62 new TWEEN.Tween(context.size)
63 .to({ x: context.targetSize.x, y: context.targetSize.y }, context.time)
64 .easing(TWEEN.Easing.Quadratic.In)
65 .start();
66 }
67
68 checkArrayStatus (currentRequest)
69 {
70 let areAllDone = currentRequest.data.every(e => e.done);
71 if (areAllDone)
72 {
73 currentRequest.done = true;
74 }
75 }
76 }
@@ -0,0 +1,76 @@
1class SimpleImagesRenderTween extends GameObject
2{
3 static Instance;
4
5 targetPosition = null;
6 targetSize = null;
7
8 imageDataPerRequest = []; //[{position, size, targetPosition, targetSize,image, subimage, time}]
9
10 time = 400;
11
12 constructor (x, y, sprite, layer)
13 {
14 super(x, y, sprite, layer);
15
16 SimpleImagesRenderTween.Instance = this;
17 }
18
19 add (arr, onStart, onDone)
20 {
21 this.imageDataPerRequest.push({ data: arr, onStart: onStart, onDone: onDone });
22 let current = this.imageDataPerRequest[this.imageDataPerRequest.length - 1];
23 arr.forEach(e => this.createTween(e, current));
24 }
25
26 update (deltaTime)
27 {
28 super.update(deltaTime);
29 this.clean();
30 }
31
32 draw (context)
33 {
34 super.draw(context);
35 this.drawImages();
36 }
37
38 clean ()
39 {
40 this.imageDataPerRequest = this.imageDataPerRequest.filter(e => !e.done);
41 }
42
43 drawImages ()
44 {
45 let context = this;
46 this.imageDataPerRequest.forEach(arr => arr.data.forEach(e =>
47 context.scene.game.images.fruit.draw(context.scene,
48 e.position.x, e.position.y,
49 e.image.width * e.size.x, game.images.fruit.height * e.size.y,
50 0, 0, 0, 1, e.subImage)));
51 }
52
53 createTween (context, currentRequest)
54 {
55 new TWEEN.Tween(context.position)
56 .to({ x: context.targetPosition.x, y: context.targetPosition.y }, context.time)
57 .easing(TWEEN.Easing.Quadratic.In)
58 .start()
59 .onStart(() => { currentRequest.onStart && currentRequest.onStart.call(this); })
60 .onComplete(() => { context.done = true, this.checkArrayStatus(currentRequest), currentRequest.onDone && currentRequest.onDone.call(this); });
61
62 new TWEEN.Tween(context.size)
63 .to({ x: context.targetSize.x, y: context.targetSize.y }, context.time)
64 .easing(TWEEN.Easing.Quadratic.In)
65 .start();
66 }
67
68 checkArrayStatus (currentRequest)
69 {
70 let areAllDone = currentRequest.data.every(e => e.done);
71 if (areAllDone)
72 {
73 currentRequest.done = true;
74 }
75 }
76}
src/objects/menus/btnFruit.js +1 −1
@@ -4,7 +4,7 @@ class BtnFruit extends BaseMenuButton
4 4
5 5 addedToScene ()
6 6 {
7 this.fruitInventory = this.scene.addObject(new FruitInventory(352, -500, this.scene.game.images.fruitInventory));
7 this.fruitInventory = this.scene.addObject(new FruitInventory(352, -500, this.scene.game.images.fruitInventory, 1));
8 8 }
9 9
10 10 onClick (btns)
@@ -4,7 +4,7 @@ class BtnFruit extends BaseMenuButton
4
4
5 addedToScene ()
5 addedToScene ()
6 {
6 {
7 this.fruitInventory = this.scene.addObject(new FruitInventory(352, -500, this.scene.game.images.fruitInventory));
7 this.fruitInventory = this.scene.addObject(new FruitInventory(352, -500, this.scene.game.images.fruitInventory, 1));
8 }
8 }
9
9
10 onClick (btns)
10 onClick (btns)
src/objects/menus/fruitInventory.js +1 −4
@@ -171,10 +171,9 @@ class FruitInventory extends GameObject
171 171
172 172 for (let i = 0; i < amount; i++)
173 173 {
174 this.fruitInventoryButtons.push(this.scene.addObject(new FruitInventoryButton(-50, -50, this.scene.game.images.fruit, 0, t)));
174 this.fruitInventoryButtons.push(this.scene.addObject(new FruitInventoryButton(-50, -50, this.scene.game.images.fruit, 2, t)));
175 175 }
176 176 }
177 console.log("creating inv");
178 177
179 178 }
180 179
@@ -184,8 +183,6 @@ class FruitInventory extends GameObject
184 183 this.fruitInventoryButtons[i].destroy();
185 184
186 185 this.fruitInventoryButtons = [];
187
188 console.log("cleared inv");
189 186 }
190 187
191 188 stopActiveTween (tween)
@@ -171,10 +171,9 @@ class FruitInventory extends GameObject
171
171
172 for (let i = 0; i < amount; i++)
172 for (let i = 0; i < amount; i++)
173 {
173 {
174 this.fruitInventoryButtons.push(this.scene.addObject(new FruitInventoryButton(-50, -50, this.scene.game.images.fruit, 0, t)));
174 this.fruitInventoryButtons.push(this.scene.addObject(new FruitInventoryButton(-50, -50, this.scene.game.images.fruit, 2, t)));
175 }
175 }
176 }
176 }
177 console.log("creating inv");
178
177
179 }
178 }
180
179
@@ -184,8 +183,6 @@ class FruitInventory extends GameObject
184 this.fruitInventoryButtons[i].destroy();
183 this.fruitInventoryButtons[i].destroy();
185
184
186 this.fruitInventoryButtons = [];
185 this.fruitInventoryButtons = [];
187
188 console.log("cleared inv");
189 }
186 }
190
187
191 stopActiveTween (tween)
188 stopActiveTween (tween)