BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / btnGames.js

btnGames.js

Frog game init works
timfa wsadhelios · pushed by timfa · 4 years ago
42885b26 History
btnGames.js 3.1 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 () { MinigameInitialiser.StartFrogGame(this.scene); MenuManager.Instance.moveToClose(); });
24
25 for (let i = 0; i < this.buttons.length; i++)
26 {
27 if (i == 0)
28 {
29 this.buttons[i].endPos = new vector(this.transform.position.x + this.transform.size.x * 2 + this.buttonPadding, this.transform.position.y + this.buttonPadding);
30 }
31 else
32 {
33 this.buttons[i].endPos = new vector(
34 this.buttons[i - 1].endPos.x,
35 this.buttons[i - 1].endPos.y + this.buttons[i - 1].transform.size.y + this.buttonPadding);
36 }
37 }
38 }
39
40 createSubButton (image, onClick)
41 {
42 let button = new GameObject(this.subBtnStartPosX, this.transform.position.y, image, 3);
43 this.scene.addObject(button);
44 button.startPos = new vector(-32, this.transform.position.y);
45 button.onClick = onClick;
46
47 this.buttons.push(button);
48 }
49
50 onClick (btn)
51 {
52 this.state == MenuManager.States.closed ?
53 this.moveToOpen() :
54 this.moveToClose();
55 }
56
57 moveToOpen ()
58 {
59 if (this.state == MenuManager.States.open)
60 return;
61
62 this.state = MenuManager.States.open;
63 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());
64 }
65
66 moveToClose ()
67 {
68 if (this.state == MenuManager.States.closed)
69 return;
70 this.state = MenuManager.States.closed;
71 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());
72 }
73
74 createOpenTween (target, newTarget, length)
75 {
76 return new TWEEN.Tween(target)
77 .to(newTarget, length)
78 .onUpdate(() =>
79 {
80 this.updateButtonPosition && this.updateButtonPosition();
81 this.buttons.forEach(e => e.onUpdate && e.onUpdate());
82 })
83 .easing(TWEEN.Easing.Back.Out);
84 }
85
86 createCloseTween (target, newTarget, length)
87 {
88 return new TWEEN.Tween(target)
89 .to(newTarget, length)
90 .onUpdate(() =>
91 {
92 this.updateButtonPosition && this.updateButtonPosition();
93 this.buttons.forEach(e => e.onUpdate && e.onUpdate());
94 })
95 .easing(TWEEN.Easing.Quintic.Out);
96 }
97
98 update (deltaTime)
99 {
100 super.update(deltaTime);
101 }
102}