]> git.lizzy.rs Git - dragonblocks.git/blob - engine/node.js
Map abstraction and World class
[dragonblocks.git] / engine / node.js
1 /*
2  * node.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.Node = class extends dragonblocks.Item
25 {
26         constructor(def){
27                 super(def);
28
29                 if (this.drops == "")
30                         this.drops = " ";
31
32                 if (this.drop == "")
33                         this.drop = " ";
34
35                 this.drops = this.drops || this.drop || this.name;
36
37                 if (this.mobstable == undefined)
38                         this.mobstable = this.stable;
39
40                 if (this.liquid) {
41                         this.hardness = 1;
42
43                         this.ondig = this.ondig || (_ => {
44                                 return false;
45                         });
46
47                         this.onblast = this.onblast || (_ => {
48                                 return false;
49                         });
50
51                         let oldOnset = this.onset;
52                         let self = this;
53
54                         this.onset = (map, x, y) => {
55                                 let meta = map.getNode(x, y).meta;
56
57                                 meta.liquidInterval = setInterval(_ => {
58                                         for(let [ix, iy] of [[x + 1, y], [x - 1, y], [x, y + 1]]){
59                                                 let node = map.getNode(ix, iy);
60
61                                                 if (! node || node.stable || node.toNode().liquid)
62                                                         continue;
63
64                                                 map.setNode(ix, iy, self.name);
65                                         }
66                                 }, self.liquidTickSpeed || 2000);
67
68                                 if (oldOnset)
69                                         oldOnset(map, x, y);
70
71                                 return meta;
72                         };
73
74                         let oldOnremove = this.onremove;
75
76                         this.onremove = (map, x, y) => {
77                                 clearInterval(map.getNode(x, y).meta.liquidInterval);
78
79                                 if (oldOnremove)
80                                         oldOnremove(map, x, y);
81                         };
82                 }
83         }
84 };
85
86 dragonblocks.nodes = {};
87 dragonblocks.registeredNodes = [];
88 dragonblocks.registerNode = def => {
89         let nodeDef = new dragonblocks.Node(def);
90         dragonblocks.nodes[nodeDef.name] = nodeDef;
91         dragonblocks.registeredNodes.push(nodeDef);
92 };