4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
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.
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.
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,
24 dragonblocks.Map = class
30 this.entityContainer = dragonblocks.mapDisplay.element.appendChild(document.createElement("div"));
31 this.entityContainer.style.position = "absolute";
32 this.entityContainer.style.visibility = "hidden";
34 this.deserialize(data);
44 entities: dblib.removeTmp(this.entities),
45 structures: this.structures,
51 this.width = data.width;
52 this.height = data.height;
56 this.structures = data.structures || {};
58 for (let x = 0; x < this.width; 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"));
65 for (let entity of data.entities)
66 new dragonblocks.SpawnedEntity(entity, this);
72 this.entityContainer.style.visibility = "inherit";
78 this.entityContainer.style.visibility = "hidden";
83 return x < this.width && y < this.height && x >= 0 && y >= 0;
88 if (this.withinBounds(x, y))
89 return this.data[x][y];
94 node = new dragonblocks.MapNode(node);
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);
101 this.data[x][y] = node;
103 let nodeDef = node.toNode();
104 nodeDef.onset && nodeDef.onset(this, x, y);
106 this.active && dragonblocks.mapDisplay.updateNode(x, y);
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);
119 let nodeDef = node.toNode();
120 nodeDef.onactivate && nodeDef.onactivate(this, ix, iy);
122 for (let func of dragonblocks.onActivateCallbacks)
128 addStructure(name, msg, pos)
130 this.structures[name] = this.structures[name] || [];
131 this.structures[name].push({msg, pos});
134 spawnEntity(name, x, y)
136 let def = dragonblocks.entities[name];
139 return new dragonblocks.SpawnedEntity(def, this, x, y);
147 dragonblocks.mapMgr.setSky(this.sky);
151 dragonblocks.mapMgr = new dragonblocks.ContentMgr(dragonblocks.Map);
153 dragonblocks.onActivateCallbacks = [];
154 dragonblocks.registerOnActivate = func => {
155 dragonblocks.onActivateCallbacks.push(func);