]> git.lizzy.rs Git - dragonblocks.git/blob - engine/inventory.js
Implement per-map sky
[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
30                 this.slots = slots;
31                 this.columns = columns;
32                 this.list = [];
33
34                 let self = this;
35
36                 for (let i = 0; i < this.slots; i++){
37                         let stack = this.list[i] = new dragonblocks.ItemStack();
38                         stack.addEventListener("update", event => {
39                                 self.dispatchEvent(new dragonblocks.Inventory.Event("updateStack", event.stack));
40                         });
41                 }
42
43                 this.display = false;
44         }
45
46         serialize()
47         {
48                 let str = "";
49
50                 for (let stack of this.list)
51                         str += stack.serialize() + ",";
52
53                 return str;
54         }
55
56         deserialize(str)
57         {
58                 let strList = str.split(",");
59
60                 for (let i in this.list)
61                         this.list[i].deserialize(strList[i]);
62         }
63
64         add(itemstring)
65         {
66                 let itemstack = new dragonblocks.ItemStack(itemstring);
67
68                 for (let stack of this.list)
69                         stack.item == itemstack.item && stack.add(itemstack);
70
71                 for (let stack of this.list)
72                         stack.add(itemstack);
73
74                 return itemstack;
75         }
76
77         isEmpty()
78         {
79                 for (let stack of this.list)
80                         if (stack.item)
81                                 return false;
82
83                 return true;
84         }
85
86         clear()
87         {
88                 for(let stack of this.list)
89                         stack.clear();
90         }
91
92         calculateWidth(columns)
93         {
94                 return dragonblocks.settings.inventory.scale * 1.1 * this.columns + (dragonblocks.settings.inventory.scale * 0.1);
95         }
96
97         calculateHeight()
98         {
99                 return dragonblocks.settings.inventory.scale * 1.1 * Math.ceil(this.slots / this.columns) + dragonblocks.settings.inventory.scale * 0.1
100         }
101
102         draw(parent, x, y)
103         {
104                 if (! this.display)
105                         this.initGraphics();
106
107                 if (this.display.parentElement != parent)
108                         this.display = parent.appendChild(this.display);
109
110                 this.display.style.left = x + "px";
111                 this.display.style.top = y + "px";
112
113                 this.update();
114         }
115
116         remove()
117         {
118                 this.display.remove();
119         }
120
121         update()
122         {
123                 for (let stack of this.list)
124                         stack.update();
125         }
126
127         getSlot(i)
128         {
129                 return this.list[i];
130         }
131
132         initGraphics()
133         {
134                 this.display = document.createElement("div");
135                 this.display.style.position = "absolute";
136                 this.display.style.width =  this.calculateWidth() + "px";
137                 this.display.style.height = this.calculateHeight() + "px";
138
139                 let scale = dragonblocks.settings.inventory.scale * 1.1;
140                 let offset = dragonblocks.settings.inventory.scale * 0.1;
141
142                 for (let i in this.list) {
143                         let x = i % this.columns;
144                         let y = (i - x) / this.columns;
145                         this.list[i].draw(this.display, offset + x * scale, offset + y * scale);
146                 }
147         }
148 };
149
150 dragonblocks.Inventory.Event = class extends Event
151 {
152         constructor(type, stack)
153         {
154                 super(type);
155                 this.stack = stack;
156         }
157 };