]> git.lizzy.rs Git - dragonblocks.git/blob - engine/node.js
95eb19fbe3d4402f974673eaa67d528d34bc14c1
[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 dragonblocks.Node = class extends dragonblocks.Item{
24         constructor(obj){
25                 super(obj);
26                 if(this.drops == "")
27                         this.drops = " ";
28                 if(this.drop == "")
29                         this.drop = " ";
30                 this.drops = this.drops || this.drop || this.name;
31                 if(this.mobstable == undefined)
32                         this.mobstable = this.stable;
33                 let self = this;
34                 if(this.liquid){
35                         this.hardness = 1;
36                         let oldOndig = this.ondig;
37                         this.ondig =  (x, y) => {
38                                 if(oldOndig)
39                                         return oldOndig(x, y);
40                                 return false;
41                         };
42                         let oldBlast = this.onblast;
43                         this.onblast = (x, y) => {
44                                 if(oldBlast)
45                                         return oldBlast(x, y);
46                                 return false;
47                         };
48                         let oldOnset = this.onset;
49                         this.onset = (x, y) => {
50                                 let meta = dragonblocks.getNode(x, y).meta;
51                                 meta.liquidInterval = setInterval(_ => {
52                                         for(let [ix, iy] of [[x + 1, y], [x - 1, y], [x, y + 1]]){
53                                                 let mapNode = dragonblocks.getNode(ix, iy);
54                                                 if(! mapNode || mapNode.stable || mapNode.toNode().liquid)
55                                                         continue;
56                                                 dragonblocks.setNode(ix, iy, self.name);
57                                         }
58                                 }, self.liquidTickSpeed || 2000);
59                                 if(oldOnset)
60                                         oldOnset(x, y);
61                                 return meta;
62                         }
63                         let oldOnremove = this.onremove;
64                         this.onremove = (x, y) => {
65                                 clearInterval(dragonblocks.getNode(x, y).meta.liquidInterval)
66                                 if(oldOnremove)
67                                         oldOnremove(x, y);
68                         }
69                 }
70                 dragonblocks.nodes[this.name] = this;
71                 dragonblocks.registeredNodes.push(this);
72         }
73 }
74 dragonblocks.nodes = {};
75 dragonblocks.registeredNodes = [];
76 dragonblocks.registerNode = function(obj){
77         new dragonblocks.Node(obj);
78 }
79 dragonblocks.onSetNodeFunctions = [];
80 dragonblocks.registerOnSetNode = function(func){
81         dragonblocks.onSetNodeFunctions.push(func);
82 }
83 dragonblocks.onRemoveNodeFunctions = [];
84 dragonblocks.registerOnRemoveNode = function(func){
85         dragonblocks.onRemoveNodeFunctions.push(func);
86 }
87 dragonblocks.onPlaceNodeFunctions = [];
88 dragonblocks.registerOnPlaceNode = function(func){
89         dragonblocks.onPlaceNodeFunctions.push(func);
90 }
91 dragonblocks.onDigNodeFunctions = [];
92 dragonblocks.registerOnDigNode = function(func){
93         dragonblocks.onDigNodeFunctions.push(func);
94 }
95 dragonblocks.onClickNodeFunctions = [];
96 dragonblocks.registerOnClickNode = function(func){
97         dragonblocks.onClickNodeFunctions.push(func);
98 }
99 dragonblocks.onActivateNodeFunctions = [];
100 dragonblocks.registerOnActivateNode = function(func){
101         dragonblocks.onActivateNodeFunctions.push(func);
102 }
103 dragonblocks.onPunchNodeFunctions = [];
104 dragonblocks.registerOnPunchNode = function(func){
105         dragonblocks.onPunchNodeFunctions.push(func);
106 }
107