]> git.lizzy.rs Git - dragonblocks.git/blob - game/tnt/init.js
Map abstraction and World class
[dragonblocks.git] / game / tnt / init.js
1 tnt = {};
2 tnt.explosion = new dragonblocks.Schematic([
3         ["", "air", "air", "air", ""],
4         ["air", "air", "air", "air", "air"],
5         ["air", "air", "§air", "air", "air"],
6         ["air", "air", "air", "air", "air"],
7         ["", "air", "air", "air", ""],
8 ]);
9 tnt.explosion.addFunction((node, map, x, y) => {
10         if(node.onblast && node.onblast(map, x, y) == false)
11                 return false;
12         return dblib.random(0, 100) < 90;
13 });
14 tnt.ignite = function(map, x, y, time){
15         dragonblocks.setTimer("tntTimer", time, _ => {
16                 tnt.explode(map, x, y);
17         }, map.getNode(x, y).meta);
18 }
19 tnt.explode = function(map, x, y){
20         map.setNode(x, y, "air");
21         dragonblocks.playSound("tnt_explode.ogg");
22         tnt.explosion.apply(map, x, y);
23 }
24 dragonblocks.registerNode({
25         name: "tnt:tnt",
26         groups: ["snappy"],
27         hardness: 3,
28         stable: true,
29         desc: "TNT",
30         texture: "tnt_tnt.png",
31         onfire: (x, y) => {
32                 dragonblocks.playSound("tnt_ignite.ogg");
33                 map.setNode(x, y, "tnt:active_tnt");
34         },
35         onblast: (map, x, y) => {
36                 tnt.ignite(map, x, y, 0.1);
37         },
38         onclick: (map, x, y) => {
39                 if(dragonblocks.player.getWieldedItem().item == "torch:torch"){
40                         dragonblocks.playSound("tnt_ignite.ogg");
41                         map.setNode(x, y, "tnt:active_tnt");
42                 }
43         },
44         flammable: true
45 });
46 dragonblocks.registerNode({
47         name: "tnt:active_tnt",
48         groups: [],
49         hardness: 1,
50         stable: true,
51         desc: "TNT (active)",
52         texture: "tnt_active_tnt.png",
53         onset: (map, x, y) => {
54                 tnt.ignite(map, x, y, 4);
55         },
56         ondig: _ => {
57                 return false;
58         },
59         onblast: _ => {
60                 return false;
61         },
62         hidden: true
63 });
64 dragonblocks.registerNode({
65         name: "tnt:gunpowder",
66         groups: ["snappy"],
67         hardness: 1,
68         stable: true,
69         mobstable: false,
70         desc: "Gunpowder",
71         texture: "tnt_gunpowder.png",
72         onclick: (map, x, y) => {
73                 if(dragonblocks.player.getWieldedItem().item == "torch:torch_floor")
74                         map.setNode(x, y, "tnt:active_gunpowder");
75         },
76         hidden: true,
77 });
78 dragonblocks.registerNode({
79         name: "tnt:active_gunpowder",
80         groups: ["snappy"],
81         hardness: 1,
82         stable: true,
83         mobstable: false,
84         desc: "Gunpowder (active)",
85         texture: "tnt_active_gunpowder.png",
86         drops: "tnt:gunpowder",
87         onset: (map, x, y) => {
88                 let meta = map.getNode(x, y).meta;
89                 meta.gunpowderTime = 1;
90                 meta.gunpowderInterval = setInterval(_ => {
91                         meta.gunpowderTime -= 0.1;
92                         if(meta.gunpowderTime <= 0){
93                                 map.setNode(x, y, "air");
94                                 for(let [ix, iy] of [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]]){
95                                         if(map.getNode(ix, iy).name == "tnt:gunpowder")
96                                                 map.setNode(ix, iy, "tnt:active_gunpowder");
97                                         else if(map.getNode(ix, iy).name == "tnt:tnt")
98                                                 tnt.ignite(map, ix, iy, tnt.time);
99                                 }
100                                 clearInterval(meta.gunpowderInterval);
101                         }
102                 }, 100);
103         },
104         onremove: (map, x, y) => {
105                 clearInterval(map.getNode(x, y).meta.gunpowderInterval);
106         },
107         hidden: true
108 });