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