]> git.lizzy.rs Git - dragonblocks.git/blob - engine/falling_node.js
Map abstraction and World class
[dragonblocks.git] / engine / falling_node.js
1 /*
2  * falling_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.registerEntity({
25         name: "dragonblocks:falling_node",
26         gravity: true,
27         width: 1,
28         height: 1,
29         texture: this.texture,
30         oncollide: self => {
31                 let under = self.map.getNode(Math.floor(self.x), Math.floor(self.y) + 1);
32
33                 if (! under || under.mobstable) {
34                         self.map.setNode(Math.floor(self.x), Math.floor(self.y), self.meta.nodeName);
35                         self.despawn();
36                 }
37         }
38 });
39
40 dragonblocks.registerOnActivate((map, x, y) => {
41         let node = map.getNode(x, y);
42         if (! node.toNode().physics)
43                 return;
44
45         let under = map.getNode(x, y + 1);
46         if (under && ! under.mobstable)
47                 dragonblocks.spawnFallingNode(node.name, map, x, y)
48 });
49
50 dragonblocks.spawnFallingNode = (nodename, map, x, y) => {
51         setTimeout(_ => {
52                 map.activate(x, y);
53         }, 50);
54
55         map.setNode(x, y, "air");
56
57         let entity = map.spawnEntity("dragonblocks:falling_node", x, y);
58         entity.meta.nodeName = nodename;
59         entity.texture = dragonblocks.nodes[nodename].texture;
60         entity.updateTexture();
61 };