]> git.lizzy.rs Git - dragonblocks.git/commitdiff
Rework inventory rendering
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 30 Jun 2021 12:12:35 +0000 (14:12 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 30 Jun 2021 12:12:35 +0000 (14:12 +0200)
12 files changed:
engine/craftfield.js
engine/creative_inventory.js
engine/inventory.js
engine/inventory_container.js
engine/inventory_group.js
engine/item_stack.js
engine/out_stack.js
engine/player.js
game/furnace/api.js
game/furnace/init.js
game/furnace/inventory.js
game/furnace/itemdef.js

index 38a981bd2b34576795b0d99a40b00f915d8fc19f..3f331acbeef4be5d1ca6e4f780136b24d7b4ea25 100644 (file)
@@ -35,10 +35,10 @@ dragonblocks.Craftfield = class extends dragonblocks.Inventory
                let self = this;
                this.resultfield.action = out => {
                        out.add(self.resultfield) && self.reduce();
-               }
+               };
 
                this.addEventListener("updateStack", _ => {
-                       self.update();
+                       self.updateResult();
                });
        }
 
@@ -47,24 +47,13 @@ dragonblocks.Craftfield = class extends dragonblocks.Inventory
                return super.calculateWidth() + dragonblocks.settings.inventory.scale * 1.1 * 2;
        }
 
-       draw(parent, x, y)
-       {
-               if (! super.draw(parent, x, y))
-                       return false;
-
-               this.resultfield.draw(this.getDisplay(), dragonblocks.settings.inventory.scale * 0.1 + (this.width + 1) * dragonblocks.settings.inventory.scale * 1.1, dragonblocks.settings.inventory.scale * 0.1 + (this.height / 2 - 0.5) * dragonblocks.settings.inventory.scale * 1.1);
-       }
-
        reduce()
        {
-               for (let stack of this.list) {
-                       let vstack = new dragonblocks.ItemStack();
-                       vstack.addOne(stack);
-               }
-               this.update();
+               for (let stack of this.list)
+                       new dragonblocks.ItemStack().addOne(stack);
        }
 
-       update()
+       updateResult()
        {
                this.resultfield.deserialize("");
 
@@ -73,4 +62,11 @@ dragonblocks.Craftfield = class extends dragonblocks.Inventory
                                return this.resultfield.deserialize(recipe.result);
                }
        }
+
+       initGraphics()
+       {
+               super.initGraphics();
+
+               this.resultfield.draw(this.display, dragonblocks.settings.inventory.scale * 0.1 + (this.width + 1) * dragonblocks.settings.inventory.scale * 1.1, dragonblocks.settings.inventory.scale * 0.1 + (this.height / 2 - 0.5) * dragonblocks.settings.inventory.scale * 1.1);
+       }
 };
index e94490dfae7d9efce0c5ec237a452b96e99e1537..7b69f5a4e0870fa8b25e5a9aa75d03dda0c49c5e 100644 (file)
@@ -36,6 +36,7 @@ dragonblocks.CreativeInventory = class extends dragonblocks.Inventory
 
                for (let i = 0; i < this.slots; i++) {
                        let stack = this.list[i];
+
                        stack.addEventListener("update", event => {
                                if (event.stack.refilling)
                                        return;
@@ -52,38 +53,45 @@ dragonblocks.CreativeInventory = class extends dragonblocks.Inventory
                return super.calculateHeight() + dragonblocks.settings.inventory.scale;
        }
 
-       draw(parent, x, y)
+       update()
+       {
+               if (this.page == -1)
+                       this.page++;
+
+               if (this.page == this.pages)
+                       this.page--;
+
+               this.pageDisplay.textContent = "Page " + (this.page + 1) + " of " + this.pages;
+
+               for (let slot of this.list)
+                       slot.update();
+       }
+
+       initGraphics()
        {
-               if (! super.draw(parent, x, y))
-                       return false;
-
-               let display = this.getDisplay();
-               display.style.height = this.calculateHeight();
-
-               let creativeDisplay = display.appendChild(document.createElement("div"));
-               creativeDisplay.id = "dragonblocks.inventory[" + this.id + "].creative";
-               creativeDisplay.style.height = dragonblocks.settings.inventory.scale + "px";
-               creativeDisplay.style.width = this.calculateWidth() + "px";
-               creativeDisplay.style.left = "0px";
-               creativeDisplay.style.top = super.calculateHeight() + "px";
-               creativeDisplay.style.position = "absolute";
-
-               let pageDisplay = creativeDisplay.appendChild(document.createElement("span"));
-               pageDisplay.id = "dragonblocks.inventory[" + this.id + "].creative.page";
-               pageDisplay.style.color = "#343434";
-               pageDisplay.style.position = "absolute";
-               pageDisplay.style.left = dragonblocks.settings.inventory.scale * 1.1 + "px";
-               pageDisplay.style.width = "100%";
-               pageDisplay.style.fontSize = dragonblocks.settings.inventory.scale / (5 / 3) + "px";
-               pageDisplay.style.height = dragonblocks.settings.inventory.scale / (5 / 3) + "px";
-
-               dblib.centerVertical(pageDisplay);
+               super.initGraphics();
+
+               let pageContainer = this.display.appendChild(document.createElement("div"));
+               pageContainer.style.height = dragonblocks.settings.inventory.scale + "px";
+               pageContainer.style.width = this.calculateWidth() + "px";
+               pageContainer.style.left = "0px";
+               pageContainer.style.top = super.calculateHeight() + "px";
+               pageContainer.style.position = "absolute";
+
+               this.pageDisplay = pageContainer.appendChild(document.createElement("span"));
+               this.pageDisplay.style.color = "#343434";
+               this.pageDisplay.style.position = "absolute";
+               this.pageDisplay.style.left = dragonblocks.settings.inventory.scale * 1.1 + "px";
+               this.pageDisplay.style.width = "100%";
+               this.pageDisplay.style.fontSize = dragonblocks.settings.inventory.scale / (5 / 3) + "px";
+               this.pageDisplay.style.height = dragonblocks.settings.inventory.scale / (5 / 3) + "px";
+
+               dblib.centerVertical(this.pageDisplay);
 
                let self = this;
 
                for (let dir of ["left", "right"]) {
-                       let arrow = creativeDisplay.appendChild(document.createElement("div"));
-                       arrow.id = "dragonblocks.inventory[" + this.id + "].creative.arrow." + dir;
+                       let arrow = pageContainer.appendChild(document.createElement("div"));
                        arrow.style.position = "absolute";
                        arrow.style.width = dragonblocks.settings.inventory.scale + "px";
                        arrow.style.height = dragonblocks.settings.inventory.scale + "px";
@@ -95,30 +103,15 @@ dragonblocks.CreativeInventory = class extends dragonblocks.Inventory
                                arrow.style.transform = "rotate(180deg)";
 
                        arrow.addEventListener("click", _ => {
-                               if(dir == "right")
+                               if (dir == "right")
                                        self.page++;
                                else
                                        self.page--;
+
                                self.update();
                        });
 
                        dblib.centerVertical(arrow);
                }
-
-               this.update();
-       }
-
-       update()
-       {
-               if (this.page == -1)
-                       this.page++;
-
-               if (this.page == this.pages)
-                       this.page--;
-
-               document.getElementById("dragonblocks.inventory[" + this.id + "].creative.page").textContent = "Page " + (this.page + 1) + " of " + this.pages;
-
-               for (let slot of this.list)
-                       slot.update();
        }
 };
index b7142458c6f5455e8ace485fe38bb5608d83c867..d6e5b2e9e9ccd88eb576021f0b6c58db21936110 100644 (file)
@@ -26,7 +26,6 @@ dragonblocks.Inventory = class extends EventTarget
        constructor(slots, columns)
        {
                super();
-               this.id = dragonblocks.getToken();
 
                this.slots = slots;
                this.columns = columns;
@@ -102,54 +101,21 @@ dragonblocks.Inventory = class extends EventTarget
 
        draw(parent, x, y)
        {
-               if (this.display) {
-                       let display = this.getDisplay();
-                       if (display.parentElement == parent) {
-                               display.style.left = x + "px";
-                               display.style.top = y + "px";
-                               return false;
-                       } else {
-                               this.remove();
-                       }
-               }
-
-               this.display = true;
-
-               let display = parent.appendChild(document.createElement("div"));
-               display.id = "dragonblocks.inventory[" + this.id + "]";
-               display.style.position = "absolute";
-               display.style.left = x + "px";
-               display.style.top = y + "px";
-               display.style.width =  this.calculateWidth() + "px";
-               display.style.height = this.calculateHeight() + "px";
-
-               let scale = dragonblocks.settings.inventory.scale * 1.1;
-               let offset = dragonblocks.settings.inventory.scale * 0.1;
+               if (! this.display)
+                       this.initGraphics();
 
-               for (let i in this.list) {
-                       let x = i % this.columns;
-                       let y = (i - x) / this.columns;
-                       this.list[i].draw(display, offset + x * scale, offset + y * scale);
-               }
-
-               return true;
-       }
+               if (this.display.parentElement != parent)
+                       this.display = parent.appendChild(this.display);
 
-       remove()
-       {
-               this.getDisplay().remove();
-               this.display = false;
-       }
+               this.display.style.left = x + "px";
+               this.display.style.top = y + "px";
 
-       show()
-       {
-               this.getDisplay().style.visibility = "inherit";
                this.update();
        }
 
-       hide()
+       remove()
        {
-               this.getDisplay().style.visibility = "hidden";
+               this.display.remove();
        }
 
        update()
@@ -163,9 +129,21 @@ dragonblocks.Inventory = class extends EventTarget
                return this.list[i];
        }
 
-       getDisplay()
+       initGraphics()
        {
-               return document.getElementById("dragonblocks.inventory[" + this.id + "]");
+               this.display = document.createElement("div");
+               this.display.style.position = "absolute";
+               this.display.style.width =  this.calculateWidth() + "px";
+               this.display.style.height = this.calculateHeight() + "px";
+
+               let scale = dragonblocks.settings.inventory.scale * 1.1;
+               let offset = dragonblocks.settings.inventory.scale * 0.1;
+
+               for (let i in this.list) {
+                       let x = i % this.columns;
+                       let y = (i - x) / this.columns;
+                       this.list[i].draw(this.display, offset + x * scale, offset + y * scale);
+               }
        }
 };
 
index f3d5c2d8cbf779acfb5b5183d52ff65f89896544..525c43bf0782206edc3fca741b3e6624b4c04589 100644 (file)
@@ -31,54 +31,21 @@ dragonblocks.InventoryContainer = class extends EventTarget
 
        draw(parent, x, y)
        {
-               if (this.display) {
-                       let display = this.getDisplay();
-                       if (display.parentElement == parent) {
-                               display.style.left = x + "px";
-                               display.style.top = y + "px";
-                               return false;
-                       } else {
-                               this.remove();
-                       }
-               }
+               if (! this.display)
+                       this.initGraphics();
 
-               let display = parent.appendChild(document.createElement("div"));
-               display.id = "dragonblocks.inventoryContainer[" + this.inventory.id + "]";
-               display.style.width = this.calculateWidth() + "px";
-               display.style.height = this.calculateHeight() + "px";
-               display.style.left = x + "px";
-               display.style.top = y + "px";
+               if (this.display.parentElement != parent)
+                       this.display = parent.appendChild(this.display);
 
-               this.inventory.draw(display, dragonblocks.settings.inventory.scale * 1.1 * this.left, dragonblocks.settings.inventory.scale * 1.1 * this.top);
+               this.display.style.left = x + "px";
+               this.display.style.top = y + "px";
 
-               this.display = true;
-               return true;
-       }
-
-       serialize()
-       {
-               return this.inventory.serialize();
-       }
-
-       deserialize(str)
-       {
-               this.inventory.deserialize(str);
+               this.inventory.draw(this.display, dragonblocks.settings.inventory.scale * 1.1 * this.left, dragonblocks.settings.inventory.scale * 1.1 * this.top);
        }
 
        remove()
        {
-               this.getDisplay().remove();
-       }
-
-       show()
-       {
-               this.getDisplay().style.visibility = "inherit";
-               this.update();
-       }
-
-       hide()
-       {
-               this.getDisplay().style.visibility = "hidden";
+               this.display.remove();
        }
 
        calculateWidth()
@@ -91,13 +58,10 @@ dragonblocks.InventoryContainer = class extends EventTarget
                return this.inventory.calculateHeight() + dragonblocks.settings.inventory.scale * 1.1 * (this.top + this.bottom);
        }
 
-       getDisplay()
-       {
-               return document.getElementById("dragonblocks.inventoryContainer[" + this.inventory.id + "]");
-       }
-
-       update()
+       initGraphics()
        {
-               this.inventory.update();
+               this.display = document.createElement("div");
+               this.display.style.width = this.calculateWidth() + "px";
+               this.display.style.height = this.calculateHeight() + "px";
        }
 };
index 098f993a60a99940b666f43905e059aed278f152..909bbe472c4755eb6cd8e5f066d859279a44cd51 100644 (file)
@@ -25,37 +25,33 @@ dragonblocks.InventoryGroup = class
 {
        constructor()
        {
-               this.id = dragonblocks.getToken();
-
-               this._elements = [];
+               this.elements = [];
                this.opened = false;
 
-               let display = dragonblocks.addInventoryMenuDisplay(document.createElement("div"));
-               display.id = "dragonblocks.inventoryGroup[" + this.id + "]";
-               display.style.position = "fixed";
-               display.style.backgroundColor = "#535353";
-               display.style.visibility = "hidden";
+               this.display = dragonblocks.addInventoryMenuDisplay(document.createElement("div"));
+               this.display.style.position = "fixed";
+               this.display.style.backgroundColor = "#535353";
+               this.display.style.visibility = "hidden";
        }
 
        close()
        {
                this.opened = false;
 
-               document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]").style.visibility = "hidden";
-               dragonblocks.outStack.getDisplay().style.visibility = "hidden";
-
-               if(this.onNextClose)
-                       this.onNextClose();
+               this.display.style.visibility = "hidden";
+               dragonblocks.outStack.display.style.visibility = "hidden";
 
-               this.onNextClose = null;
+               this.onNextClose = this.onNextClose && this.onNextClose();
        }
 
        open()
        {
                this.opened = true;
 
-               document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]").style.visibility = "inherit";
-               dragonblocks.outStack.getDisplay().style.visibility = "visible";
+               this.display.style.visibility = "inherit";
+               dragonblocks.outStack.display.style.visibility = "visible";
+
+               this.update();
        }
 
        toggle()
@@ -63,34 +59,31 @@ dragonblocks.InventoryGroup = class
                this.opened ? this.close() : this.open();
        }
 
-       set elements(elements)
+       update()
        {
-               for (let element of this.elements)
-                       element.hide();
-
-               this._elements = elements;
-
-               let container = document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]");
-
                let height = 0;
                let width = 0;
 
                for (let element of this.elements) {
-                       element.draw(container, 0, height);
+                       element.draw(this.display, 0, height);
                        height += element.calculateHeight();
                        width = Math.max(width, element.calculateWidth());
-                       element.show();
                }
 
-               container.style.width = width + "px";
-               container.style.height = height + "px";
+               this.display.style.width = width + "px";
+               this.display.style.height = height + "px";
 
-               dblib.center(container);
-               dblib.centerVertical(container);
+               dblib.center(this.display);
+               dblib.centerVertical(this.display);
        }
 
-       get elements()
+       setElements(elements)
        {
-               return this._elements;
+               for (let element of this.elements)
+                       element.remove();
+
+               this.elements = elements;
+
+               this.update();
        }
 };
index 5024bb388ed019cc5c84bef86512c2c54e0cfef6..53ad772bff9f5384511e8628825bba288bf69ef2 100644 (file)
@@ -29,7 +29,6 @@ dragonblocks.ItemStack = class extends EventTarget
 
                this.count = 0;
                this.item = null;
-               this.id = dragonblocks.getToken();
 
                if (itemstring)
                        this.deserialize(itemstring);
@@ -143,38 +142,29 @@ dragonblocks.ItemStack = class extends EventTarget
                return this.addItems(itemstack, Math.ceil(itemstack.count / 2));
        }
 
-       getDisplay()
-       {
-               return document.getElementById("dragonblocks.itemstack[" + this.id + "]");
-       }
-
        draw(parent, x, y)
        {
-               let display = parent.appendChild(document.createElement("div"));
-               display.id = "dragonblocks.itemstack[" + this.id + "]";
-               display.stackid = this.id;
-               display.style.borderStyle = "solid";
-               display.style.borderWidth = "1px";
-               display.style.borderColor = "#2D2D2D";
-               display.style.width = dragonblocks.settings.inventory.scale + "px";
-               display.style.height = dragonblocks.settings.inventory.scale + "px";
-               display.style.backgroundColor = "#343434";
-               display.style.position = "absolute";
-               display.style.left = x + "px";
-               display.style.top = y + "px";
-
-               let countDisplay = display.appendChild(document.createElement("span"));
-               countDisplay.id = "dragonblocks.itemstack[" + this.id + "].count";
-               countDisplay.stackid = this.id;
-               countDisplay.style.position = "absolute";
-               countDisplay.style.right = "5px";
-               countDisplay.style.bottom = "5px";
-               countDisplay.style.color = "white";
-               countDisplay.style.cursor = "default";
+               this.display = parent.appendChild(document.createElement("div"));
+               this.display.style.borderStyle = "solid";
+               this.display.style.borderWidth = "1px";
+               this.display.style.borderColor = "#2D2D2D";
+               this.display.style.width = dragonblocks.settings.inventory.scale + "px";
+               this.display.style.height = dragonblocks.settings.inventory.scale + "px";
+               this.display.style.backgroundColor = "#343434";
+               this.display.style.position = "absolute";
+               this.display.style.left = x + "px";
+               this.display.style.top = y + "px";
+
+               this.countDisplay = this.display.appendChild(document.createElement("span"));
+               this.countDisplay.style.position = "absolute";
+               this.countDisplay.style.right = "5px";
+               this.countDisplay.style.bottom = "5px";
+               this.countDisplay.style.color = "white";
+               this.countDisplay.style.cursor = "default";
 
                let self = this;
 
-               display.addEventListener("mousedown", event => {
+               this.display.addEventListener("mousedown", event => {
                        let out = dragonblocks.outStack;
 
                        if (self.action)
@@ -196,12 +186,12 @@ dragonblocks.ItemStack = class extends EventTarget
                        }
                });
 
-               display.addEventListener("mouseover", event => {
+               this.display.addEventListener("mouseover", event => {
                        self.focused = true;
                        self.redraw();
                });
 
-               display.addEventListener("mouseleave", event => {
+               this.display.addEventListener("mouseleave", event => {
                        self.focused = false;
                        self.redraw();
                });
@@ -215,31 +205,27 @@ dragonblocks.ItemStack = class extends EventTarget
 
        redraw()
        {
-               let display = this.getDisplay();
-
-               if (! display)
+               if (! this.display)
                        return;
 
-               let countDisplay = document.getElementById("dragonblocks.itemstack[" + this.id + "].count");
-
                if (this.item) {
                        let item = this.toItem();
 
-                       display.title = item.desc;
-                       display.style.background = dragonblocks.getTexture(item.texture);
+                       this.display.title = item.desc;
+                       this.display.style.background = dragonblocks.getTexture(item.texture);
 
                        if (this.count > 1)
-                               countDisplay.innerHTML = this.count;
+                               this.countDisplay.innerHTML = this.count;
                        else
-                               countDisplay.innerHTML = "";
+                               this.countDisplay.innerHTML = "";
                } else {
-                       display.title = "";
-                       display.style.background = "none";
+                       this.display.title = "";
+                       this.display.style.background = "none";
 
-                       countDisplay.innerHTML = "";
+                       this.countDisplay.innerHTML = "";
                }
 
-               display.style.backgroundColor = this.focused ? "#7E7E7E" : "#343434";
+               this.display.style.backgroundColor = this.focused ? "#7E7E7E" : "#343434";
 
                this.trigger("redraw");
        }
index 3a5380ff69ac68ecc7fb382db62266a1e4f641c8..00c12a71df69165408b5b53959accce86da164c3 100644 (file)
  */
 
 dragonblocks.addInventoryMenuDisplay = elem => {
-       return document.body.insertBefore(elem, dragonblocks.outStack.getDisplay());
+       return document.body.insertBefore(elem, dragonblocks.outStack.display);
 };
 
 setTimeout(_ => {
        let out = dragonblocks.outStack = new dragonblocks.ItemStack();
 
        out.draw(document.body, 0, 0);
-       out.getDisplay().style.position = "fixed";
+       out.display.style.position = "fixed";
 
        out.addEventListener("redraw", _ => {
-               let display = out.getDisplay();
-               display.style.backgroundColor = "";
-               display.style.border = "none";
+               out.display.style.backgroundColor = "";
+               out.display.style.border = "none";
        });
 
        addEventListener("mousemove", event => {
-               let display = out.getDisplay();
-               display.style.left = event.clientX + 5 + "px";
-               display.style.top = event.clientY + 5 + "px";
+               out.display.style.left = event.clientX + 5 + "px";
+               out.display.style.top = event.clientY + 5 + "px";
        });
 
        out.update();
index 02155e283de685f9bb71837127bc126bab01ffdb..344c315c0e2361004dea22dee1477be27241b3d3 100644 (file)
@@ -345,14 +345,12 @@ dragonblocks.Player = class extends dragonblocks.SpawnedEntity
 
        setInventoryElements(elems)
        {
-               this.tmp.inventory.elements = elems;
+               this.tmp.inventory.setElements(elems);
        }
 
        resetInventoryElements()
        {
-               let elems = [this.tmp.mainInventory];
-               elems.unshift(this.gamemode == "creative" ? this.tmp.creativeInventory : this.tmp.survivalInventory);
-               this.setInventoryElements(elems);
+               this.setInventoryElements([this.gamemode == "creative" ? this.tmp.creativeInventory : this.tmp.survivalInventory, this.tmp.mainInventory]);
        }
 
        previousItem()
index a2d757bc22a985a9546e7cfc5c510d6cacf676ec..f04a3f05614d66cacc68aae744521de9bd9071b9 100644 (file)
@@ -1,6 +1,8 @@
 furnace.recipes = [];
-furnace.registerRecipe = function(obj){
-       if(! obj || ! obj.input || ! obj.output || ! obj.time)
+
+furnace.registerRecipe = def => {
+       if (! def || ! def.input || ! def.output || ! def.time)
                return;
-       furnace.recipes.push(obj);
-}
+
+       furnace.recipes.push(def);
+};
index 090ca77575a5f8b87701d9c2ac85a20e3ae99421..d396712210c4b25f56bb247a73ae9d93b79b3d03 100644 (file)
@@ -1,4 +1,7 @@
 furnace = {};
-$.getScript(dragonblocks.getModpath("furnace") + "/api.js");
-$.getScript(dragonblocks.getModpath("furnace") + "/inventory.js");
-$.getScript(dragonblocks.getModpath("furnace") + "/itemdef.js");
+
+let modpath = dragonblocks.getModpath("furnace");
+
+$.getScript(modpath + "/api.js");
+$.getScript(modpath + "/inventory.js");
+$.getScript(modpath + "/itemdef.js");
index d28ba3b4a8aa257406f1a57212625e9a3c032e00..409652e958a4e24d48db4e990bcd2a455cbc673e 100644 (file)
@@ -1,5 +1,7 @@
-furnace.Inventory = class extends dragonblocks.InventoryContainer{
-       constructor(){
+furnace.Inventory = class extends dragonblocks.InventoryContainer
+{
+       constructor()
+       {
                super({
                        inventory: new dragonblocks.Inventory(4, 2),
                        top: 1,
@@ -7,114 +9,147 @@ furnace.Inventory = class extends dragonblocks.InventoryContainer{
                        left: 4,
                        right: 2,
                });
+
                let self = this;
+
                this.input = new dragonblocks.ItemStack();
                this.input.addEventListener("update", _ => {
                        self.update();
                });
+
                this.fuel = new dragonblocks.ItemStack();
                this.fuel.addEventListener("update", _ => {
                        self.update();
                });
-               this.burnProgressDisplay = new dragonblocks.ItemStack();
-               this.burnProgressDisplay.deserialize("furnace:burn_progress_0");
-               this.burnProgressDisplay.action = _ => {};
-               this.burnProgressDisplay.addEventListener("redraw", _ => {
-                       dragonblocks.Inventory.getStackDisplay(self.burnProgressDisplay.id).style.backgroundColor = "";
-                       dragonblocks.Inventory.getStackDisplay(self.burnProgressDisplay.id).style.border = "none";
+
+               this.burnProgressStack = new dragonblocks.ItemStack();
+               this.burnProgressStack.deserialize("furnace:burn_progress_0");
+               this.burnProgressStack.action = _ => {};
+               this.burnProgressStack.addEventListener("redraw", event => {
+                       event.stack.display.style.backgroundColor = "";
+                       event.stack.display.style.border = "none";
                });
-               this.fuelProgressDisplay = new dragonblocks.ItemStack();
-               this.fuelProgressDisplay.deserialize("furnace:fuel_progress_0");
-               this.fuelProgressDisplay.action = _ => {};
-               this.fuelProgressDisplay.addEventListener("redraw", _ => {
-                       dragonblocks.Inventory.getStackDisplay(self.fuelProgressDisplay.id).style.backgroundColor = "";
-                       dragonblocks.Inventory.getStackDisplay(self.fuelProgressDisplay.id).style.border = "none";
+
+               this.fuelProgressStack = new dragonblocks.ItemStack();
+               this.fuelProgressStack.deserialize("furnace:fuel_progress_0");
+               this.fuelProgressStack.action = _ => {};
+               this.fuelProgressStack.addEventListener("redraw", event => {
+                       event.stack.display.style.backgroundColor = "";
+                       event.stack.display.style.border = "none";
                });
+
                this.clear();
                this.clearFuel();
+
                this.update();
        }
-       draw(parent, x, y){
-               if(! super.draw(parent, x, y))
-                       return false;
-               this.input.draw(this.getDisplay(), 2 * dragonblocks.settings.inventory.scale * 1.1, 0.5 * dragonblocks.settings.inventory.scale * 1.1);
-               this.burnProgressDisplay.draw(this.getDisplay(), 3 * dragonblocks.settings.inventory.scale * 1.1, 1.5 * dragonblocks.settings.inventory.scale * 1.1);
-               this.fuelProgressDisplay.draw(this.getDisplay(), 2 * dragonblocks.settings.inventory.scale * 1.1, 1.5 * dragonblocks.settings.inventory.scale * 1.1);
-               this.fuel.draw(this.getDisplay(), 2 * dragonblocks.settings.inventory.scale * 1.1, 2.5 * dragonblocks.settings.inventory.scale * 1.1);
-               return true;
+
+       initGraphics()
+       {
+               super.initGraphics();
+
+               this.input.draw(this.display, 2 * dragonblocks.settings.inventory.scale * 1.1, 0.5 * dragonblocks.settings.inventory.scale * 1.1);
+               this.fuel.draw(this.display, 2 * dragonblocks.settings.inventory.scale * 1.1, 2.5 * dragonblocks.settings.inventory.scale * 1.1);
+               this.burnProgressStack.draw(this.display, 3 * dragonblocks.settings.inventory.scale * 1.1, 1.5 * dragonblocks.settings.inventory.scale * 1.1);
+               this.fuelProgressStack.draw(this.display, 2 * dragonblocks.settings.inventory.scale * 1.1, 1.5 * dragonblocks.settings.inventory.scale * 1.1);
        }
-       isEmpty(){
+
+       isEmpty()
+       {
                return this.inventory.isEmpty() && ! this.fuel.item && ! this.input.item;
        }
-       deserialize(str){
-               let obj = JSON.parse(str);
-               this.inventory.deserialize(obj.inventory);
-               this.input.deserialize(obj.input);
-               this.fuel.deserialize(obj.fuel);
+
+       deserialize(str)
+       {
+               let data = JSON.parse(str);
+
+               this.inventory.deserialize(data.inventory);
+               this.input.deserialize(data.input);
+               this.fuel.deserialize(data.fuel);
        }
-       serialize(){
+
+       serialize()
+       {
                return JSON.stringify({
                        inventory: this.inventory.serialize(),
                        input: this.input.serialize(),
-                       fuel: this.fuel.serializes()
+                       fuel: this.fuel.serialize()
                });
        }
-       getRecipe(){
-               for(let recipe of furnace.recipes)
-                       if(dragonblocks.itemMatch(recipe.input, this.input.item))
+
+       getRecipe()
+       {
+               for (let recipe of furnace.recipes)
+                       if (dragonblocks.itemMatch(recipe.input, this.input.item))
                                return recipe;
        }
-       getRecipeOutput(){
+
+       getRecipeOutput()
+       {
                return this.getRecipe() && this.getRecipe().output;
        }
-       getRecipeTime(){
+
+       getRecipeTime()
+       {
                return this.getRecipe() && this.getRecipe().time || 1;
        }
-       clear(){
+
+       clear()
+       {
                this.burnProgress = 0;
                clearInterval(this.burnInterval);
                this.burning = false;
        }
-       clearFuel(){
+
+       clearFuel()
+       {
                this.fuelPower = this.fullFuelPower = 0;
                clearInterval(this.fuelInterval);
                this.fuelBurning = false;
        }
-       update(){
-               super.update();
+
+       update()
+       {
                let self = this;
-               if(! this.getRecipeOutput())
+
+               if (! this.getRecipeOutput()) {
                        this.clear();
-               else if(this.burnProgress > this.getRecipeTime()){
-                       this.inventory.add(this.getRecipeOutput())
+               } else if (this.burnProgress > this.getRecipeTime()) {
+                       this.inventory.add(this.getRecipeOutput());
                        this.clear();
+
                        this.input.count--;
                        this.input.update();
-               }
-               else if(! this.burning){
+               } else if (! this.burning) {
                        this.burnInterval = setInterval(_ => {
                                self.burnProgress++;
                                self.update();
                        }, 1000);
+
                        this.burning = true;
                }
-               if(this.fuelBurning && this.fuelPower <= 0)
+
+               if (this.fuelBurning && this.fuelPower <= 0)
                        this.clearFuel();
-               if(! this.fuelBurning && this.burning){
-                       if(this.fuel.toItem() && this.fuel.toItem().flammable){
+
+               if (! this.fuelBurning && this.burning) {
+                       if (this.fuel.toItem() && this.fuel.toItem().flammable) {
                                this.fuelBurning = true;
                                this.fullFuelPower = this.fuelPower = this.fuel.toItem().hardness;
+
                                this.fuelInterval = setInterval(_ => {
                                        self.fuelPower--;
                                        self.update();
                                }, 1000);
+
                                this.fuel.count--;
                                this.fuel.update();
-                       }
-                       else
+                       } else {
                                this.clear();
+                       }
                }
-               this.burnProgressDisplay.deserialize("furnace:burn_progress_" + parseInt(this.burnProgress / this.getRecipeTime() * 5));
-               this.fuelProgressDisplay.deserialize("furnace:fuel_progress_" + (parseInt(this.fuelPower / this.fullFuelPower * 5) || 0));
+
+               this.burnProgressStack.deserialize("furnace:burn_progress_" + parseInt(this.burnProgress / this.getRecipeTime() * 5));
+               this.fuelProgressStack.deserialize("furnace:fuel_progress_" + (parseInt(this.fuelPower / this.fullFuelPower * 5) || 0));
        }
 }
index 4c0135c6c284299ab9fbf436b102106f74fdbbed..837e6107973a73daa34c08c01f35d87a3d605a11 100644 (file)
@@ -7,14 +7,18 @@ dragonblocks.registerNode({
        stable: true,
        onset: (map, x, y) => {
                let meta = map.getNode(x, y).meta;
+
                meta.inventory = new furnace.Inventory();
-               if(meta.inventoryString)
-                       meta.inventory.ceserialize(meta.inventoryString);
+
+               if (meta.inventoryString)
+                       meta.inventory.deserialize(meta.inventoryString);
        },
        onclick: (map, x, y) => {
                let meta = map.getNode(x, y).meta;
+
                dragonblocks.player.setInventoryElements([meta.inventory, dragonblocks.player.tmp.mainInventory]);
                dragonblocks.player.openInventory();
+
                dragonblocks.player.onNextInventoryClose = _ => {
                        dragonblocks.player.resetInventoryElements();
                        meta.inventoryString = meta.inventory.serialize();
@@ -24,7 +28,8 @@ dragonblocks.registerNode({
                return map.getNode(x, y).meta.inventory.isEmpty();
        },
 });
-for(let i = 0; i < 6; i++){
+
+for (let i = 0; i < 6; i++) {
        dragonblocks.registerItem({
                name: "furnace:burn_progress_" + i,
                texture: "furnace_burn_progress_" + i + ".png",
@@ -32,6 +37,7 @@ for(let i = 0; i < 6; i++){
                hidden: true,
                desc: "",
        });
+
        dragonblocks.registerItem({
                name: "furnace:fuel_progress_" + i,
                texture: "furnace_fuel_progress_" + i + ".png",