]> git.lizzy.rs Git - dragonblocks.git/blob - engine/map.js
Add Map Manager
[dragonblocks.git] / engine / map.js
1 /*
2  * map.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.Map = class
25 {
26         constructor(data)
27         {
28                 this.active = false;
29
30                 this.entityContainer = dragonblocks.mapDisplay.element.appendChild(document.createElement("div"));
31                 this.entityContainer.style.position = "absolute";
32                 this.entityContainer.style.visibility = "hidden";
33
34                 this.deserialize(data);
35         }
36
37         serialize()
38         {
39                 return {
40                         data: this.data,
41                         width: this.width,
42                         height: this.height,
43                         structures: this.structures,
44                         entities: dblib.removeTmp(this.entities),
45                 };
46         }
47
48         deserialize(data)
49         {
50                 this.data = [];
51
52                 this.width = data.width;
53                 this.height = data.height;
54
55                 this.entities = [];
56                 this.structures = data.structures || {};
57
58                 for (let x = 0; x < this.width; x++) {
59                         this.data[x] = [];
60                         for (let y = 0; y < this.height; y++)
61                                 this.setNode(x, y, data.data ? new dragonblocks.MapNode().createFromMapNode(data.data[x][y]) : new dragonblocks.MapNode("air"));
62                 }
63
64                 if (data.entities)
65                         for (let entity of data.entities)
66                                 new dragonblocks.SpawnedEntity(entity, this);
67         }
68
69         setActive()
70         {
71                 this.active = true;
72                 this.entityContainer.style.visibility = "inherit";
73         }
74
75         setInactive()
76         {
77                 this.active = false;
78                 this.entityContainer.style.visibility = "hidden";
79         }
80
81         withinBounds(x, y)
82         {
83                 return x < this.width && y < this.height && x >= 0 && y >= 0;
84         }
85
86         getNode(x, y)
87         {
88                 if (this.withinBounds(x, y))
89                         return this.data[x][y];
90         }
91
92         setNode(x, y, node)
93         {
94                 node = new dragonblocks.MapNode(node);
95
96                 if (this.withinBounds(x, y)) {
97                         let oldNode = this.data[x][y];
98                         let oldNodeDef = oldNode instanceof dragonblocks.MapNode && oldNode.toNode();
99                         oldNodeDef && oldNodeDef.onremove && oldNodeDef.onremove(this, x, y);
100
101                         this.data[x][y] = node;
102
103                         let nodeDef = node.toNode();
104                         nodeDef.onset && nodeDef.onset(this, x, y);
105
106                         this.active && dragonblocks.mapDisplay.updateNode(x, y);
107                 }
108         }
109
110         activate(x, y)
111         {
112                 for (let ix = x - 1; ix <= x + 1; ix++) {
113                         for (let iy = y - 1; iy <= y + 1; iy++) {
114                                 let node = this.getNode(ix, iy);
115
116                                 if (! node)
117                                         continue;
118
119                                 let nodeDef = node.toNode();
120                                 nodeDef.onactivate && nodeDef.onactivate(this, ix, iy);
121
122                                 for (let func of dragonblocks.onActivateCallbacks)
123                                         func(this, ix, iy);
124                         }
125                 }
126         }
127
128         addStructure(name, msg, pos)
129         {
130                 this.structures[name] = this.structures[name] || [];
131                 this.structures[name].push({msg, pos});
132         }
133
134         spawnEntity(name, x, y)
135         {
136                 let def = dragonblocks.entities[name];
137
138                 if (def)
139                         return new dragonblocks.SpawnedEntity(def, this, x, y);
140         }
141
142 };
143
144 dragonblocks.mapMgr = new dragonblocks.ContentMgr(dragonblocks.Map);
145
146 dragonblocks.onActivateCallbacks = [];
147 dragonblocks.registerOnActivate = func => {
148         dragonblocks.onActivateCallbacks.push(func);
149 };