some cool shtuff
timfa wsadhelios · pushed by timfa
Authored 2022-10-24 · 4 years ago
- Hash
ed656e167937f6fe26ecd3aaf34563e77e3615da- Parent
9733eb94
3 files changed
+73
−1
@@ -1,6 +1,6 @@
1
1
class BaseMenu extends BaseMenuButton
2
2
{
3
children = []; // {label button}
3
children = []; // {label, button}
4
4
5
5
state = 0;
6
6
prevState = null;
@@ -1,6 +1,6 @@1
class BaseMenu extends BaseMenuButton1
class BaseMenu extends BaseMenuButton2
{2
{3
children = []; // {label button}3
children = []; // {label, button}4
4
5
state = 0;5
state = 0;6
prevState = null;6
prevState = null;@@ -7,6 +7,8 @@ class BaseMenuButton extends GameObject
7
7
8
8
activeTween = null;
9
9
10
shouldPeekOnHover = true;
11
10
12
constructor (sx, sy, ex, ey, sprite, layer)
11
13
{
12
14
super(sx, sy, sprite, layer);
@@ -14,6 +16,37 @@ class BaseMenuButton extends GameObject
14
16
this.targetPosition = new vector(ex, ey);
15
17
}
16
18
19
peekOnHover ()
20
{
21
if (!GameInput.mousePosition)
22
return;
23
if (this.collider.isInBounds(GameInput.mousePosition))
24
{
25
if (this.activeTween == null && (this.transform.position.x == this.targetPosition.x && this.transform.position.y == this.targetPosition.y))
26
{
27
this.createHoverTween(400, null, this).easing(TWEEN.Easing.Elastic.Out).start();
28
}
29
}
30
else
31
{
32
if (this.activeTween == null &&
33
(this.transform.position.x != this.targetPosition.x || this.transform.position.y != this.targetPosition.y) &&
34
this.parent && this.parent.state == MenuStates.open)
35
{
36
this.createActiveStateTween(400, null, this).start();
37
}
38
}
39
}
40
41
update (deltaTime)
42
{
43
super.update(deltaTime);
44
if (this.shouldPeekOnHover)
45
{
46
this.peekOnHover();
47
}
48
}
49
17
50
moveOnParentOpen ()
18
51
{
19
52
this.parent.isOpened.add((a, b) => this.moveToActiveState(a, b), this);
@@ -52,6 +85,15 @@ class BaseMenuButton extends GameObject
52
85
return tween.onStart(() => context.stopActiveTween(tween));
53
86
}
54
87
88
createHoverTween (time, onDone, context)
89
{
90
let tween = new TWEEN.Tween(context.transform.position)
91
.to({ x: context.targetPosition.x + 10, y: context.targetPosition.y }, time)
92
.easing(TWEEN.Easing.Elastic.In)
93
.onComplete(() => { context.activeTween = null; onDone && onDone.call(context); });
94
return tween.onStart(() => context.stopActiveTween(tween));
95
}
96
55
97
stopActiveTween (tween)
56
98
{
57
99
if (this.activeTween)
@@ -7,6 +7,8 @@ class BaseMenuButton extends GameObject7
7
8
activeTween = null;8
activeTween = null;9
9
10
shouldPeekOnHover = true;11
10
constructor (sx, sy, ex, ey, sprite, layer)12
constructor (sx, sy, ex, ey, sprite, layer)11
{13
{12
super(sx, sy, sprite, layer);14
super(sx, sy, sprite, layer);@@ -14,6 +16,37 @@ class BaseMenuButton extends GameObject14
this.targetPosition = new vector(ex, ey);16
this.targetPosition = new vector(ex, ey);15
}17
}16
18
19
peekOnHover ()20
{21
if (!GameInput.mousePosition)22
return;23
if (this.collider.isInBounds(GameInput.mousePosition))24
{25
if (this.activeTween == null && (this.transform.position.x == this.targetPosition.x && this.transform.position.y == this.targetPosition.y))26
{27
this.createHoverTween(400, null, this).easing(TWEEN.Easing.Elastic.Out).start();28
}29
}30
else31
{32
if (this.activeTween == null &&33
(this.transform.position.x != this.targetPosition.x || this.transform.position.y != this.targetPosition.y) &&34
this.parent && this.parent.state == MenuStates.open)35
{36
this.createActiveStateTween(400, null, this).start();37
}38
}39
}40
41
update (deltaTime)42
{43
super.update(deltaTime);44
if (this.shouldPeekOnHover)45
{46
this.peekOnHover();47
}48
}49
17
moveOnParentOpen ()50
moveOnParentOpen ()18
{51
{19
this.parent.isOpened.add((a, b) => this.moveToActiveState(a, b), this);52
this.parent.isOpened.add((a, b) => this.moveToActiveState(a, b), this);@@ -52,6 +85,15 @@ class BaseMenuButton extends GameObject52
return tween.onStart(() => context.stopActiveTween(tween));85
return tween.onStart(() => context.stopActiveTween(tween));53
}86
}54
87
88
createHoverTween (time, onDone, context)89
{90
let tween = new TWEEN.Tween(context.transform.position)91
.to({ x: context.targetPosition.x + 10, y: context.targetPosition.y }, time)92
.easing(TWEEN.Easing.Elastic.In)93
.onComplete(() => { context.activeTween = null; onDone && onDone.call(context); });94
return tween.onStart(() => context.stopActiveTween(tween));95
}96
55
stopActiveTween (tween)97
stopActiveTween (tween)56
{98
{57
if (this.activeTween) 99
if (this.activeTween) @@ -57,4 +57,34 @@ class MainMenu extends BaseMenu
57
57
58
58
}
59
59
}
60
61
//override for peekOnHover during closed state
62
peekOnHover ()
63
{
64
if (!GameInput.mousePosition)
65
return;
66
if (this.collider.isInBounds(GameInput.mousePosition))
67
{
68
if (this.activeTween == null && (this.transform.position.x == this.originPosition.x && this.transform.position.y == this.originPosition.y))
69
{
70
this.createHoverTween(400, null, this).easing(TWEEN.Easing.Elastic.Out).start();
71
}
72
}
73
else
74
{
75
if (this.activeTween == null &&
76
(this.transform.position.x != this.originPosition.x || this.transform.position.y != this.originPosition.y) &&
77
this.state == MenuStates.closed)
78
{
79
this.createInactiveStateTween(400, null, this).start();
80
}
81
}
82
}
83
84
//override for small opening from closed state
85
createHoverTween (time, onDone, context)
86
{
87
return super.createHoverTween(time, onDone, context)
88
.to({ x: this.originPosition.x + 10 }, time);
89
}
60
90
}
@@ -57,4 +57,34 @@ class MainMenu extends BaseMenu57
57
58
}58
}59
}59
}60
61
//override for peekOnHover during closed state62
peekOnHover ()63
{64
if (!GameInput.mousePosition)65
return;66
if (this.collider.isInBounds(GameInput.mousePosition))67
{68
if (this.activeTween == null && (this.transform.position.x == this.originPosition.x && this.transform.position.y == this.originPosition.y))69
{70
this.createHoverTween(400, null, this).easing(TWEEN.Easing.Elastic.Out).start();71
}72
}73
else74
{75
if (this.activeTween == null &&76
(this.transform.position.x != this.originPosition.x || this.transform.position.y != this.originPosition.y) &&77
this.state == MenuStates.closed)78
{79
this.createInactiveStateTween(400, null, this).start();80
}81
}82
}83
84
//override for small opening from closed state85
createHoverTween (time, onDone, context)86
{87
return super.createHoverTween(time, onDone, context)88
.to({ x: this.originPosition.x + 10 }, time);89
}60
}90
}