]> git.lizzy.rs Git - dragonblocks.git/blob - engine/item.js
Map abstraction and World class
[dragonblocks.git] / engine / item.js
1 /*
2  * item.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
24 dragonblocks.Item = class
25 {
26         constructor(def)
27         {
28                 def || dragonblocks.error("Cannot register item: Missing argument");
29
30                 dblib.copy(this, def);
31
32                 this.name || dragonblocks.error("Cannot register item: Missing name");
33                 this.texture || dragonblocks.error("Cannot register item: Missing texture");
34                 dragonblocks.items[this.name] && dragonblocks.error("Cannot register item '" + this.name + "': Item already exists");
35
36                 if (this.desc != "" && this.description != "")
37                         this.desc = this.description || this.desc || this.name;
38
39                 this.stacksize = this.stacksize || dragonblocks.settings.item.defaultStacksize;
40
41                 this.groups = this.groups || [];
42                 this.groups.push("default");
43
44                 dragonblocks.items[this.name] = this;
45                 dragonblocks.registeredItems.push(this);
46         }
47
48         inGroup(name)
49         {
50                 return this.groups.includes(name);
51         }
52
53         playSound(s)
54         {
55                 let sound = this.sounds && this.sounds[s];
56
57                 if (sound == "")
58                         return;
59
60                 if (sound) {
61                         dragonblocks.playSound(sound);
62                 } else {
63                         for (let groupname of this.groups){
64                                 let group = dragonblocks.groups[groupname];
65                                 let sound = group && group.sounds && group.sounds[s];
66
67                                 if (sound == "")
68                                         return;
69
70                                 if (sound) {
71                                         dragonblocks.playSound(sound);
72                                         return;
73                                 }
74                         }
75                 }
76         }
77 };
78
79 dragonblocks.registeredItems = [];
80 dragonblocks.items = {};
81
82 dragonblocks.registerItem = def => {
83         new dragonblocks.Item(def);
84 };
85
86 dragonblocks.itemMatch = (item1, item2) => {
87         item1 = dragonblocks.items[item1] || dragonblocks.groups[item1] || item1;
88         item2 = dragonblocks.items[item2] || dragonblocks.groups[item2] || item2;
89         return item1 == item2 || item1 && item2 && (item1.name == item2.name || item1.inGroup && item1.inGroup(item2.name) || item2.inGroup && item2.inGroup(item1.name));
90 };