]> git.lizzy.rs Git - dragonblocks.git/blob - engine/hudbar.js
a28b3176b1c5f947ffec6ca3a8af104d470075db
[dragonblocks.git] / engine / hudbar.js
1 /*
2  * hudbar.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 dragonblocks.Hudbar = class{
24         constructor(inventory, slots){
25                 this.id = dragonblocks.getToken();
26                 this.inventory = inventory;
27                 this.slots = slots;
28                 this.selectedSlot = 0;
29                 let display = document.createElement("div");
30                 display.id = "dragonblocks.hudbar[" + this.id + "]";
31                 display.style.position = "fixed";
32                 display.style.bottom = "5px";
33                 display.style.height = "60px";
34                 display.style.width = "445px";
35                 for(let i = 0; i < this.slots; i++){
36                         let slotDisplay = document.createElement("div");
37                         slotDisplay.id = "dragonblocks.hudbar[" + this.id + "].slot[" + i + "]";
38                         slotDisplay.style.position = "absolute";
39                         slotDisplay.style.top = "3px";
40                         slotDisplay.style.left = i * 53 + "px";
41                         slotDisplay.style.width = "50px";
42                         slotDisplay.style.height = "50px";
43                         slotDisplay.style.backgroundColor = "black";
44                         slotDisplay.style.boxShadow = "0 0 0 3px #C5C5C5";
45                         let slotCountDisplay = document.createElement("span");
46                         slotCountDisplay.id = slotDisplay.id + ".count";
47                         slotCountDisplay.style.position = "absolute";
48                         slotCountDisplay.style.right = "5px";
49                         slotCountDisplay.style.bottom = "5px";
50                         slotCountDisplay.style.color = "white";
51                         slotDisplay.appendChild(slotCountDisplay);
52                         display.appendChild(slotDisplay);
53                 }
54                 let selectorDisplay = document.createElement("div");
55                 selectorDisplay.id = "dragonblocks.hudbar[" + this.id + "].selector";
56                 selectorDisplay.style.position = "absolute";
57                 selectorDisplay.style.top = "3px";
58                 selectorDisplay.style.width = "50px";
59                 selectorDisplay.style.height = "50px";
60                 selectorDisplay.style.boxShadow = "0 0 0 5px #999999";
61                 display.appendChild(selectorDisplay);
62                 let itemnameDisplay = document.createElement("span");
63                 itemnameDisplay.id = "dragonblocks.hudbar[" + this.id + "].itemname";
64                 itemnameDisplay.style.position = "absolute";
65                 itemnameDisplay.style.bottom = "60px";
66                 itemnameDisplay.style.color = "white";
67                 itemnameDisplay.style.fontSize = "20px";
68                 display.appendChild(itemnameDisplay);
69                 display = document.body.insertBefore(display, document.getElementById("dragonblocks.map").nextSibling);
70                 dblib.center(display);
71                 this.update();
72         }
73         nextItem(){
74                 (this.selectedSlot++ == 7) && (this.selectedSlot = 0);
75                 this.update();
76         }
77         previousItem(){
78                 (this.selectedSlot-- == 0) && (this.selectedSlot = 7);
79                 this.update();
80         }
81         select(i){
82                 this.selectedSlot = i;
83                 this.update();
84         }
85         update(){
86                 let display = document.getElementById("dragonblocks.hudbar[" + this.id + "]");
87                 if(! display)
88                         return;
89                 for(let i = 0; i < this.slots; i++){
90                         let itemstack = this.inventory.getSlot(i);
91                         let slotDisplay = document.getElementById("dragonblocks.hudbar[" + this.id + "].slot[" + i + "]");
92                         slotDisplay.style.background = itemstack.item ? dragonblocks.getTexture(itemstack.toItem().texture) : "black";
93                         slotDisplay.style.backgroundSize = "cover";
94                         slotDisplay.style.opacity = itemstack.item ? 1 : 0.3;
95                         document.getElementById(slotDisplay.id + ".count").innerHTML = (itemstack.count <= 1) ? "" : itemstack.count;
96                         if(i == this.selectedSlot){
97                                 document.getElementById("dragonblocks.hudbar[" + this.id + "].selector").style.left = slotDisplay.style.left;
98                                 document.getElementById("dragonblocks.hudbar[" + this.id + "].itemname").innerHTML = itemstack.item ? itemstack.toItem().desc : "";
99                                 dblib.center(document.getElementById("dragonblocks.hudbar[" + this.id + "].itemname"));
100                         }
101                 }
102         }
103         getSelectedItem(){
104                 return this.inventory.getSlot(this.selectedSlot);
105         }
106