BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / btnGames.js

btnGames.js

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