]> git.lizzy.rs Git - dragonblocks.git/blob - engine/inventory.js
b7142458c6f5455e8ace485fe38bb5608d83c867
[dragonblocks.git] / engine / inventory.js
1 /*
2  * inventory.js
3  *
4  * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  *
22  */
23
24 dragonblocks.Inventory = class extends EventTarget
25 {
26         constructor(slots, columns)
27         {
28                 super();
29                 this.id = dragonblocks.getToken();
30
31                 this.slots = slots;
32                 this.columns = columns;
33                 this.list = [];
34
35                 let self = this;
36
37                 for (let i = 0; i < this.slots; i++){
38                         let stack = this.list[i] = new dragonblocks.ItemStack();
39                         stack.addEventListener("update", event => {
40                                 self.dispatchEvent(new dragonblocks.Inventory.Event("updateStack", event.stack));
41                         });
42                 }
43
44                 this.display = false;
45         }
46
47         serialize()
48         {
49                 let str = "";
50
51                 for (let stack of this.list)
52                         str += stack.serialize() + ",";
53
54                 return str;
55         }
56
57         deserialize(str)
58         {
59                 let strList = str.split(",");
60
61                 for (let i in this.list)
62                         this.list[i].deserialize(strList[i]);
63         }
64
65         add(itemstring)
66         {
67                 let itemstack = new dragonblocks.ItemStack(itemstring);
68
69                 for (let stack of this.list)
70                         stack.item == itemstack.item && stack.add(itemstack);
71
72                 for (let stack of this.list)
73                         stack.add(itemstack);
74
75                 return itemstack;
76         }
77
78         isEmpty()
79         {
80                 for (let stack of this.list)
81                         if (stack.item)
82                                 return false;
83
84                 return true;
85         }
86
87         clear()
88         {
89                 for(let stack of this.list)
90                         stack.clear();
91         }
92
93         calculateWidth(columns)
94         {
95                 return dragonblocks.settings.inventory.scale * 1.1 * this.columns + (dragonblocks.settings.inventory.scale * 0.1);
96         }
97
98         calculateHeight()
99         {
100                 return dragonblocks.settings.inventory.scale * 1.1 * Math.ceil(this.slots / this.columns) + dragonblocks.settings.inventory.scale * 0.1
101         }
102
103         draw(parent, x, y)
104         {
105                 if (this.display) {
106                         let display = this.getDisplay();
107                         if (display.parentElement == parent) {
108                                 display.style.left = x + "px";
109                                 display.style.top = y + "px";
110                                 return false;
111                         } else {
112                                 this.remove();
113                         }
114                 }
115
116                 this.display = true;
117
118                 let display = parent.appendChild(document.createElement("div"));
119                 display.id = "dragonblocks.inventory[" + this.id + "]";
120                 display.style.position = "absolute";
121                 display.style.left = x + "px";
122                 display.style.top = y + "px";
123                 display.style.width =  this.calculateWidth() + "px";
124                 display.style.height = this.calculateHeight() + "px";
125
126                 let scale = dragonblocks.settings.inventory.scale * 1.1;
127                 let offset = dragonblocks.settings.inventory.scale * 0.1;
128
129                 for (let i in this.list) {
130                         let x = i % this.columns;
131                         let y = (i - x) / this.columns;
132                         this.list[i].draw(display, offset + x * scale, offset + y * scale);
133                 }
134
135                 return true;
136         }
137
138         remove()
139         {
140                 this.getDisplay().remove();
141                 this.display = false;
142         }
143
144         show()
145         {
146                 this.getDisplay().style.visibility = "inherit";
147                 this.update();
148         }
149
150         hide()
151         {
152                 this.getDisplay().style.visibility = "hidden";
153         }
154
155         update()
156         {
157                 for (let stack of this.list)
158                         stack.update();
159         }
160
161         getSlot(i)
162         {
163                 return this.list[i];
164         }
165
166         getDisplay()
167         {
168                 return document.getElementById("dragonblocks.inventory[" + this.id + "]");
169         }
170 };
171
172 dragonblocks.Inventory.Event = class extends Event
173 {
174         constructor(type, stack)
175         {
176                 super(type);
177                 this.stack = stack;
178         }
179 };