BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / simpleImagesRenderTween.js

simpleImagesRenderTween.js

simpleImagesRenderTween.js 2.5 KiB Download Raw
1class SimpleImagesRenderTween extends GameObject
2{
3 static Instance;
4
5 targetPosition = null;
6 targetSize = null;
7
8 imageDataPerRequest = [];
9 //[
10 // {
11 // data: {position, size, targetPosition, targetSize,image, subimage, time},
12 // onStart: val ,
13 // onDone: val
14 // }
15 //]
16
17 tween = {
18 default: (cont, req) => this.createTween(cont, req)
19 };
20
21 constructor (x, y, sprite, layer)
22 {
23 super(x, y, sprite, layer);
24
25 SimpleImagesRenderTween.Instance = this;
26 }
27
28 add (arr, onStart, onDone, preset = "default")
29 {
30 this.imageDataPerRequest.push({ data: arr, onStart: onStart, onDone: onDone });
31 let current = this.imageDataPerRequest[this.imageDataPerRequest.length - 1];
32 arr.forEach(e => this.tween[preset](e, current));
33 }
34
35 update (deltaTime)
36 {
37 super.update(deltaTime);
38 this.clean();
39 }
40
41 draw (context)
42 {
43 super.draw(context);
44 this.drawImages();
45 }
46
47 clean ()
48 {
49 this.imageDataPerRequest = this.imageDataPerRequest.filter(e => !e.done);
50 }
51
52 drawImages ()
53 {
54 let context = this;
55 this.imageDataPerRequest.forEach(arr => arr.data.forEach(e =>
56 e.image.draw(
57 context.scene,
58 e.position.x, e.position.y,
59 e.image.width * e.size.x, game.images.fruit.height * e.size.y,
60 0, 0, 0, 1, e.subImage
61 )
62 ));
63 }
64
65 createTween (context, currentRequest)
66 {
67 if (context.targetPosition)
68 {
69 new TWEEN.Tween(context.position)
70 .to({ x: context.targetPosition.x, y: context.targetPosition.y }, context.time)
71 .easing(TWEEN.Easing.Quadratic.In)
72 .start()
73 .onStart(() => { currentRequest.onStart && currentRequest.onStart.call(this); })
74 .onComplete(() => { context.done = true, this.checkArrayStatus(currentRequest), currentRequest.onDone && currentRequest.onDone.call(this); });
75 }
76 if (context.targetSize)
77 {
78 new TWEEN.Tween(context.size)
79 .to({ x: context.targetSize.x, y: context.targetSize.y }, context.time)
80 .easing(TWEEN.Easing.Quadratic.In)
81 .start();
82 }
83 }
84
85 checkArrayStatus (currentRequest)
86 {
87 let areAllDone = currentRequest.data.every(e => e.done);
88 if (areAllDone)
89 {
90 currentRequest.done = true;
91 }
92 }
93}