]> git.lizzy.rs Git - dragonblocks.git/blob - engine/map.js
3d0ced6c04d062038693febfbba093b5bf24af23
[dragonblocks.git] / engine / map.js
1 /*
2  * map.js
3  * 
4  * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5  * 
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.
10  * 
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.
15  * 
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,
19  * MA 02110-1301, USA.
20  * 
21  * 
22  */
23 dragonblocks.Map = class{
24         constructor(){
25                 dragonblocks.map = this;
26                 dblib.copy(this, dragonblocks.world.map);
27                 this.graphicsInit();
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]));
31         }
32         setNode(x, y, node){
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)
37                                 func(x, y);
38                         this.content[x][y] = node;
39                         if(node.toNode().onset)
40                                 node.toNode().onset(x, y);
41                         for(let func of dragonblocks.onSetNodeFunctions)
42                                 func(x, y);
43                         this.graphicsUpdateNode(x, y);
44                 }
45         }
46         activate(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))
50                                         continue;
51                                 if(this.getNode(ix, iy).toNode().onactivate)
52                                         this.getNode(ix, iy).toNode().onactivate(ix, iy);
53                                 for(let func of dragonblocks.onActivateNodeFunctions)
54                                         func(ix, iy);
55                         }
56                 }
57         }
58         getNode(x, y){
59                 if(this.contains(x, y))
60                         return this.content[x][y];
61         }
62         contains(x, y){
63                 return x < this.width && y < this.height && x >= 0 && y >= 0;
64         }
65         getNodeGraphics(x, y){
66                 return document.getElementById("dragonblocks.map.node[" + (x - this.displayLeft) + "][" + (y - this.displayTop) + "]");
67         }
68         getCoordinate(x, y){
69                 return [Math.floor(x / dragonblocks.settings.map.scale) + this.displayLeft, Math.floor(y / dragonblocks.settings.map.scale) + this.displayTop];
70         }
71         graphicsUpdateNode(x, y){
72                 let nodeDisplay = this.getNodeGraphics(x, y);
73                 let node = this.getNode(x, y).toNode();
74                 if(!nodeDisplay || !node)
75                         return;
76                 nodeDisplay.style.background = dragonblocks.getTexture(node.texture);
77                 nodeDisplay.style.backgroundSize = "cover";
78                 nodeDisplay.style.zIndex = node.zIndex || "1";
79         }
80         graphicsUpdate(){
81                 if(this.displayLeft < 0)
82                         this.displayLeft = 0;
83                 else if(this.displayLeft + this.displayWidth > this.width)
84                         this.displayLeft = this.width - this.displayWidth;
85                 if(this.displayTop < 0)
86                         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);
92                         }
93                 }
94         }
95         graphicsInit(){
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);
117                         }
118                 }
119                 document.body.insertBefore(map, document.body.firstChild);
120         }
121 }
122 dragonblocks.setNode = function(x, y, node){
123         dragonblocks.map.setNode(x, y, new dragonblocks.MapNode(node));
124 }
125 dragonblocks.getNode = function(x, y){
126         return dragonblocks.map.getNode(x, y);
127 }
128 dragonblocks.registerOnStarted(_ => {
129         document.getElementById("dragonblocks.map").style.visibility = "visible";
130 });
131 dragonblocks.registerOnQuit(_ => {
132         document.getElementById("dragonblocks.map").style.visibility = "hidden";
133 });