BlueGit

blue-star speeltuin

btn reactiveness : stop active tween when new one is started

timfa wsadhelios · pushed by timfa

Authored 2022-10-22 · 4 years ago

Hash
e8b1c27b171b3c1eb7dc0c55197956fec37ba116
Parent
4e3f8c90
2 files changed +75 −88
src/objects/menus/btnGames.js +44 −28
@@ -3,7 +3,7 @@ class BtnGames extends GameObject
3 3 buttons = [];
4 4 buttonPadding = 10;
5 5 state = 0;
6 prevState = 0;
6 prevState = 1;
7 7
8 8 subBtnStartPosX = -32;
9 9
@@ -20,7 +20,7 @@ class BtnGames extends GameObject
20 20
21 21 getSubButton (label)
22 22 {
23 return this.buttons.filter(e => e.label == label)[0];
23 return this.buttons.filter(e => e.label == label)[0].button;
24 24 }
25 25
26 26 initializeButtons ()
@@ -63,49 +63,65 @@ class BtnGames extends GameObject
63 63
64 64 onClick (btn)
65 65 {
66 this.state == MenuManager.States.closed ?
67 this.moveToOpen() :
68 this.moveToClose();
66 this.prevState == MenuManager.States.closed ?
67 this.moveToClose() :
68 this.moveToOpen();
69 69 }
70 70
71 71 moveToOpen (onDone)
72 72 {
73 73 if (this.state == MenuManager.States.open)
74 {
74 75 return;
75
76 this.state = MenuManager.States.open;
77 this.buttons.forEach(e => this.createOpenTween(e.button.transform.position, { x: [e.button.endPos.x], y: [e.button.startPos.y, e.button.startPos.y, e.button.startPos.y, e.button.endPos.y] }, 500).start().onComplete(() => onDone && onDone.call()));
76 }
77 this.prevState = MenuManager.States.closed;
78 this.state = MenuManager.States.moving;
79 this.buttons.forEach(e => this.createOpenTween(e.button, onDone));
78 80 }
79 81
80 82 moveToClose (onDone)
81 83 {
82 if (this.state == MenuManager.States.closed)
84 if (this.state == MenuManager.States.closed || this.state == MenuManager.States.moving && this.prevState == MenuManager.States.open)
85 {
83 86 return;
84 this.state = MenuManager.States.closed;
85 this.buttons.forEach(e => this.createCloseTween(e.button.transform.position, { x: [e.button.endPos.x, e.button.endPos.x, e.button.endPos.x, e.button.endPos.x, e.button.startPos.x], y: [e.button.startPos.y] }, 1000).start().onComplete(() => onDone && onDone.call()));
87 }
88 this.prevState = MenuManager.States.open;
89 this.state = MenuManager.States.moving;
90 this.buttons.forEach(e => this.createCloseTween(e.button, onDone));
86 91 }
87 92
88 createOpenTween (target, newTarget, length)
93 createOpenTween (button, onDone)
89 94 {
90 return new TWEEN.Tween(target)
91 .to(newTarget, length)
92 .onUpdate(() =>
93 {
94 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
95 })
96 .easing(TWEEN.Easing.Back.Out);
95 let context = this;
96 let tween = new TWEEN.Tween(button.transform.position)
97 .to({ x: [button.endPos.x], y: [button.startPos.y, button.startPos.y, button.startPos.y, button.endPos.y] }, 500)
98 .onUpdate(() => button.onUpdate && e.button.onUpdate())
99 .easing(TWEEN.Easing.Back.Out)
100 .onComplete(() => { this.state = MenuManager.States.open; button.activeTween = null; onDone && onDone.call(context); })
101 .start();
102
103 return tween.onStart(() => { this.stopActiveTween(button, tween); });
97 104 }
98 105
99 createCloseTween (target, newTarget, length)
106 createCloseTween (button, onDone)
100 107 {
101 return new TWEEN.Tween(target)
102 .to(newTarget, length)
103 .onUpdate(() =>
104 {
105 this.updateButtonPosition && this.updateButtonPosition();
106 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
107 })
108 .easing(TWEEN.Easing.Quintic.Out);
108 let tween = new TWEEN.Tween(button.transform.position)
109 .to({ x: [button.endPos.x, button.endPos.x, button.endPos.x, button.endPos.x, button.startPos.x], y: [button.startPos.y] }, 1000)
110 .onUpdate(() => button.onUpdate && e.button.onUpdate())
111 .easing(TWEEN.Easing.Quintic.Out)
112 .onComplete(() => { this.state = MenuManager.States.closed; button.activeTween = null; onDone && onDone.call(context); })
113 .start();
114
115 return tween.onStart(() => { this.stopActiveTween(button, tween); });
116 }
117
118 stopActiveTween (button, newTween)
119 {
120 if (button.activeTween)
121 {
122 button.activeTween.stop();
123 }
124 button.activeTween = newTween;
109 125 }
110 126
111 127 update (deltaTime)
@@ -3,7 +3,7 @@ class BtnGames extends GameObject
3 buttons = [];
3 buttons = [];
4 buttonPadding = 10;
4 buttonPadding = 10;
5 state = 0;
5 state = 0;
6 prevState = 0;
6 prevState = 1;
7
7
8 subBtnStartPosX = -32;
8 subBtnStartPosX = -32;
9
9
@@ -20,7 +20,7 @@ class BtnGames extends GameObject
20
20
21 getSubButton (label)
21 getSubButton (label)
22 {
22 {
23 return this.buttons.filter(e => e.label == label)[0];
23 return this.buttons.filter(e => e.label == label)[0].button;
24 }
24 }
25
25
26 initializeButtons ()
26 initializeButtons ()
@@ -63,49 +63,65 @@ class BtnGames extends GameObject
63
63
64 onClick (btn)
64 onClick (btn)
65 {
65 {
66 this.state == MenuManager.States.closed ?
66 this.prevState == MenuManager.States.closed ?
67 this.moveToOpen() :
67 this.moveToClose() :
68 this.moveToClose();
68 this.moveToOpen();
69 }
69 }
70
70
71 moveToOpen (onDone)
71 moveToOpen (onDone)
72 {
72 {
73 if (this.state == MenuManager.States.open)
73 if (this.state == MenuManager.States.open)
74 {
74 return;
75 return;
75
76 }
76 this.state = MenuManager.States.open;
77 this.prevState = MenuManager.States.closed;
77 this.buttons.forEach(e => this.createOpenTween(e.button.transform.position, { x: [e.button.endPos.x], y: [e.button.startPos.y, e.button.startPos.y, e.button.startPos.y, e.button.endPos.y] }, 500).start().onComplete(() => onDone && onDone.call()));
78 this.state = MenuManager.States.moving;
79 this.buttons.forEach(e => this.createOpenTween(e.button, onDone));
78 }
80 }
79
81
80 moveToClose (onDone)
82 moveToClose (onDone)
81 {
83 {
82 if (this.state == MenuManager.States.closed)
84 if (this.state == MenuManager.States.closed || this.state == MenuManager.States.moving && this.prevState == MenuManager.States.open)
85 {
83 return;
86 return;
84 this.state = MenuManager.States.closed;
87 }
85 this.buttons.forEach(e => this.createCloseTween(e.button.transform.position, { x: [e.button.endPos.x, e.button.endPos.x, e.button.endPos.x, e.button.endPos.x, e.button.startPos.x], y: [e.button.startPos.y] }, 1000).start().onComplete(() => onDone && onDone.call()));
88 this.prevState = MenuManager.States.open;
89 this.state = MenuManager.States.moving;
90 this.buttons.forEach(e => this.createCloseTween(e.button, onDone));
86 }
91 }
87
92
88 createOpenTween (target, newTarget, length)
93 createOpenTween (button, onDone)
89 {
94 {
90 return new TWEEN.Tween(target)
95 let context = this;
91 .to(newTarget, length)
96 let tween = new TWEEN.Tween(button.transform.position)
92 .onUpdate(() =>
97 .to({ x: [button.endPos.x], y: [button.startPos.y, button.startPos.y, button.startPos.y, button.endPos.y] }, 500)
93 {
98 .onUpdate(() => button.onUpdate && e.button.onUpdate())
94 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
99 .easing(TWEEN.Easing.Back.Out)
95 })
100 .onComplete(() => { this.state = MenuManager.States.open; button.activeTween = null; onDone && onDone.call(context); })
96 .easing(TWEEN.Easing.Back.Out);
101 .start();
102
103 return tween.onStart(() => { this.stopActiveTween(button, tween); });
97 }
104 }
98
105
99 createCloseTween (target, newTarget, length)
106 createCloseTween (button, onDone)
100 {
107 {
101 return new TWEEN.Tween(target)
108 let tween = new TWEEN.Tween(button.transform.position)
102 .to(newTarget, length)
109 .to({ x: [button.endPos.x, button.endPos.x, button.endPos.x, button.endPos.x, button.startPos.x], y: [button.startPos.y] }, 1000)
103 .onUpdate(() =>
110 .onUpdate(() => button.onUpdate && e.button.onUpdate())
104 {
111 .easing(TWEEN.Easing.Quintic.Out)
105 this.updateButtonPosition && this.updateButtonPosition();
112 .onComplete(() => { this.state = MenuManager.States.closed; button.activeTween = null; onDone && onDone.call(context); })
106 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
113 .start();
107 })
114
108 .easing(TWEEN.Easing.Quintic.Out);
115 return tween.onStart(() => { this.stopActiveTween(button, tween); });
116 }
117
118 stopActiveTween (button, newTween)
119 {
120 if (button.activeTween)
121 {
122 button.activeTween.stop();
123 }
124 button.activeTween = newTween;
109 }
125 }
110
126
111 update (deltaTime)
127 update (deltaTime)
src/objects/menus/menuManager.js +31 −60
@@ -8,27 +8,24 @@ class MenuManager extends GameObject
8 8 state = 0;
9 9 prevState = 0;
10 10
11 openTween = null;
11 openTargetX = 60;
12 12 openTweenTime = 800;
13 13
14 closeTween = null;
14 closedTargetX = -15;
15 15 closeTweenTime = 1400;
16 16
17 tweenTimer = 0;
18 timeDiffPercent = 1;
19 17 isOpened = null;
20 18 isClosed = null;
21 19 isLoaded = null;
22 20
21 activeTween = null;
22
23 23 static States = { closed: 0, open: 1, moving: 2 };
24 24
25 25 constructor (x, y, sprite, layer)
26 26 {
27 27 super(x, y, sprite, layer);
28 28 MenuManager.Instance = this;
29
30 this.openTween = this.createOpenTween(this.transform.position, { x: 60 }, this.openTweenTime);
31 this.closeTween = this.createCloseTween(this.transform.position, { x: -15 }, this.closeTweenTime);
32 29 }
33 30
34 31 getSubButton (label)
@@ -68,17 +65,9 @@ class MenuManager extends GameObject
68 65 this.isOpened.dispatch();
69 66 this.prevState = MenuManager.States.closed;
70 67
71 if (this.state == MenuManager.States.moving)
72 this.closeTween.stop();
73
74 this.openTween.to({ x: 60 }, this.openTweenTime * this.timeDiffPercent);
75
76 68 this.state = MenuManager.States.moving;
77 69
78 let context = this;
79 this.openTween
80 .onComplete(() => { this.state = MenuManager.States.open; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); })
81 .start();
70 this.createOpenTween(onDone);
82 71 }
83 72
84 73 moveToClose (onDone)
@@ -86,52 +75,52 @@ class MenuManager extends GameObject
86 75 this.isClosed.dispatch();
87 76 this.prevState = MenuManager.States.open;
88 77
89 if (this.state == MenuManager.States.moving)
90 this.openTween.stop();
91
92 this.closeTween.to({ x: -15 }, this.closeTweenTime * this.timeDiffPercent);
93
94 78 this.state = MenuManager.States.moving;
95 79
96 let context = this;
97 this.closeTween
98 .onComplete(() => { this.state = MenuManager.States.closed; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); })
99 .start();
80 this.createCloseTween(onDone);
100 81 }
101 82
102 createOpenTween (target, newTarget, length)
83 createOpenTween (onDone)
103 84 {
104 return new TWEEN.Tween(target)
105 .to(newTarget, length)
106 .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.openTweenTime); this.tweenTimer = 0; })
85 let context = this;
86 let tween = new TWEEN.Tween(this.transform.position)
87 .to({ x: this.openTargetX }, this.openTweenTime)
107 88 .onUpdate(() =>
108 89 {
109 90 this.updateButtonPosition();
110 91 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
111 92 })
112 .easing(TWEEN.Easing.Bounce.Out);
93 .easing(TWEEN.Easing.Bounce.Out)
94 .onComplete(() => { this.state = MenuManager.States.open; this.activeTween = null; onDone && onDone.call(context); })
95 .start();
96
97 return tween.onStart(() => this.stopActiveTween(tween));
113 98 }
114 99
115 createCloseTween (target, newTarget, length)
100 createCloseTween (onDone)
116 101 {
117 return new TWEEN.Tween(target)
118 .to(newTarget, length)
119 .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.closeTweenTime); this.tweenTimer = 0; })
102 let context = this;
103 let tween = new TWEEN.Tween(this.transform.position)
104 .to({ x: this.closedTargetX }, this.closeTweenTime)
120 105 .onUpdate(() =>
121 106 {
122 107 this.updateButtonPosition();
108 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
123 109 })
124 .easing(TWEEN.Easing.Elastic.Out);
110 .easing(TWEEN.Easing.Elastic.Out)
111 .onComplete(() => { this.state = MenuManager.States.closed; this.activeTween = null; onDone && onDone.call(context); })
112 .start();
113
114 return tween.onStart(() => this.stopActiveTween(tween));
125 115 }
126 116
127 calculateTimeLeftPercent (expectedTimeLength)
117 stopActiveTween (tween)
128 118 {
129 let t = Math.round(this.tweenTimer * 1000);
130 if (t == 0)
131 return 1;
132 else if (t < expectedTimeLength)
133 return Math.max(t / expectedTimeLength, .5);
134 return 1;
119 if (this.activeTween)
120 {
121 this.activeTween.stop();
122 }
123 this.activeTween = tween;
135 124 }
136 125
137 126 updateButtonPosition ()
@@ -150,22 +139,4 @@ class MenuManager extends GameObject
150 139 }
151 140 }
152 141 }
153
154 update (deltaTime)
155 {
156 super.update(deltaTime);
157
158 if (this.state == MenuManager.States.moving)
159 {
160 this.tweenTimer += deltaTime;
161 }
162 else
163 {
164 if (this.tweenTimer > 0)
165 {
166 this.tweenTimer = 0;
167 this.timeDiffPercent = 1;
168 }
169 }
170 }
171 142 }
@@ -8,27 +8,24 @@ class MenuManager extends GameObject
8 state = 0;
8 state = 0;
9 prevState = 0;
9 prevState = 0;
10
10
11 openTween = null;
11 openTargetX = 60;
12 openTweenTime = 800;
12 openTweenTime = 800;
13
13
14 closeTween = null;
14 closedTargetX = -15;
15 closeTweenTime = 1400;
15 closeTweenTime = 1400;
16
16
17 tweenTimer = 0;
18 timeDiffPercent = 1;
19 isOpened = null;
17 isOpened = null;
20 isClosed = null;
18 isClosed = null;
21 isLoaded = null;
19 isLoaded = null;
22
20
21 activeTween = null;
22
23 static States = { closed: 0, open: 1, moving: 2 };
23 static States = { closed: 0, open: 1, moving: 2 };
24
24
25 constructor (x, y, sprite, layer)
25 constructor (x, y, sprite, layer)
26 {
26 {
27 super(x, y, sprite, layer);
27 super(x, y, sprite, layer);
28 MenuManager.Instance = this;
28 MenuManager.Instance = this;
29
30 this.openTween = this.createOpenTween(this.transform.position, { x: 60 }, this.openTweenTime);
31 this.closeTween = this.createCloseTween(this.transform.position, { x: -15 }, this.closeTweenTime);
32 }
29 }
33
30
34 getSubButton (label)
31 getSubButton (label)
@@ -68,17 +65,9 @@ class MenuManager extends GameObject
68 this.isOpened.dispatch();
65 this.isOpened.dispatch();
69 this.prevState = MenuManager.States.closed;
66 this.prevState = MenuManager.States.closed;
70
67
71 if (this.state == MenuManager.States.moving)
72 this.closeTween.stop();
73
74 this.openTween.to({ x: 60 }, this.openTweenTime * this.timeDiffPercent);
75
76 this.state = MenuManager.States.moving;
68 this.state = MenuManager.States.moving;
77
69
78 let context = this;
70 this.createOpenTween(onDone);
79 this.openTween
80 .onComplete(() => { this.state = MenuManager.States.open; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); })
81 .start();
82 }
71 }
83
72
84 moveToClose (onDone)
73 moveToClose (onDone)
@@ -86,52 +75,52 @@ class MenuManager extends GameObject
86 this.isClosed.dispatch();
75 this.isClosed.dispatch();
87 this.prevState = MenuManager.States.open;
76 this.prevState = MenuManager.States.open;
88
77
89 if (this.state == MenuManager.States.moving)
90 this.openTween.stop();
91
92 this.closeTween.to({ x: -15 }, this.closeTweenTime * this.timeDiffPercent);
93
94 this.state = MenuManager.States.moving;
78 this.state = MenuManager.States.moving;
95
79
96 let context = this;
80 this.createCloseTween(onDone);
97 this.closeTween
98 .onComplete(() => { this.state = MenuManager.States.closed; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); })
99 .start();
100 }
81 }
101
82
102 createOpenTween (target, newTarget, length)
83 createOpenTween (onDone)
103 {
84 {
104 return new TWEEN.Tween(target)
85 let context = this;
105 .to(newTarget, length)
86 let tween = new TWEEN.Tween(this.transform.position)
106 .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.openTweenTime); this.tweenTimer = 0; })
87 .to({ x: this.openTargetX }, this.openTweenTime)
107 .onUpdate(() =>
88 .onUpdate(() =>
108 {
89 {
109 this.updateButtonPosition();
90 this.updateButtonPosition();
110 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
91 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
111 })
92 })
112 .easing(TWEEN.Easing.Bounce.Out);
93 .easing(TWEEN.Easing.Bounce.Out)
94 .onComplete(() => { this.state = MenuManager.States.open; this.activeTween = null; onDone && onDone.call(context); })
95 .start();
96
97 return tween.onStart(() => this.stopActiveTween(tween));
113 }
98 }
114
99
115 createCloseTween (target, newTarget, length)
100 createCloseTween (onDone)
116 {
101 {
117 return new TWEEN.Tween(target)
102 let context = this;
118 .to(newTarget, length)
103 let tween = new TWEEN.Tween(this.transform.position)
119 .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.closeTweenTime); this.tweenTimer = 0; })
104 .to({ x: this.closedTargetX }, this.closeTweenTime)
120 .onUpdate(() =>
105 .onUpdate(() =>
121 {
106 {
122 this.updateButtonPosition();
107 this.updateButtonPosition();
108 this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate());
123 })
109 })
124 .easing(TWEEN.Easing.Elastic.Out);
110 .easing(TWEEN.Easing.Elastic.Out)
111 .onComplete(() => { this.state = MenuManager.States.closed; this.activeTween = null; onDone && onDone.call(context); })
112 .start();
113
114 return tween.onStart(() => this.stopActiveTween(tween));
125 }
115 }
126
116
127 calculateTimeLeftPercent (expectedTimeLength)
117 stopActiveTween (tween)
128 {
118 {
129 let t = Math.round(this.tweenTimer * 1000);
119 if (this.activeTween)
130 if (t == 0)
120 {
131 return 1;
121 this.activeTween.stop();
132 else if (t < expectedTimeLength)
122 }
133 return Math.max(t / expectedTimeLength, .5);
123 this.activeTween = tween;
134 return 1;
135 }
124 }
136
125
137 updateButtonPosition ()
126 updateButtonPosition ()
@@ -150,22 +139,4 @@ class MenuManager extends GameObject
150 }
139 }
151 }
140 }
152 }
141 }
153
154 update (deltaTime)
155 {
156 super.update(deltaTime);
157
158 if (this.state == MenuManager.States.moving)
159 {
160 this.tweenTimer += deltaTime;
161 }
162 else
163 {
164 if (this.tweenTimer > 0)
165 {
166 this.tweenTimer = 0;
167 this.timeDiffPercent = 1;
168 }
169 }
170 }
171}
142}