4 * Copyright 2021 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.MapDisplay = class
31 this.scale = dragonblocks.settings.mapDisplay.scale;
32 this.width = Math.ceil(innerWidth / this.scale);
33 this.height = Math.ceil(innerHeight / this.scale);
37 this.element = document.body.insertBefore(document.createElement("div"), document.body.firstChild);
38 this.element.style.width = this.width * this.scale + "px";
39 this.element.style.height = this.height * this.scale + "px";
40 this.element.style.position = "fixed";
41 this.element.style.top = "0px";
42 this.element.style.left = "0px";
43 this.element.style.visibility = "hidden";
46 for (let x = 0; x < this.width; x++) {
48 for (let y = 0; y < this.height; y++) {
49 let node = this.nodes[x][y] = this.element.appendChild(document.createElement("div"));
50 node.style.position = "absolute";
51 node.style.top = y * this.scale + "px";
52 node.style.left = x * this.scale + "px";
53 node.style.width = this.scale + "px";
54 node.style.height = this.scale + "px";
63 this.interval = setInterval(_ => {
67 this.element.style.visibility = "visible";
73 clearInterval(this.interval);
80 this.element.style.visibility = "hidden";
86 this.map.setInactive();
91 this.autoScroll() || this.update();
102 this.element.style.background = sky;
105 getActiveEntityContainer()
107 return this.entityContainers[this.map];
112 return this.map && this.anchor;
117 return x < this.width && y < this.height && x >= 0 && y >= 0;
122 return this.withinBounds(x, y) && this.nodes[x][y];
127 if (! this.isInitialized())
130 let node = this.getNode(x - this.left, y - this.top);
135 let mapNodeDef = this.map.getNode(x, y).toNode();
138 node.style.background = dragonblocks.getTexture(mapNodeDef.texture);
139 node.style.zIndex = mapNodeDef.zIndex || "1";
141 node.style.background = "black";
147 if (! this.isInitialized())
154 if (this.map.width >= this.width)
155 this.left = parseInt(Math.max(Math.min(this.left, this.map.width - this.width, this.anchor.x - 3), 0, this.anchor.x + this.anchor.width + 3 - this.width));
157 this.left = parseInt((this.width - this.map.width) / 2);
159 if (this.map.width >= this.width)
160 this.top = parseInt(Math.max(Math.min(this.top, this.map.height - this.height, this.anchor.y - 3), 0, this.anchor.y + this.anchor.height + 3 - this.height));
162 this.top = parseInt((this.height - this.map.height) / 2);
164 let changed = oldLeft != this.left || oldTop != this.top;
174 if (! this.isInitialized())
177 this.map.entityContainer.style.left = -this.left * this.scale + "px";
178 this.map.entityContainer.style.top = -this.top * this.scale + "px";
180 for (let x = 0; x < this.width; x++)
181 for(let y = 0; y < this.height; y++)
182 this.updateNode(x + this.left, y + this.top);
186 dragonblocks.mapDisplay = new dragonblocks.MapDisplay();
188 dragonblocks.registerOnQuit(_ => {
189 dragonblocks.mapDisplay.setInactive();
192 dragonblocks.registerOnStarted(_ => {
193 dragonblocks.mapDisplay.setSky("skyblue");
194 dragonblocks.mapDisplay.setMap(dragonblocks.player.map);
195 dragonblocks.mapDisplay.setAnchor(dragonblocks.player);
197 dragonblocks.mapDisplay.setActive();