BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / fruitInventory.js

fruitInventory.js

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