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
28 dblib.copy(this, dragonblocks.world.map);
29 this.data = this.data || this.content;
35 for (let x = 0; x < this.width; x++)
36 for (let y = 0; y < this.height; y++)
37 this.setNode(x, y, new dragonblocks.MapNode().createFromMapNode(this.data[x][y]));
44 if (this.withinBounds(x, y)) {
45 let oldNode = this.data[x][y];
46 let oldNodeDef = oldNode instanceof dragonblocks.MapNode && oldNode.toNode();
47 oldNodeDef && oldNodeDef.onremove && oldNodeDef.onremove(x, y);
49 for (let func of dragonblocks.onRemoveNodeCallbacks)
52 this.data[x][y] = node;
54 let nodeDef = node.toNode();
55 nodeDef.onset && nodeDef.onset(x, y);
57 for (let func of dragonblocks.onSetNodeCallbacks)
60 this.updateNodeGraphics(x, y);
66 for (let ix = x - 1; ix <= x + 1; ix++) {
67 for (let iy = y - 1; iy <= y + 1; iy++) {
68 let node = this.getNode(ix, iy);
73 let nodeDef = node.toNode();
74 nodeDef.onactivate && nodeDef.onactivate(ix, iy);
76 for(let func of dragonblocks.onActivateNodeCallbacks)
84 if (this.withinBounds(x, y))
85 return this.data[x][y];
90 return x < this.width && y < this.height && x >= 0 && y >= 0;
95 return document.getElementById("dragonblocks.map.node[" + (x - this.displayLeft) + "][" + (y - this.displayTop) + "]");
98 getScreenCoordinates(x, y)
100 return [Math.floor(x / dragonblocks.settings.map.scale) + this.displayLeft, Math.floor(y / dragonblocks.settings.map.scale) + this.displayTop];
103 updateNodeGraphics(x, y)
105 let nodeDisplay = this.getNodeDisplay(x, y);
110 let nodeDef = this.getNode(x, y).toNode();
115 nodeDisplay.style.background = dragonblocks.getTexture(nodeDef.texture);
116 nodeDisplay.style.backgroundSize = "cover";
117 nodeDisplay.style.zIndex = nodeDef.zIndex || "1";
122 if (this.displayLeft < 0)
123 this.displayLeft = 0;
124 else if (this.displayLeft + this.displayWidth > this.width)
125 this.displayLeft = this.width - this.displayWidth;
127 if (this.displayTop < 0)
129 else if (this.displayTop + this.displayHeight > this.height)
130 this.displayTop = this.height - this.displayHeight;
132 for (let x = 0; x < this.displayWidth; x++) {
133 for(let y = 0; y < this.displayHeight; y++) {
134 this.updateNodeGraphics(x + this.displayLeft, y + this.displayTop);
141 this.displayWidth = Math.min(Math.ceil(innerWidth / dragonblocks.settings.map.scale), this.width);
142 this.displayHeight = Math.min(Math.ceil(innerHeight / dragonblocks.settings.map.scale), this.height);
144 let display = document.body.insertBefore(document.createElement("div"), document.body.firstChild);
145 display.id = "dragonblocks.map";
146 display.style.width = this.displayWidth * dragonblocks.settings.map.scale + "px";
147 display.style.height = this.displayHeight * dragonblocks.settings.map.scale + "px";
148 display.style.position = "fixed";
149 display.style.top = "0px";
150 display.style.left = "0px";
151 display.style.backgroundColor = "skyblue";
152 display.style.visibility = "hidden";
154 for (let x = 0; x < this.displayWidth; x++){
155 for (let y = 0; y < this.displayHeight; y++){
156 let nodeDisplay = display.appendChild(document.createElement("div"));
157 nodeDisplay.id = "dragonblocks.map.node[" + x + "][" + y + "]";
158 nodeDisplay.style.position = "absolute";
159 nodeDisplay.style.top = y * dragonblocks.settings.map.scale + "px";
160 nodeDisplay.style.left = x * dragonblocks.settings.map.scale + "px";
161 nodeDisplay.style.width = dragonblocks.settings.map.scale + "px";
162 nodeDisplay.style.height = dragonblocks.settings.map.scale + "px";
168 dragonblocks.setNode = (x, y, node) => {
169 dragonblocks.map.setNode(x, y, new dragonblocks.MapNode(node));
172 dragonblocks.getNode = (x, y) => {
173 return dragonblocks.map.getNode(x, y);
176 dragonblocks.registerOnStarted(_ => {
177 document.getElementById("dragonblocks.map").style.visibility = "visible";
180 dragonblocks.registerOnQuit(_ => {
181 document.getElementById("dragonblocks.map").style.visibility = "hidden";