BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / btnGames.js

btnGames.js

Oh boy tweening out the wazoo
timfa wsadhelios · pushed by timfa · 4 years ago
8b45c044 History
btnGames.js 3.2 KiB Download Raw
1class BtnGames extends GameObject
2{
3 buttons = [];
4 buttonPadding = 10;
5 state = 0;
6 prevState = 0;
7
8 subBtnStartPosX = -32;
9
10 constructor (x, y, sprite, layer)
11 {
12 super(x, y, sprite, layer);
13 }
14
15 addedToScene ()
16 {
17 MenuManager.Instance.isLoaded.add(() => this.initializeButtons(), this);
18 MenuManager.Instance.isClosed.add(() => this.moveToClose(), this);
19 }
20
21 initializeButtons ()
22 {
23 this.createSubButton(this.scene.game.images.btnFrog, function ()
24 {
25 MinigameInitialiser.StartFrogGame(this.scene, this.transform.position, this.transform.size);
26 MenuManager.Instance.moveToClose();
27
28 });
29
30 for (let i = 0; i < this.buttons.length; i++)
31 {
32 if (i == 0)
33 {
34 this.buttons[i].endPos = new vector(this.transform.position.x + this.transform.size.x * 2 + this.buttonPadding, this.transform.position.y + this.buttonPadding);
35 }
36 else
37 {
38 this.buttons[i].endPos = new vector(
39 this.buttons[i - 1].endPos.x,
40 this.buttons[i - 1].endPos.y + this.buttons[i - 1].transform.size.y + this.buttonPadding);
41 }
42 }
43 }
44
45 createSubButton (image, onClick)
46 {
47 let button = new GameObject(this.subBtnStartPosX, this.transform.position.y, image, 3);
48 this.scene.addObject(button);
49 button.startPos = new vector(-32, this.transform.position.y);
50 button.onClick = onClick;
51
52 this.buttons.push(button);
53 }
54
55 onClick (btn)
56 {
57 this.state == MenuManager.States.closed ?
58 this.moveToOpen() :
59 this.moveToClose();
60 }
61
62 moveToOpen ()
63 {
64 if (this.state == MenuManager.States.open)
65 return;
66
67 this.state = MenuManager.States.open;
68 this.buttons.forEach(e => this.createOpenTween(e.transform.position, { x: [e.endPos.x], y: [e.startPos.y, e.startPos.y, e.startPos.y, e.endPos.y] }, 500).start());
69 }
70
71 moveToClose ()
72 {
73 if (this.state == MenuManager.States.closed)
74 return;
75 this.state = MenuManager.States.closed;
76 this.buttons.forEach(e => this.createCloseTween(e.transform.position, { x: [e.endPos.x, e.endPos.x, e.endPos.x, e.endPos.x, e.startPos.x], y: [e.startPos.y] }, 1000).start());
77 }
78
79 createOpenTween (target, newTarget, length)
80 {
81 return new TWEEN.Tween(target)
82 .to(newTarget, length)
83 .onUpdate(() =>
84 {
85 this.updateButtonPosition && this.updateButtonPosition();
86 this.buttons.forEach(e => e.onUpdate && e.onUpdate());
87 })
88 .easing(TWEEN.Easing.Back.Out);
89 }
90
91 createCloseTween (target, newTarget, length)
92 {
93 return new TWEEN.Tween(target)
94 .to(newTarget, length)
95 .onUpdate(() =>
96 {
97 this.updateButtonPosition && this.updateButtonPosition();
98 this.buttons.forEach(e => e.onUpdate && e.onUpdate());
99 })
100 .easing(TWEEN.Easing.Quintic.Out);
101 }
102
103 update (deltaTime)
104 {
105 super.update(deltaTime);
106 }
107}