]> git.lizzy.rs Git - dragonblocks.git/blob - engine/player.js
Code style overhaul
[dragonblocks.git] / engine / player.js
1 /*
2  * player.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.registerTool({
24         name: "dragonblocks:hand",
25         interval: 500,
26         groups: [
27                 {
28                         name: "default",
29                         damage: 2,
30                 },
31                 {
32                         name: "cracky",
33                         damage: 0,
34                 }
35         ]
36 });
37 dragonblocks.registerTool({
38         name: "dragonblocks:creative_hand",
39         range: Infinity,
40         groups: [
41                 {
42                         name: "default",
43                         damage: Infinity
44                 }
45         ]
46 });
47 dragonblocks.registerEntity({
48         name: "dragonblocks:player",
49         gravity: true,
50         width: 1,
51         height: 2,
52         horizontalSpeed: dragonblocks.settings.player.speed,
53         verticalSpeed: dragonblocks.settings.player.jumpspeed,
54         meta: {
55                 skin: dragonblocks.settings.player.defaultSkin,
56                 creative: false,
57         }
58 });
59
60 dragonblocks.Player = class extends dragonblocks.SpawnedEntity{
61         constructor()
62         {
63                 if (dragonblocks.worldIsLoaded) {
64                         super(dragonblocks.world.spawnedEntities.filter(entity => {
65                                 return entity.name == "dragonblocks:player";
66                         })[0]);
67
68                         dragonblocks.world.spawnedEntities = dragonblocks.world.spawnedEntities.filter(entity => {
69                                 return entity.name != "dragonblocks:player";
70                         });
71                 } else {
72                         super(dragonblocks.entities["dragonblocks:player"], dragonblocks.map.width / 2, 5);
73                 }
74
75                 let self = this;
76
77                 // Skin
78                 this.skin = this.meta.skin;
79
80                 // Inventory
81                 this.tmp.inventory = new dragonblocks.InventoryGroup();                                                                 // Create Inventory Group that can hold multible Inventories
82
83                 // Main Inventory
84                 this.tmp.mainInventory = new dragonblocks.Inventory(32, 8);                                                             // The Main Inventory
85
86                 if (this.meta.mainInventory)
87                         this.tmp.mainInventory.deserialize(this.meta.mainInventory);                                            // Load saved Inventory
88
89                 this.tmp.mainInventory.addEventListener("updateStack", event => {
90                         self.meta.mainInventory = this.tmp.mainInventory.serialize();                                           // Save inventory after every change
91
92                         if (self.gamemode == "creative" && event.stack.count > 1)                       // Keep itemcount of every stack at one when in creative
93                                 event.stack.count = 1;
94                 });
95
96                 this.tmp.mainInventory.addEventListener("updateStack", _ => {
97                         if (self.tmp.hudbar)
98                                 self.tmp.hudbar.update();
99                 });
100
101                 // Hudbar
102                 this.tmp.hudbar = new dragonblocks.Hudbar(this.tmp.mainInventory, 8);                                   // The hudbar has 8 slots
103
104                 // Creative Inventory
105                 let creativelist = [];
106
107                 dragonblocks.registeredItems.filter(item => {
108                         return ! item.hidden;
109                 }).forEach(item => {
110                         creativelist.push(item.name);
111                 });
112
113                 this.tmp.creativeInventory = new dragonblocks.CreativeInventory(32, creativelist, 8);   // The creative Inventory contains every registered item that is not marked as hidden
114
115                 // Survival Inventory
116                 this.tmp.survivalInventory = new dragonblocks.InventoryContainer({
117                         inventory: new dragonblocks.Craftfield(3, 3),
118                         top: 0.5,
119                         bottom: 0.5,
120                         left: 1,
121                         right: 2,
122                 });
123
124                 if (this.meta.survivalInventory)
125                         this.tmp.survivalInventory.deserialize(this.meta.survivalInventory);
126
127                 this.tmp.survivalInventory.addEventListener("updateStack", _ => {
128                         self.meta.survivalInventory = this.tmp.survivalInventory.serialize();
129                 });
130
131                 // Init Inventory
132                 this.resetInventoryElements();
133
134                 // Map Interaction
135                 this.tmp.tool = null;
136                 this.tmp.defaultTool = dragonblocks.tools[this.meta.creative ? "dragonblocks:creative_hand" : "dragonblocks:hand"];
137                 this.initMapInteraction();
138
139                 // Map Scroll
140                 setInterval(_ => {
141                         if (dragonblocks.map.displayLeft + dragonblocks.map.displayWidth < self.x + self.width + 3)
142                                 dragonblocks.map.displayLeft = parseInt(self.x + self.width + 3 - dragonblocks.map.displayWidth);
143                         else if (dragonblocks.map.displayLeft > self.x - 2)
144                                 dragonblocks.map.displayLeft = parseInt(self.x - 2);
145                         if (dragonblocks.map.displayTop + dragonblocks.map.displayHeight < self.y + self.height + 3)
146                                 dragonblocks.map.displayTop = parseInt(self.y + self.height + 3 - dragonblocks.map.displayHeight);
147                         else if (dragonblocks.map.displayTop > self.y - 2)
148                                 dragonblocks.map.displayTop = parseInt(self.y - 2);
149
150                         dragonblocks.map.updateGraphics();
151                 });
152
153                 // Controls
154                 dragonblocks.keyHandler.down(" ", _ => {
155                         self.jump();
156                 });
157
158                 dragonblocks.keyHandler.up(" ", _ => {
159                         self.stopJump();
160                 });
161
162                 dragonblocks.keyHandler.down("ArrowLeft", _ => {
163                         self.moveLeft();
164                 });
165
166                 dragonblocks.keyHandler.down("ArrowRight", _ => {
167                         self.moveRight();
168                 });
169
170                 dragonblocks.keyHandler.up("ArrowLeft", _ => {
171                         self.stop();
172                 });
173
174                 dragonblocks.keyHandler.up("ArrowRight", _ => {
175                         self.stop();
176                 });
177
178                 dragonblocks.keyHandler.down("i", _ => {
179                         self.toggleInventory();
180                 });
181
182                 dragonblocks.keyHandler.down("n", _ => {
183                         self.nextItem();
184                 });
185
186                 dragonblocks.keyHandler.down("b", _=> {
187                         self.previousItem();
188                 });
189
190                 dragonblocks.keyHandler.down("scroll", _ => {
191                         self.nextItem();
192                 });
193
194                 dragonblocks.keyHandler.up("scroll", _=>{
195                         self.previousItem();
196                 });
197
198                 for (let i = 1; i < 9; i++) {
199                         dragonblocks.keyHandler.down(i.toString(), _ => {
200                                 self.select(i - 1);
201                         });
202                 }
203
204                 let mapDisplay = document.getElementById("dragonblocks.map");
205
206                 addEventListener("mouseup", event => {
207                         if (event.which == 1)
208                                 self.digStop();
209                 });
210
211                 addEventListener("keydown", event => {
212                         if (event.key == "Escape" && self.inventoryIsOpen())
213                                 self.closeInventory();
214                 });
215
216                 // Map Interaction Controls
217                 for (let x = 0; x < dragonblocks.map.displayWidth; x++) {
218                         for (let y = 0; y < dragonblocks.map.displayHeight; y++) {
219                                 let nodeDisplay = document.getElementById("dragonblocks.map.node[" + x + "][" + y + "]");
220
221                                 nodeDisplay.addEventListener("mouseover", event => {
222                                         if (self.canReach(x + dragonblocks.map.displayLeft, y + dragonblocks.map.displayTop))
223                                                 event.srcElement.style.boxShadow = "0 0 0 1px black inset";
224                                 });
225
226                                 nodeDisplay.addEventListener("mouseleave", event => {
227                                         event.srcElement.style.boxShadow = "none";
228                                 });
229
230                                 nodeDisplay.addEventListener("mousedown", event => {
231                                         let [ix, iy] = [x + dragonblocks.map.displayLeft, y + dragonblocks.map.displayTop];
232
233                                         switch(event.which) {
234                                                 case 1:
235                                                         self.digStart(ix, iy);
236                                                         break;
237
238                                                 case 3:
239                                                         self.build(ix, iy);
240                                                         break;
241                                         };
242                                 });
243                         }
244                 }
245         }
246
247         set skin(value)
248         {
249                 this.meta.skin = value;
250                 this.texture = dragonblocks.registeredSkins[value].texture;
251                 this.updateTexture();
252         }
253
254         get skin()
255         {
256                 return this.meta.skin;
257         }
258
259         set gamemode(mode)
260         {
261                 this.setGamemode(mode);
262         }
263
264         get gamemode()
265         {
266                 return this.meta.creative ? "creative" : "survival";
267         }
268
269         get tool()
270         {
271                 return dragonblocks.tools[this.getWieldedItem().item] || this.tmp.defaultTool;
272         }
273
274         setGamemode(mode)
275         {
276                 switch (mode.toString().toLowerCase()) {
277                         case "0":
278                         case "survival":
279                                 this.meta.creative = false;
280                                 break;
281
282                         case "1":
283                         case "creative":
284                                 this.meta.creative = true;
285                                 break;
286
287                         default:
288                                 return false;
289                 }
290
291                 this.resetInventoryElements();
292                 this.tmp.defaultTool = dragonblocks.tools[this.meta.creative ? "dragonblocks:creative_hand" : "dragonblocks:hand"];
293
294                 return true;
295         }
296
297         inventoryIsOpen()
298         {
299                 return this.tmp.inventory.opened;
300         }
301
302         openInventory()
303         {
304                 this.tmp.inventory.open();
305                 dragonblocks.keyHandler.lockAll();
306                 dragonblocks.keyHandler.unlock("i");
307                 dragonblocks.gui.showLayer();
308         }
309
310         closeInventory()
311         {
312                 this.tmp.inventory.close();
313                 dragonblocks.keyHandler.unlockAll();
314                 dragonblocks.gui.hideLayer();
315         }
316
317         toggleInventory()
318         {
319                 this.inventoryIsOpen() ? this.closeInventory() : this.openInventory();
320         }
321
322         give(itemstring)
323         {
324                 return this.tmp.mainInventory.add(itemstring);
325         }
326
327         clearInventory()
328         {
329                 this.tmp.mainInventory.clear();
330         }
331
332         setInventoryElements(elems)
333         {
334                 this.tmp.inventory.elements = elems;
335         }
336
337         resetInventoryElements()
338         {
339                 let elems = [this.tmp.mainInventory];
340                 elems.unshift(this.gamemode == "creative" ? this.tmp.creativeInventory : this.tmp.survivalInventory);
341                 this.setInventoryElements(elems);
342         }
343
344         previousItem()
345         {
346                 this.tmp.hudbar.previousItem();
347         }
348
349         nextItem()
350         {
351                 this.tmp.hudbar.nextItem();
352         }
353
354         select(i)
355         {
356                 this.tmp.hudbar.select(i);
357         }
358
359         getWieldedItem()
360         {
361                 return this.tmp.hudbar.getSelectedItem();
362         }
363
364         set onNextInventoryClose(func)
365         {
366                 this.tmp.inventory.onNextClose = func;
367         }
368 };
369
370 Object.assign(dragonblocks.Player.prototype, dragonblocks.MapInteraction);      //Mixin