BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / fruitInventory.js

fruitInventory.js

Menu component
timfa Nick Hiert · pushed by timfa · 4 years ago
f555b1c2 History
fruitInventory.js 5.0 KiB Download Raw
1class FruitInventory extends GameObject
2{
3 closeButton = null;
4 closeBtnX = 120;
5 closeBtnY = -140;
6 state = 0;
7
8 hBounds = 120;
9 vBounds = 120;
10 vOffset = 20;
11
12 static Instance = null;
13
14 activeTween = null;
15
16 fruitInventoryButtons = [];
17
18 g_inv = null;
19 get inventory ()
20 {
21 if (!this.g_inv)
22 {
23 let inv = CookieManager.getCookie("fruitInventory");
24 if (inv.length <= 0)
25 inv = "[]";
26
27 this.g_inv = JSON.parse(inv);
28 this.g_inv = this.g_inv || [];
29 }
30
31 return this.g_inv;
32 }
33
34 constructor (x, y, sprite, layer)
35 {
36 super(x, y, sprite, layer);
37 FruitInventory.Instance = this;
38 }
39
40 saveInventory ()
41 {
42 CookieManager.setCookie("fruitInventory", JSON.stringify(this.inventory));
43 }
44
45 getFruitAmountOfType (type)
46 {
47 let count = 0;
48
49 for (let i = 0; i < this.inventory.length; i++)
50 {
51 if (this.inventory[i] == type)
52 count++;
53 }
54
55 return count;
56 }
57
58 takeFromInventory (type)
59 {
60 for (let i = 0; i < this.inventory.length; i++)
61 {
62 if (this.inventory[i] == type)
63 {
64 this.inventory.splice(i, 1);
65 this.saveInventory();
66
67 let newFruit = new ItemFruit(GameInput.mousePosition.x, GameInput.mousePosition.y, this.scene.game.images.fruit, 0);
68 newFruit.setType(type);
69 return newFruit;
70 }
71 }
72
73 this.moveToClose();
74
75 return null;
76 }
77
78 addToInventory (type, amount = 1)
79 {
80 for (let i = 0; i < amount; i++)
81 this.inventory.push(type);
82
83 this.saveInventory();
84 }
85
86 addedToScene ()
87 {
88 let pos = this.transform.position.add(new vector(this.closeBtnX, this.closeBtnY));
89 this.closeButton = this.scene.addObject(new SubBtn(pos.x, pos.y, this.scene.game.images.closeButton));
90
91 this.moveToClose();
92
93 this.closeButton.isClicked.add(function ()
94 {
95 this.moveToClose();
96 }, this);
97
98 MainMenu.Instance.menu.isOpened.add(function ()
99 {
100 this.moveToClose();
101 }, this);
102 }
103
104 update (deltaTime)
105 {
106 super.update(deltaTime);
107 let pos = this.transform.position.add(new vector(this.closeBtnX, this.closeBtnY));
108 this.closeButton.transform.position = pos;
109 }
110
111 updateFruitsPositions ()
112 {
113 let xp = -this.hBounds;
114 let yp = -this.vBounds;
115
116 for (let i = 0; i < this.fruitInventoryButtons.length; i++)
117 {
118 this.fruitInventoryButtons[i].transform.position.x = this.transform.position.x + xp;
119 this.fruitInventoryButtons[i].transform.position.y = this.transform.position.y + yp + this.vOffset;
120
121 yp += this.scene.game.images.fruit.height * this.fruitInventoryButtons[i].transform.scale.y;
122
123 if (yp > this.vBounds)
124 {
125 yp = -this.vBounds;
126 xp += this.scene.game.images.fruit.width * this.fruitInventoryButtons[i].transform.scale.x;
127 }
128 }
129 }
130
131 moveToOpen (onDone)
132 {
133 let context = this;
134 let tween = new TWEEN.Tween(this.transform.position)
135 .to({ y: 250 }, 1500)
136 .easing(TWEEN.Easing.Elastic.Out)
137 .onUpdate(() => { FruitInventory.Instance.updateFruitsPositions(); })
138 .onComplete(() => { this.state = MenuStates.open; this.activeTween = null; onDone && onDone.call(context); });
139
140
141 tween.onStart(() => { this.clearFruitButtons(); this.stopActiveTween(tween); this.createFruitButtons(); }).start();
142
143 let spawnedAndBelowMax = true;
144
145 }
146
147 moveToClose (onDone)
148 {
149 let context = this;
150 let tween = new TWEEN.Tween(this.transform.position)
151 .to({ y: -500 }, 500)
152 .easing(TWEEN.Easing.Back.In)
153 .onUpdate(() => { FruitInventory.Instance.updateFruitsPositions(); })
154 .onComplete(() =>
155 {
156 this.state = MenuStates.closed;
157 this.activeTween = null;
158 this.clearFruitButtons();
159
160 onDone && onDone.call(context);
161 });
162
163 tween.onStart(() => this.stopActiveTween(tween)).start();
164 }
165
166 createFruitButtons ()
167 {
168 for (let t = 0; t < this.scene.game.images.fruit.subImages; t++)
169 {
170 let amount = Math.min(17, this.getFruitAmountOfType(t));
171
172 for (let i = 0; i < amount; i++)
173 {
174 this.fruitInventoryButtons.push(this.scene.addObject(new FruitInventoryButton(-50, -50, this.scene.game.images.fruit, 2, t)));
175 }
176 }
177
178 }
179
180 clearFruitButtons ()
181 {
182 for (let i = 0; i < this.fruitInventoryButtons.length; i++)
183 this.fruitInventoryButtons[i].destroy();
184
185 this.fruitInventoryButtons = [];
186 }
187
188 stopActiveTween (tween)
189 {
190 if (this.activeTween)
191 {
192 this.activeTween.stop();
193 }
194 this.activeTween = tween;
195 }
196}