BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / simpleImagesRenderTween.js

simpleImagesRenderTween.js

tween presets?
timfa wsadhelios · pushed by timfa · 4 years ago
2d3f3a6c History
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 context.scene.game.images.fruit.draw(context.scene,
57 e.position.x, e.position.y,
58 e.image.width * e.size.x, game.images.fruit.height * e.size.y,
59 0, 0, 0, 1, e.subImage)));
60 }
61
62 createTween (context, currentRequest)
63 {
64 if (context.targetPosition)
65 {
66 new TWEEN.Tween(context.position)
67 .to({ x: context.targetPosition.x, y: context.targetPosition.y }, context.time)
68 .easing(TWEEN.Easing.Quadratic.In)
69 .start()
70 .onStart(() => { currentRequest.onStart && currentRequest.onStart.call(this); })
71 .onComplete(() => { context.done = true, this.checkArrayStatus(currentRequest), currentRequest.onDone && currentRequest.onDone.call(this); });
72 }
73 if (context.targetSize)
74 {
75 new TWEEN.Tween(context.size)
76 .to({ x: context.targetSize.x, y: context.targetSize.y }, context.time)
77 .easing(TWEEN.Easing.Quadratic.In)
78 .start();
79 }
80 }
81
82 checkArrayStatus (currentRequest)
83 {
84 let areAllDone = currentRequest.data.every(e => e.done);
85 if (areAllDone)
86 {
87 currentRequest.done = true;
88 }
89 }
90}