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,
23 dragonblocks.Map = class{
25 dragonblocks.map = this;
26 dblib.copy(this, dragonblocks.world.map);
28 for(let x = 0; x < this.width; x++)
29 for(let y = 0; y < this.height; y++)
30 this.setNode(x, y, new dragonblocks.MapNode().createFromMapNode(this.content[x][y]));
33 if(this.contains(x, y)){
34 if(this.content[x][y] instanceof dragonblocks.MapNode && this.content[x][y].toNode().onremove)
35 this.content[x][y].toNode().onremove(x, y);
36 for(let func of dragonblocks.onRemoveNodeFunctions)
38 this.content[x][y] = node;
39 if(node.toNode().onset)
40 node.toNode().onset(x, y);
41 for(let func of dragonblocks.onSetNodeFunctions)
43 this.graphicsUpdateNode(x, y);
47 for(let ix = x - 1; ix <= x + 1; ix++){
48 for(let iy = y - 1; iy <= y + 1; iy++){
49 if(! this.getNode(ix, iy))
51 if(this.getNode(ix, iy).toNode().onactivate)
52 this.getNode(ix, iy).toNode().onactivate(ix, iy);
53 for(let func of dragonblocks.onActivateNodeFunctions)
59 if(this.contains(x, y))
60 return this.content[x][y];
63 return x < this.width && y < this.height && x >= 0 && y >= 0;
65 getNodeGraphics(x, y){
66 return document.getElementById("dragonblocks.map.node[" + (x - this.displayLeft) + "][" + (y - this.displayTop) + "]");
69 return [Math.floor(x / dragonblocks.settings.map.scale) + this.displayLeft, Math.floor(y / dragonblocks.settings.map.scale) + this.displayTop];
71 graphicsUpdateNode(x, y){
72 let nodeDisplay = this.getNodeGraphics(x, y);
73 let node = this.getNode(x, y).toNode();
74 if(!nodeDisplay || !node)
76 nodeDisplay.style.background = dragonblocks.getTexture(node.texture);
77 nodeDisplay.style.backgroundSize = "cover";
78 nodeDisplay.style.zIndex = node.zIndex || "1";
81 if(this.displayLeft < 0)
83 else if(this.displayLeft + this.displayWidth > this.width)
84 this.displayLeft = this.width - this.displayWidth;
85 if(this.displayTop < 0)
87 else if(this.displayTop + this.displayHeight > this.height)
88 this.displayTop = this.height - this.displayHeight;
89 for(let x = 0; x < this.displayWidth; x++){
90 for(let y = 0; y < this.displayHeight; y++){
91 this.graphicsUpdateNode(x + this.displayLeft, y + this.displayTop);
96 this.displayWidth = Math.min(Math.ceil(innerWidth / dragonblocks.settings.map.scale), this.width);
97 this.displayHeight = Math.min(Math.ceil(innerHeight / dragonblocks.settings.map.scale), this.height);
98 var map = document.createElement("div");
99 map.id = "dragonblocks.map";
100 map.style.width = this.displayWidth * dragonblocks.settings.map.scale + "px";
101 map.style.height = this.displayHeight * dragonblocks.settings.map.scale + "px";
102 map.style.position = "fixed";
103 map.style.top = "0px";
104 map.style.left = "0px";
105 map.style.backgroundColor = "skyblue";
106 map.style.visibility = "hidden";
107 for(let x = 0; x < this.displayWidth; x++){
108 for(let y = 0; y < this.displayHeight; y++){
109 var node = document.createElement("div");
110 node.id = "dragonblocks.map.node[" + x + "][" + y + "]";
111 node.style.position = "absolute";
112 node.style.top = y * dragonblocks.settings.map.scale + "px";
113 node.style.left = x * dragonblocks.settings.map.scale + "px";
114 node.style.width = dragonblocks.settings.map.scale + "px";
115 node.style.height = dragonblocks.settings.map.scale + "px";
116 map.appendChild(node);
119 document.body.insertBefore(map, document.body.firstChild);
122 dragonblocks.setNode = function(x, y, node){
123 dragonblocks.map.setNode(x, y, new dragonblocks.MapNode(node));
125 dragonblocks.getNode = function(x, y){
126 return dragonblocks.map.getNode(x, y);
128 dragonblocks.registerOnStarted(_ => {
129 document.getElementById("dragonblocks.map").style.visibility = "visible";
131 dragonblocks.registerOnQuit(_ => {
132 document.getElementById("dragonblocks.map").style.visibility = "hidden";