]> git.lizzy.rs Git - dragonblocks.git/blob - game/furnace/inventory.js
Rework inventory rendering
[dragonblocks.git] / game / furnace / inventory.js
1 furnace.Inventory = class extends dragonblocks.InventoryContainer
2 {
3         constructor()
4         {
5                 super({
6                         inventory: new dragonblocks.Inventory(4, 2),
7                         top: 1,
8                         bottom: 1,
9                         left: 4,
10                         right: 2,
11                 });
12
13                 let self = this;
14
15                 this.input = new dragonblocks.ItemStack();
16                 this.input.addEventListener("update", _ => {
17                         self.update();
18                 });
19
20                 this.fuel = new dragonblocks.ItemStack();
21                 this.fuel.addEventListener("update", _ => {
22                         self.update();
23                 });
24
25                 this.burnProgressStack = new dragonblocks.ItemStack();
26                 this.burnProgressStack.deserialize("furnace:burn_progress_0");
27                 this.burnProgressStack.action = _ => {};
28                 this.burnProgressStack.addEventListener("redraw", event => {
29                         event.stack.display.style.backgroundColor = "";
30                         event.stack.display.style.border = "none";
31                 });
32
33                 this.fuelProgressStack = new dragonblocks.ItemStack();
34                 this.fuelProgressStack.deserialize("furnace:fuel_progress_0");
35                 this.fuelProgressStack.action = _ => {};
36                 this.fuelProgressStack.addEventListener("redraw", event => {
37                         event.stack.display.style.backgroundColor = "";
38                         event.stack.display.style.border = "none";
39                 });
40
41                 this.clear();
42                 this.clearFuel();
43
44                 this.update();
45         }
46
47         initGraphics()
48         {
49                 super.initGraphics();
50
51                 this.input.draw(this.display, 2 * dragonblocks.settings.inventory.scale * 1.1, 0.5 * dragonblocks.settings.inventory.scale * 1.1);
52                 this.fuel.draw(this.display, 2 * dragonblocks.settings.inventory.scale * 1.1, 2.5 * dragonblocks.settings.inventory.scale * 1.1);
53                 this.burnProgressStack.draw(this.display, 3 * dragonblocks.settings.inventory.scale * 1.1, 1.5 * dragonblocks.settings.inventory.scale * 1.1);
54                 this.fuelProgressStack.draw(this.display, 2 * dragonblocks.settings.inventory.scale * 1.1, 1.5 * dragonblocks.settings.inventory.scale * 1.1);
55         }
56
57         isEmpty()
58         {
59                 return this.inventory.isEmpty() && ! this.fuel.item && ! this.input.item;
60         }
61
62         deserialize(str)
63         {
64                 let data = JSON.parse(str);
65
66                 this.inventory.deserialize(data.inventory);
67                 this.input.deserialize(data.input);
68                 this.fuel.deserialize(data.fuel);
69         }
70
71         serialize()
72         {
73                 return JSON.stringify({
74                         inventory: this.inventory.serialize(),
75                         input: this.input.serialize(),
76                         fuel: this.fuel.serialize()
77                 });
78         }
79
80         getRecipe()
81         {
82                 for (let recipe of furnace.recipes)
83                         if (dragonblocks.itemMatch(recipe.input, this.input.item))
84                                 return recipe;
85         }
86
87         getRecipeOutput()
88         {
89                 return this.getRecipe() && this.getRecipe().output;
90         }
91
92         getRecipeTime()
93         {
94                 return this.getRecipe() && this.getRecipe().time || 1;
95         }
96
97         clear()
98         {
99                 this.burnProgress = 0;
100                 clearInterval(this.burnInterval);
101                 this.burning = false;
102         }
103
104         clearFuel()
105         {
106                 this.fuelPower = this.fullFuelPower = 0;
107                 clearInterval(this.fuelInterval);
108                 this.fuelBurning = false;
109         }
110
111         update()
112         {
113                 let self = this;
114
115                 if (! this.getRecipeOutput()) {
116                         this.clear();
117                 } else if (this.burnProgress > this.getRecipeTime()) {
118                         this.inventory.add(this.getRecipeOutput());
119                         this.clear();
120
121                         this.input.count--;
122                         this.input.update();
123                 } else if (! this.burning) {
124                         this.burnInterval = setInterval(_ => {
125                                 self.burnProgress++;
126                                 self.update();
127                         }, 1000);
128
129                         this.burning = true;
130                 }
131
132                 if (this.fuelBurning && this.fuelPower <= 0)
133                         this.clearFuel();
134
135                 if (! this.fuelBurning && this.burning) {
136                         if (this.fuel.toItem() && this.fuel.toItem().flammable) {
137                                 this.fuelBurning = true;
138                                 this.fullFuelPower = this.fuelPower = this.fuel.toItem().hardness;
139
140                                 this.fuelInterval = setInterval(_ => {
141                                         self.fuelPower--;
142                                         self.update();
143                                 }, 1000);
144
145                                 this.fuel.count--;
146                                 this.fuel.update();
147                         } else {
148                                 this.clear();
149                         }
150                 }
151
152                 this.burnProgressStack.deserialize("furnace:burn_progress_" + parseInt(this.burnProgress / this.getRecipeTime() * 5));
153                 this.fuelProgressStack.deserialize("furnace:fuel_progress_" + (parseInt(this.fuelPower / this.fullFuelPower * 5) || 0));
154         }
155 }