BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / fruitInventory.js

fruitInventory.js

fix fruit menu
timfa Tim · pushed by timfa · 4 years ago
65325845 History
fruitInventory.js 2.9 KiB Download Raw
1class FruitInventory extends GameObject
2{
3 closeButton = null;
4 closeBtnX = 120;
5 closeBtnY = -140;
6 state = 0;
7
8 static Instance = null;
9
10 g_inv = null;
11 get inventory ()
12 {
13 if (!this.g_inv)
14 {
15 let inv = CookieManager.getCookie("fruitInventory");
16 if (inv.length <= 0)
17 inv = "[]";
18
19 this.g_inv = JSON.parse(inv);
20 this.g_inv = this.g_inv || [];
21 }
22
23 return this.g_inv;
24 }
25
26 constructor (x, y, sprite, layer)
27 {
28 super(x, y, sprite, layer);
29 FruitInventory.Instance = this;
30 }
31
32 saveInventory ()
33 {
34 CookieManager.setCookie("fruitInventory", JSON.stringify(this.inventory));
35 }
36
37 getFruitAmountOfType (type)
38 {
39 let count = 0;
40
41 for (let i = 0; i < this.inventory.length; i++)
42 {
43 if (this.inventory[i] == type)
44 count++;
45 }
46
47 return count;
48 }
49
50 takeFromInventory (type)
51 {
52 for (let i = 0; i < this.inventory.length; i++)
53 {
54 if (this.inventory[i] == type)
55 {
56 this.inventory.splice(i, 1);
57 this.saveInventory();
58
59 return new ItemFruit(GameInput.mousePosition.x, GameInput.mousePosition.y, this.scene.game.images.fruit, 0, type);
60 }
61 }
62
63 this.moveToClose();
64
65 return null;
66 }
67
68 addToInventory (type, amount = 1)
69 {
70 for (let i = 0; i < amount; i++)
71 this.inventory.push(type);
72
73 this.saveInventory();
74 }
75
76 addedToScene ()
77 {
78 let pos = this.transform.position.add(new vector(this.closeBtnX, this.closeBtnY));
79 this.closeButton = this.scene.addObject(new SubBtn(pos.x, pos.y, this.scene.game.images.closeButton));
80
81 this.moveToClose();
82
83 this.closeButton.isClicked.add(function ()
84 {
85 this.moveToClose();
86 }, this);
87
88 MenuManager.Instance.isOpened.add(function ()
89 {
90 this.moveToClose();
91 }, this);
92 }
93
94 update (deltaTime)
95 {
96 super.update(deltaTime);
97 let pos = this.transform.position.add(new vector(this.closeBtnX, this.closeBtnY));
98 this.closeButton.transform.position = pos;
99 }
100
101 moveToOpen (onDone)
102 {
103 let context = this;
104 new TWEEN.Tween(this.transform.position)
105 .to({ y: 250 }, 1500)
106 .easing(TWEEN.Easing.Elastic.Out)
107 .onComplete(() => { this.state = MenuManager.States.open; onDone && onDone.call(context); })
108 .start();
109 }
110
111 moveToClose (onDone)
112 {
113 let context = this;
114 new TWEEN.Tween(this.transform.position)
115 .to({ y: -500 }, 1500)
116 .easing(TWEEN.Easing.Elastic.Out)
117 .onComplete(() => { this.state = MenuManager.States.closed; onDone && onDone.call(context); })
118 .start();
119 }
120}