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";
35 this.deserialize(data);
46 displayLeft: this.displayLeft,
47 displayTop: this.displayTop,
48 structures: this.structures,
49 entities: dblib.removeTmp(this.entities),
56 this.width = data.width;
57 this.height = data.height;
58 this.displayLeft = data.displayLeft;
59 this.displayTop = data.displayTop;
61 this.structures = data.structures;
63 for (let x = 0; x < this.width; x++) {
65 for (let y = 0; y < this.height; y++)
66 this.setNode(x, y, new dragonblocks.MapNode().createFromMapNode(data.data[x][y]));
69 for (let entity of data.entities)
70 new dragonblocks.SpawnedEntity(entity);
76 this.entityContainer.style.visibility = "inherit";
82 this.entityContainer.style.visibility = "hidden";
88 this.width = dragonblocks.settings.map.width;
89 this.height = dragonblocks.settings.map.height;
90 this.displayTop = dragonblocks.settings.map.height / 2;
91 this.displayLeft = dragonblocks.settings.map.width / 2 - 5;
95 for (let x = 0; x < this.width; x++) {
97 for (let y = 0; y < this.height; y++)
98 this.setNode(x, y, new dragonblocks.MapNode("air"));
104 return x < this.width && y < this.height && x >= 0 && y >= 0;
109 if (this.withinBounds(x, y))
110 return this.data[x][y];
115 node = new dragonblocks.MapNode(node);
117 if (this.withinBounds(x, y)) {
118 let oldNode = this.data[x][y];
119 let oldNodeDef = oldNode instanceof dragonblocks.MapNode && oldNode.toNode();
120 oldNodeDef && oldNodeDef.onremove && oldNodeDef.onremove(this, x, y);
122 this.data[x][y] = node;
124 let nodeDef = node.toNode();
125 nodeDef.onset && nodeDef.onset(this, x, y);
127 this.active && dragonblocks.mapDisplay.updateNode(x, y);
133 for (let ix = x - 1; ix <= x + 1; ix++) {
134 for (let iy = y - 1; iy <= y + 1; iy++) {
135 let node = this.getNode(ix, iy);
140 let nodeDef = node.toNode();
141 nodeDef.onactivate && nodeDef.onactivate(this, ix, iy);
143 for (let func of dragonblocks.onActivateCallbacks)
149 addStructure(name, msg, pos)
151 this.structures[name] = this.structures[name] || [];
152 this.structures[name].push({msg, pos});
155 spawnEntity(name, x, y)
157 let def = dragonblocks.entities[name];
160 return new dragonblocks.SpawnedEntity(def, this, x, y);
165 dragonblocks.onActivateCallbacks = [];
166 dragonblocks.registerOnActivate = func => {
167 dragonblocks.onActivateCallbacks.push(func);