BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / 12-baseMenuButton.js

12-baseMenuButton.js

good luck
timfa wsadhelios · pushed by timfa · 4 years ago
f1aab4e6 History
12-baseMenuButton.js 1.8 KiB Download Raw
1class BaseMenuButton extends GameObject
2{
3 targetPosition = null;
4 originPosition = null;
5
6 parent = null;
7
8 activeTween = null;
9
10 constructor (sx, sy, ex, ey, sprite, layer)
11 {
12 super(sx, sy, sprite, layer);
13 this.originPosition = new vector(sx, sy);
14 this.targetPosition = new vector(ex, ey);
15 }
16
17 moveOnParentOpen ()
18 {
19 this.parent.isOpened.add((a, b) => this.moveToActiveState(a, b), this);
20 }
21
22 moveOnParentClose ()
23 {
24 this.parent.isClosed.add((a, b) => this.moveToInactiveState(a, b), this);
25 }
26
27 moveToActiveState (time, onDone)
28 {
29 return this.createActiveStateTween(time, onDone, this).start();
30 }
31
32 moveToInactiveState (time, onDone)
33 {
34 return this.createInactiveStateTween(time, onDone, this).start();
35 }
36
37 createActiveStateTween (time, onDone, context)
38 {
39 console.log(context);
40 let tween = new TWEEN.Tween(context.transform.position)
41 .to({ x: context.targetPosition.x, y: context.targetPosition.y }, time)
42 .easing(TWEEN.Easing.Bounce.Out)
43 .onComplete(() => { context.activeTween = null; onDone && onDone.call(context); });
44 return tween.onStart(() => context.stopActiveTween(tween));
45 }
46
47 createInactiveStateTween (time, onDone, context)
48 {
49 let tween = new TWEEN.Tween(context.transform.position)
50 .to({ x: context.originPosition.x, y: context.originPosition.y }, time)
51 .easing(TWEEN.Easing.Elastic.Out)
52 .onComplete(() => { context.activeTween = null; onDone && onDone.call(context); });
53 return tween.onStart(() => context.stopActiveTween(tween));
54 }
55
56 stopActiveTween (tween)
57 {
58 if (this.activeTween)
59 {
60 this.activeTween.stop();
61 }
62 this.activeTween = tween;
63 }
64}