]> git.lizzy.rs Git - dragonblocks.git/blob - game/furnace/inventory.js
c70864a0e3f411b96ae3bde1eb4f7007da5d6ee8
[dragonblocks.git] / game / furnace / inventory.js
1 furnace.Inventory = class extends dragonblocks.InventoryContainer{
2         constructor(){
3                 super({
4                         inventory: new dragonblocks.Inventory(4, 2),
5                         top: 1,
6                         bottom: 1,
7                         left: 4,
8                         right: 2,
9                 });
10                 let self = this;
11                 this.input = new dragonblocks.Itemstack();
12                 this.input.addUpdateListener(_ => {
13                         self.update();
14                 });
15                 this.fuel = new dragonblocks.Itemstack();
16                 this.fuel.addUpdateListener(_ => {
17                         self.update();
18                 });
19                 this.burnProgressDisplay = new dragonblocks.Itemstack();
20                 this.burnProgressDisplay.parse("furnace:burn_progress_0");
21                 this.burnProgressDisplay.action = _ => {};
22                 this.burnProgressDisplay.onredraw = _ => {
23                         dragonblocks.Inventory.getStackDisplay(self.burnProgressDisplay.id).style.backgroundColor = "";
24                         dragonblocks.Inventory.getStackDisplay(self.burnProgressDisplay.id).style.border = "none";
25                 };
26                 this.fuelProgressDisplay = new dragonblocks.Itemstack();
27                 this.fuelProgressDisplay.parse("furnace:fuel_progress_0");
28                 this.fuelProgressDisplay.action = _ => {};
29                 this.fuelProgressDisplay.onredraw = _ => {
30                         dragonblocks.Inventory.getStackDisplay(self.fuelProgressDisplay.id).style.backgroundColor = "";
31                         dragonblocks.Inventory.getStackDisplay(self.fuelProgressDisplay.id).style.border = "none";
32                 };
33                 this.clear();
34                 this.clearFuel();
35                 this.update();
36         }
37         draw(parent, x, y){
38                 if(! super.draw(parent, x, y))
39                         return false;
40                 dragonblocks.Inventory.drawStack(this.getDisplay(), 2 * dragonblocks.settings.inventory.scale * 1.1, 0.5 * dragonblocks.settings.inventory.scale * 1.1, this.input);
41                 dragonblocks.Inventory.drawStack(this.getDisplay(), 3 * dragonblocks.settings.inventory.scale * 1.1, 1.5 * dragonblocks.settings.inventory.scale * 1.1, this.burnProgressDisplay);
42                 dragonblocks.Inventory.drawStack(this.getDisplay(), 2 * dragonblocks.settings.inventory.scale * 1.1, 1.5 * dragonblocks.settings.inventory.scale * 1.1, this.fuelProgressDisplay);
43                 dragonblocks.Inventory.drawStack(this.getDisplay(), 2 * dragonblocks.settings.inventory.scale * 1.1, 2.5 * dragonblocks.settings.inventory.scale * 1.1, this.fuel);
44                 return true;
45         }
46         isEmpty(){
47                 return this.inventory.isEmpty() && ! this.fuel.item && ! this.input.item;
48         }
49         parse(str){
50                 let obj = JSON.parse(str);
51                 this.inventory.parse(obj.inventory);
52                 this.input.parse(obj.input);
53                 this.fuel.parse(obj.fuel);
54         }
55         stringify(){
56                 return JSON.stringify({
57                         inventory: this.inventory.stringify(),
58                         input: this.input.stringify(),
59                         fuel: this.fuel.stringify()
60                 });
61         }
62         getRecipe(){
63                 for(let recipe of furnace.recipes)
64                         if(dragonblocks.itemMatch(recipe.input, this.input.item))
65                                 return recipe;
66         }
67         getRecipeOutput(){
68                 return this.getRecipe() && this.getRecipe().output;
69         }
70         getRecipeTime(){
71                 return this.getRecipe() && this.getRecipe().time || 1;
72         }
73         clear(){
74                 this.burnProgress = 0;
75                 clearInterval(this.burnInterval);
76                 this.burning = false;
77         }
78         clearFuel(){
79                 this.fuelPower = this.fullFuelPower = 0;
80                 clearInterval(this.fuelInterval);
81                 this.fuelBurning = false;
82         }
83         update(){
84                 super.update();
85                 let self = this;
86                 if(! this.getRecipeOutput())
87                         this.clear();
88                 else if(this.burnProgress > this.getRecipeTime()){
89                         this.inventory.add(this.getRecipeOutput())
90                         this.clear();
91                         this.input.count--;
92                         this.input.update();
93                 }
94                 else if(! this.burning){
95                         this.burnInterval = setInterval(_ => {
96                                 self.burnProgress++;
97                                 self.update();
98                         }, 1000);
99                         this.burning = true;
100                 }
101                 if(this.fuelBurning && this.fuelPower <= 0)
102                         this.clearFuel();
103                 if(! this.fuelBurning && this.burning){
104                         if(this.fuel.toItem() && this.fuel.toItem().flammable){
105                                 this.fuelBurning = true;
106                                 this.fullFuelPower = this.fuelPower = this.fuel.toItem().hardness;
107                                 this.fuelInterval = setInterval(_ => {
108                                         self.fuelPower--;
109                                         self.update();
110                                 }, 1000);
111                                 this.fuel.count--;
112                                 this.fuel.update();
113                         }
114                         else
115                                 this.clear();
116                 }
117                 this.burnProgressDisplay.parse("furnace:burn_progress_" + parseInt(this.burnProgress / this.getRecipeTime() * 5));
118                 this.fuelProgressDisplay.parse("furnace:fuel_progress_" + (parseInt(this.fuelPower / this.fullFuelPower * 5) || 0));
119         }
120 }