BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / simpleImagesRenderTween.js

simpleImagesRenderTween.js

simpleImagesRenderTween.js 2.1 KiB Download Raw
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}