BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / fruitInventory.js

fruitInventory.js

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