BlueGit

blue-star speeltuin

frog button out of tween from gamebtn

timfa wsadhelios · pushed by timfa

Authored 2022-10-22 · 4 years ago

Hash
23e1653d0b90f149f97ddd699d4d0a60816e4339
Parent
1607b3a6
2 files changed +89 −25
src/objects/menus/btnGames.js +80 −24
@@ -2,58 +2,114 @@ class BtnGames extends GameObject
2 2 {
3 3 buttons = [];
4 4 buttonPadding = 10;
5 buttonXOffset = -32;
6 5 state = 0;
7 6 prevState = 0;
8 7
9 menuAnchor = null;
10
11 static States = { closed: 0, open: 1, moving: 2 };
12
13 8 constructor (x, y, sprite, layer)
14 9 {
15 10 super(x, y, sprite, layer);
16
17 this.menuAnchor = this.transform.position.getCopy();
18 11 }
19 12
20 13 addedToScene ()
21 14 {
22 let frogButton = this.scene.addObject(new GameObject(0, 0, this.scene.game.images.btnFrog));
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);
23 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); };
24 38
25 39 this.buttons =
26 40 [
27 this.scene.addObject(frogButton)
41 this.scene.addObject(frogButton),
42 this.scene.addObject(frogButton2),
43 this.scene.addObject(frogButton3)
44
45
28 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 }
29 61 }
30 62
31 63 onClick (btn)
32 64 {
33 console.log("clicked games btn");
65 this.state == MenuManager.States.closed ?
66 this.moveToOpen() :
67 this.moveToClose();
34 68 }
35 69
36 onUpdate ()
70 moveToOpen ()
37 71 {
72 if (this.state == MenuManager.States.open)
73 return;
38 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());
39 77 }
40 78
41 update (deltaTime)
79 moveToClose ()
42 80 {
43 super.update(deltaTime);
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 }
44 86
45 for (let i = 0; i < this.buttons.length; i++)
46 {
47 if (i == 0)
87 createOpenTween (target, newTarget, length)
88 {
89 return new TWEEN.Tween(target)
90 .to(newTarget, length)
91 .onUpdate(() =>
48 92 {
49 this.buttons[i].transform.position.x = this.menuAnchor.x + this.buttonXOffset;
50 this.buttons[i].transform.position.y = this.menuAnchor.y + this.transform.size.y + this.buttonPadding;
51 }
52 else
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(() =>
53 104 {
54 this.buttons[i].transform.position.x = this.menuAnchor.x + this.buttonXOffset;
55 this.buttons[i].transform.position.y = this.buttons[i - 1].transform.position.y + this.buttons[i - 1].transform.size.y + this.buttonPadding;
56 }
57 }
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);
58 114 }
59 115 }
@@ -2,58 +2,114 @@ class BtnGames extends GameObject
2{
2{
3 buttons = [];
3 buttons = [];
4 buttonPadding = 10;
4 buttonPadding = 10;
5 buttonXOffset = -32;
6 state = 0;
5 state = 0;
7 prevState = 0;
6 prevState = 0;
8
7
9 menuAnchor = null;
10
11 static States = { closed: 0, open: 1, moving: 2 };
12
13 constructor (x, y, sprite, layer)
8 constructor (x, y, sprite, layer)
14 {
9 {
15 super(x, y, sprite, layer);
10 super(x, y, sprite, layer);
16
17 this.menuAnchor = this.transform.position.getCopy();
18 }
11 }
19
12
20 addedToScene ()
13 addedToScene ()
21 {
14 {
22 let frogButton = this.scene.addObject(new GameObject(0, 0, this.scene.game.images.btnFrog));
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);
23 frogButton.onClick = function () { console.log("clicked frog btn"); }; // todo instantiate guy
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); };
24
38
25 this.buttons =
39 this.buttons =
26 [
40 [
27 this.scene.addObject(frogButton)
41 this.scene.addObject(frogButton),
42 this.scene.addObject(frogButton2),
43 this.scene.addObject(frogButton3)
44
45
28 ];
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 }
29 }
61 }
30
62
31 onClick (btn)
63 onClick (btn)
32 {
64 {
33 console.log("clicked games btn");
65 this.state == MenuManager.States.closed ?
66 this.moveToOpen() :
67 this.moveToClose();
34 }
68 }
35
69
36 onUpdate ()
70 moveToOpen ()
37 {
71 {
72 if (this.state == MenuManager.States.open)
73 return;
38
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());
39 }
77 }
40
78
41 update (deltaTime)
79 moveToClose ()
42 {
80 {
43 super.update(deltaTime);
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 }
44
86
45 for (let i = 0; i < this.buttons.length; i++)
87 createOpenTween (target, newTarget, length)
46 {
88 {
47 if (i == 0)
89 return new TWEEN.Tween(target)
90 .to(newTarget, length)
91 .onUpdate(() =>
48 {
92 {
49 this.buttons[i].transform.position.x = this.menuAnchor.x + this.buttonXOffset;
93 this.updateButtonPosition && this.updateButtonPosition();
50 this.buttons[i].transform.position.y = this.menuAnchor.y + this.transform.size.y + this.buttonPadding;
94 this.buttons.forEach(e => e.onUpdate && e.onUpdate());
51 }
95 })
52 else
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(() =>
53 {
104 {
54 this.buttons[i].transform.position.x = this.menuAnchor.x + this.buttonXOffset;
105 this.updateButtonPosition && this.updateButtonPosition();
55 this.buttons[i].transform.position.y = this.buttons[i - 1].transform.position.y + this.buttons[i - 1].transform.size.y + this.buttonPadding;
106 this.buttons.forEach(e => e.onUpdate && e.onUpdate());
56 }
107 })
57 }
108 .easing(TWEEN.Easing.Quintic.Out);
109 }
110
111 update (deltaTime)
112 {
113 super.update(deltaTime);
58 }
114 }
59}
115}
src/objects/menus/menuManager.js +9 −1
@@ -17,6 +17,8 @@ class MenuManager extends GameObject
17 17 tweenTimer = 0;
18 18 timeDiffPercent = 1;
19 19 isOpened = null;
20 isClosed = null;
21 isLoaded = null;
20 22
21 23 static States = { closed: 0, open: 1, moving: 2 };
22 24
@@ -32,6 +34,8 @@ class MenuManager extends GameObject
32 34 addedToScene ()
33 35 {
34 36 this.isOpened = new Signal();
37 this.isClosed = new Signal();
38 this.isLoaded = new Signal();
35 39
36 40 this.moveToOpen(() => this.moveToClose());
37 41 this.buttons =
@@ -41,6 +45,10 @@ class MenuManager extends GameObject
41 45 this.scene.addObject(new BtnGames(0, 0, this.scene.game.images.btnGames)),
42 46 this.scene.addObject(new BtnPlants(0, 0, this.scene.game.images.btnPlants))
43 47 ];
48
49 this.updateButtonPosition();
50
51 this.isLoaded.dispatch();
44 52 }
45 53
46 54 onClick (btn)
@@ -70,6 +78,7 @@ class MenuManager extends GameObject
70 78
71 79 moveToClose (onDone)
72 80 {
81 this.isClosed.dispatch();
73 82 this.prevState = MenuManager.States.open;
74 83
75 84 if (this.state == MenuManager.States.moving)
@@ -106,7 +115,6 @@ class MenuManager extends GameObject
106 115 .onUpdate(() =>
107 116 {
108 117 this.updateButtonPosition();
109 this.buttons.forEach(e => e.onUpdate && e.onUpdate());
110 118 })
111 119 .easing(TWEEN.Easing.Elastic.Out);
112 120 }
@@ -17,6 +17,8 @@ class MenuManager extends GameObject
17 tweenTimer = 0;
17 tweenTimer = 0;
18 timeDiffPercent = 1;
18 timeDiffPercent = 1;
19 isOpened = null;
19 isOpened = null;
20 isClosed = null;
21 isLoaded = null;
20
22
21 static States = { closed: 0, open: 1, moving: 2 };
23 static States = { closed: 0, open: 1, moving: 2 };
22
24
@@ -32,6 +34,8 @@ class MenuManager extends GameObject
32 addedToScene ()
34 addedToScene ()
33 {
35 {
34 this.isOpened = new Signal();
36 this.isOpened = new Signal();
37 this.isClosed = new Signal();
38 this.isLoaded = new Signal();
35
39
36 this.moveToOpen(() => this.moveToClose());
40 this.moveToOpen(() => this.moveToClose());
37 this.buttons =
41 this.buttons =
@@ -41,6 +45,10 @@ class MenuManager extends GameObject
41 this.scene.addObject(new BtnGames(0, 0, this.scene.game.images.btnGames)),
45 this.scene.addObject(new BtnGames(0, 0, this.scene.game.images.btnGames)),
42 this.scene.addObject(new BtnPlants(0, 0, this.scene.game.images.btnPlants))
46 this.scene.addObject(new BtnPlants(0, 0, this.scene.game.images.btnPlants))
43 ];
47 ];
48
49 this.updateButtonPosition();
50
51 this.isLoaded.dispatch();
44 }
52 }
45
53
46 onClick (btn)
54 onClick (btn)
@@ -70,6 +78,7 @@ class MenuManager extends GameObject
70
78
71 moveToClose (onDone)
79 moveToClose (onDone)
72 {
80 {
81 this.isClosed.dispatch();
73 this.prevState = MenuManager.States.open;
82 this.prevState = MenuManager.States.open;
74
83
75 if (this.state == MenuManager.States.moving)
84 if (this.state == MenuManager.States.moving)
@@ -106,7 +115,6 @@ class MenuManager extends GameObject
106 .onUpdate(() =>
115 .onUpdate(() =>
107 {
116 {
108 this.updateButtonPosition();
117 this.updateButtonPosition();
109 this.buttons.forEach(e => e.onUpdate && e.onUpdate());
110 })
118 })
111 .easing(TWEEN.Easing.Elastic.Out);
119 .easing(TWEEN.Easing.Elastic.Out);
112 }
120 }