BlueGit

blue-star speeltuin

menu

timfa Tim · pushed by timfa

Authored 2022-10-21 · 4 years ago

Hash
0cb5543bf40920403bf58861d5b21b536aab50c2
Parent
fa489339
14 files changed +178 −20
build/framework.js +70 −19
@@ -320,19 +320,20 @@ class Collider
320 320 {
321 321 if (this.enabled)
322 322 {
323 let pressed = GameInput.mousePressed();
324 let held = GameInput.mouseHeld();
325 if (pressed.left || pressed.middle || pressed.right)
326 {
327 this.checkClick(true, pressed);
328 }
323 this.checkCollisions();
324 }
329 325
330 if (held.left || held.middle || held.right)
331 {
332 this.checkClick(false, held);
333 }
326 let pressed = GameInput.mousePressed();
327 let held = GameInput.mouseHeld();
334 328
335 this.checkCollisions();
329 if (pressed.left || pressed.middle || pressed.right)
330 {
331 this.checkClick(true, pressed);
332 }
333
334 if (held.left || held.middle || held.right)
335 {
336 this.checkClick(false, held);
336 337 }
337 338 }
338 339
@@ -413,11 +414,9 @@ class Collider
413 414 this.gameObject.transform.position.y + hh
414 415 ];
415 416
416 if (points[0] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw ||
417 points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw)
417 if (points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw)
418 418 {
419 if (points[2] > o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh ||
420 points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh)
419 if (points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh)
421 420 {
422 421 hit = true;
423 422 }
@@ -616,12 +615,64 @@ class Renderer
616 615 */
617 616 class Transform
618 617 {
619 position = null;
620 scale = null;
618 get position ()
619 {
620 if (!this.g_position)
621 this.g_position = new vector(0, 0);
622
623 return this.g_position;
624 }
625
626 set position (v)
627 {
628 if (!this.g_position)
629 this.g_position = new vector(v.x, v.y);
630
631 this.g_position.x = v.x;
632 this.g_position.y = v.y;
633 }
634
635 get scale ()
636 {
637 if (!this.g_scale)
638 this.g_scale = new vector(0, 0);
639
640 return this.g_scale;
641 }
642
643 set scale (v)
644 {
645 if (!this.g_scale)
646 this.g_scale = new vector(v.x, v.y);
647
648 this.g_scale.x = v.x;
649 this.g_scale.y = v.y;
650 }
651
652 get velocity ()
653 {
654 if (!this.g_velocity)
655 this.g_velocity = new vector(0, 0);
656
657 return this.g_velocity;
658 }
659
660 set velocity (v)
661 {
662 if (!this.g_velocity)
663 this.g_velocity = new vector(v.x, v.y);
664
665 this.g_velocity.x = v.x;
666 this.g_velocity.y = v.y;
667 }
668
669 g_position = null;
670
671 g_scale = null;
621 672 rotation = 0;
622 673 gameObject = null;
623 674
624 velocity = null;
675 g_velocity = null;
625 676 angularVelocity = 0;
626 677
627 678 get size ()
@@ -817,7 +868,7 @@ class Scene
817 868
818 869 if (this.pendingRemoval.length)
819 870 {
820 this.pendingRemoval.sort();
871 this.pendingRemoval.sort((a, b) => a - b);
821 872
822 873 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
823 874 {
@@ -320,19 +320,20 @@ class Collider
320 {
320 {
321 if (this.enabled)
321 if (this.enabled)
322 {
322 {
323 let pressed = GameInput.mousePressed();
323 this.checkCollisions();
324 let held = GameInput.mouseHeld();
324 }
325 if (pressed.left || pressed.middle || pressed.right)
326 {
327 this.checkClick(true, pressed);
328 }
329
325
330 if (held.left || held.middle || held.right)
326 let pressed = GameInput.mousePressed();
331 {
327 let held = GameInput.mouseHeld();
332 this.checkClick(false, held);
333 }
334
328
335 this.checkCollisions();
329 if (pressed.left || pressed.middle || pressed.right)
330 {
331 this.checkClick(true, pressed);
332 }
333
334 if (held.left || held.middle || held.right)
335 {
336 this.checkClick(false, held);
336 }
337 }
337 }
338 }
338
339
@@ -413,11 +414,9 @@ class Collider
413 this.gameObject.transform.position.y + hh
414 this.gameObject.transform.position.y + hh
414 ];
415 ];
415
416
416 if (points[0] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw ||
417 if (points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw)
417 points[1] >= o2.transform.position.x - ohw && points[0] <= o2.transform.position.x + ohw)
418 {
418 {
419 if (points[2] > o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh ||
419 if (points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh)
420 points[3] >= o2.transform.position.y - ohh && points[2] <= o2.transform.position.y + ohh)
421 {
420 {
422 hit = true;
421 hit = true;
423 }
422 }
@@ -616,12 +615,64 @@ class Renderer
616 */
615 */
617class Transform
616class Transform
618{
617{
619 position = null;
618 get position ()
620 scale = null;
619 {
620 if (!this.g_position)
621 this.g_position = new vector(0, 0);
622
623 return this.g_position;
624 }
625
626 set position (v)
627 {
628 if (!this.g_position)
629 this.g_position = new vector(v.x, v.y);
630
631 this.g_position.x = v.x;
632 this.g_position.y = v.y;
633 }
634
635 get scale ()
636 {
637 if (!this.g_scale)
638 this.g_scale = new vector(0, 0);
639
640 return this.g_scale;
641 }
642
643 set scale (v)
644 {
645 if (!this.g_scale)
646 this.g_scale = new vector(v.x, v.y);
647
648 this.g_scale.x = v.x;
649 this.g_scale.y = v.y;
650 }
651
652 get velocity ()
653 {
654 if (!this.g_velocity)
655 this.g_velocity = new vector(0, 0);
656
657 return this.g_velocity;
658 }
659
660 set velocity (v)
661 {
662 if (!this.g_velocity)
663 this.g_velocity = new vector(v.x, v.y);
664
665 this.g_velocity.x = v.x;
666 this.g_velocity.y = v.y;
667 }
668
669 g_position = null;
670
671 g_scale = null;
621 rotation = 0;
672 rotation = 0;
622 gameObject = null;
673 gameObject = null;
623
674
624 velocity = null;
675 g_velocity = null;
625 angularVelocity = 0;
676 angularVelocity = 0;
626
677
627 get size ()
678 get size ()
@@ -817,7 +868,7 @@ class Scene
817
868
818 if (this.pendingRemoval.length)
869 if (this.pendingRemoval.length)
819 {
870 {
820 this.pendingRemoval.sort();
871 this.pendingRemoval.sort((a, b) => a - b);
821
872
822 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
873 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
823 {
874 {
build/images/buttons/menubutton.png added Binary file +0 −0

The contents of this binary file are not shown.

build/images/buttons/menubutton_blank.png added Binary file +0 −0

The contents of this binary file are not shown.

build/images/buttons/menubutton_frog.png added Binary file +0 −0

The contents of this binary file are not shown.

build/images/buttons/menubutton_fruit.png added Binary file +0 −0

The contents of this binary file are not shown.

build/images/buttons/menubutton_games.png added Binary file +0 −0

The contents of this binary file are not shown.

build/images/buttons/menubutton_plants.png added Binary file +0 −0

The contents of this binary file are not shown.

build/images/buttons/menubutton_toys.png added Binary file +0 −0

The contents of this binary file are not shown.

src/game.js +9 −1
@@ -31,7 +31,13 @@ window.onload = function ()
31 31 { name: "chao_yellow", url: "images/character/chao/yellow.png", subImgTotal: 67, perRow: 9 },
32 32 { name: "tile_floor_brown", url: "images/tile/tile_floor_brown.png" },
33 33 { name: "tile_floor_explosion", url: "images/tile/tile_floor_explosion_anim.png", subImgTotal: 10, perRow: 5 },
34 { name: "fruit", url: "images/object/fruit.png", subImgTotal: 15, perRow: 8 }
34 { name: "fruit", url: "images/object/fruit.png", subImgTotal: 15, perRow: 8 },
35
36 { name: "menuButton", url: "images/buttons/menubutton.png", subImgTotal: 1, perRow: 1 },
37 { name: "btnFruit", url: "images/buttons/menubutton_fruit.png", subImgTotal: 1, perRow: 1 },
38 { name: "btnToys", url: "images/buttons/menubutton_toys.png", subImgTotal: 1, perRow: 1 },
39 { name: "btnGames", url: "images/buttons/menubutton_games.png", subImgTotal: 1, perRow: 1 },
40 { name: "btnPlants", url: "images/buttons/menubutton_plants.png", subImgTotal: 1, perRow: 1 }
35 41 ]
36 42 , function ()
37 43 {
@@ -119,6 +125,8 @@ createMainScene = function (game)
119 125 chao = new Chao(60, 60, game.images.chao_default, 0);
120 126 scene.addObject(chao);
121 127
128 scene.addObject(new MenuManager(0, 150, game.images.menuButton, 0));
129
122 130 createSubScene(scene);
123 131 };
124 132
@@ -31,7 +31,13 @@ window.onload = function ()
31 { name: "chao_yellow", url: "images/character/chao/yellow.png", subImgTotal: 67, perRow: 9 },
31 { name: "chao_yellow", url: "images/character/chao/yellow.png", subImgTotal: 67, perRow: 9 },
32 { name: "tile_floor_brown", url: "images/tile/tile_floor_brown.png" },
32 { name: "tile_floor_brown", url: "images/tile/tile_floor_brown.png" },
33 { name: "tile_floor_explosion", url: "images/tile/tile_floor_explosion_anim.png", subImgTotal: 10, perRow: 5 },
33 { name: "tile_floor_explosion", url: "images/tile/tile_floor_explosion_anim.png", subImgTotal: 10, perRow: 5 },
34 { name: "fruit", url: "images/object/fruit.png", subImgTotal: 15, perRow: 8 }
34 { name: "fruit", url: "images/object/fruit.png", subImgTotal: 15, perRow: 8 },
35
36 { name: "menuButton", url: "images/buttons/menubutton.png", subImgTotal: 1, perRow: 1 },
37 { name: "btnFruit", url: "images/buttons/menubutton_fruit.png", subImgTotal: 1, perRow: 1 },
38 { name: "btnToys", url: "images/buttons/menubutton_toys.png", subImgTotal: 1, perRow: 1 },
39 { name: "btnGames", url: "images/buttons/menubutton_games.png", subImgTotal: 1, perRow: 1 },
40 { name: "btnPlants", url: "images/buttons/menubutton_plants.png", subImgTotal: 1, perRow: 1 }
35 ]
41 ]
36 , function ()
42 , function ()
37 {
43 {
@@ -119,6 +125,8 @@ createMainScene = function (game)
119 chao = new Chao(60, 60, game.images.chao_default, 0);
125 chao = new Chao(60, 60, game.images.chao_default, 0);
120 scene.addObject(chao);
126 scene.addObject(chao);
121
127
128 scene.addObject(new MenuManager(0, 150, game.images.menuButton, 0));
129
122 createSubScene(scene);
130 createSubScene(scene);
123};
131};
124
132
src/objects/menus/btnFruit.js added +4 −0
@@ -0,0 +1,4 @@
1 class BtnFruit extends GameObject
2 {
3
4 }
@@ -0,0 +1,4 @@
1class BtnFruit extends GameObject
2{
3
4}
src/objects/menus/btnGames.js added +4 −0
@@ -0,0 +1,4 @@
1 class BtnGames extends GameObject
2 {
3
4 }
@@ -0,0 +1,4 @@
1class BtnGames extends GameObject
2{
3
4}
src/objects/menus/btnPlants.js added +4 −0
@@ -0,0 +1,4 @@
1 class BtnPlants extends GameObject
2 {
3
4 }
@@ -0,0 +1,4 @@
1class BtnPlants extends GameObject
2{
3
4}
src/objects/menus/btnToys.js added +4 −0
@@ -0,0 +1,4 @@
1 class BtnToys extends GameObject
2 {
3
4 }
@@ -0,0 +1,4 @@
1class BtnToys extends GameObject
2{
3
4}
src/objects/menus/menuManager.js added +83 −0
@@ -0,0 +1,83 @@
1 class MenuManager extends GameObject
2 {
3 static Instance;
4
5 buttons = [];
6 buttonPadding = 10;
7 buttonXOffset = -32;
8 state = 0;
9
10 static States = { closed: 0, open: 1, moving: 2 };
11
12 constructor (x, y, sprite, layer)
13 {
14 super(x, y, sprite, layer);
15 MenuManager.Instance = this;
16 }
17
18 addedToScene ()
19 {
20 this.moveToOpen(this.moveToClose);
21 this.buttons =
22 [
23 this.scene.addObject(new BtnFruit(0, 0, this.scene.game.images.btnFruit)),
24 this.scene.addObject(new BtnToys(0, 0, this.scene.game.images.btnToys)),
25 this.scene.addObject(new BtnGames(0, 0, this.scene.game.images.btnGames)),
26 this.scene.addObject(new BtnPlants(0, 0, this.scene.game.images.btnPlants))
27 ];
28 }
29
30 onClick (btn)
31 {
32 this.state == MenuManager.States.open ? this.moveToClose() : this.moveToOpen();
33 }
34
35 moveToOpen (onDone)
36 {
37 if (this.state == MenuManager.States.moving)
38 return;
39
40 this.state = MenuManager.States.moving;
41
42 let context = this;
43 new TWEEN.Tween(this.transform.position)
44 .to({ x: 60 }, 800)
45 .easing(TWEEN.Easing.Bounce.Out)
46 .onComplete(() => { this.state = MenuManager.States.open; onDone && onDone.call(context); })
47 .start();
48 }
49
50 moveToClose (onDone)
51 {
52 if (this.state == MenuManager.States.moving)
53 return;
54
55 this.state = MenuManager.States.moving;
56
57 let context = this;
58 new TWEEN.Tween(this.transform.position)
59 .to({ x: -15 }, 1500)
60 .easing(TWEEN.Easing.Elastic.Out)
61 .onComplete(() => { this.state = MenuManager.States.closed; onDone && onDone.call(context); })
62 .start();
63 }
64
65 update (deltaTime)
66 {
67 super.update(deltaTime);
68
69 for (let i = 0; i < this.buttons.length; i++)
70 {
71 if (i == 0)
72 {
73 this.buttons[i].transform.position.x = this.transform.position.x + this.buttonXOffset;
74 this.buttons[i].transform.position.y = this.transform.position.y + this.transform.size.y + this.buttonPadding;
75 }
76 else
77 {
78 this.buttons[i].transform.position.x = this.transform.position.x + this.buttonXOffset;
79 this.buttons[i].transform.position.y = this.buttons[i - 1].transform.position.y + this.buttons[i - 1].transform.size.y + this.buttonPadding;
80 }
81 }
82 }
83 }
@@ -0,0 +1,83 @@
1class MenuManager extends GameObject
2{
3 static Instance;
4
5 buttons = [];
6 buttonPadding = 10;
7 buttonXOffset = -32;
8 state = 0;
9
10 static States = { closed: 0, open: 1, moving: 2 };
11
12 constructor (x, y, sprite, layer)
13 {
14 super(x, y, sprite, layer);
15 MenuManager.Instance = this;
16 }
17
18 addedToScene ()
19 {
20 this.moveToOpen(this.moveToClose);
21 this.buttons =
22 [
23 this.scene.addObject(new BtnFruit(0, 0, this.scene.game.images.btnFruit)),
24 this.scene.addObject(new BtnToys(0, 0, this.scene.game.images.btnToys)),
25 this.scene.addObject(new BtnGames(0, 0, this.scene.game.images.btnGames)),
26 this.scene.addObject(new BtnPlants(0, 0, this.scene.game.images.btnPlants))
27 ];
28 }
29
30 onClick (btn)
31 {
32 this.state == MenuManager.States.open ? this.moveToClose() : this.moveToOpen();
33 }
34
35 moveToOpen (onDone)
36 {
37 if (this.state == MenuManager.States.moving)
38 return;
39
40 this.state = MenuManager.States.moving;
41
42 let context = this;
43 new TWEEN.Tween(this.transform.position)
44 .to({ x: 60 }, 800)
45 .easing(TWEEN.Easing.Bounce.Out)
46 .onComplete(() => { this.state = MenuManager.States.open; onDone && onDone.call(context); })
47 .start();
48 }
49
50 moveToClose (onDone)
51 {
52 if (this.state == MenuManager.States.moving)
53 return;
54
55 this.state = MenuManager.States.moving;
56
57 let context = this;
58 new TWEEN.Tween(this.transform.position)
59 .to({ x: -15 }, 1500)
60 .easing(TWEEN.Easing.Elastic.Out)
61 .onComplete(() => { this.state = MenuManager.States.closed; onDone && onDone.call(context); })
62 .start();
63 }
64
65 update (deltaTime)
66 {
67 super.update(deltaTime);
68
69 for (let i = 0; i < this.buttons.length; i++)
70 {
71 if (i == 0)
72 {
73 this.buttons[i].transform.position.x = this.transform.position.x + this.buttonXOffset;
74 this.buttons[i].transform.position.y = this.transform.position.y + this.transform.size.y + this.buttonPadding;
75 }
76 else
77 {
78 this.buttons[i].transform.position.x = this.transform.position.x + this.buttonXOffset;
79 this.buttons[i].transform.position.y = this.buttons[i - 1].transform.position.y + this.buttons[i - 1].transform.size.y + this.buttonPadding;
80 }
81 }
82 }
83}