]> git.lizzy.rs Git - dragonblocks.git/blobdiff - engine/map.js
Add Map Manager
[dragonblocks.git] / engine / map.js
index 4ff558d85dfcf90e0c7a888852d7da1839b9b01f..7dff2bc09ead0d5b997cf27519aa272ab0e50b49 100644 (file)
 
 dragonblocks.Map = class
 {
-       constructor()
+       constructor(data)
        {
-               dblib.copy(this, dragonblocks.world.map);
-               this.data = this.data || this.content;
-               delete this.content;
+               this.active = false;
+
+               this.entityContainer = dragonblocks.mapDisplay.element.appendChild(document.createElement("div"));
+               this.entityContainer.style.position = "absolute";
+               this.entityContainer.style.visibility = "hidden";
+
+               this.deserialize(data);
+       }
+
+       serialize()
+       {
+               return {
+                       data: this.data,
+                       width: this.width,
+                       height: this.height,
+                       structures: this.structures,
+                       entities: dblib.removeTmp(this.entities),
+               };
        }
 
-       load()
+       deserialize(data)
        {
-               for (let x = 0; x < this.width; x++)
+               this.data = [];
+
+               this.width = data.width;
+               this.height = data.height;
+
+               this.entities = [];
+               this.structures = data.structures || {};
+
+               for (let x = 0; x < this.width; x++) {
+                       this.data[x] = [];
                        for (let y = 0; y < this.height; y++)
-                               this.setNode(x, y, new dragonblocks.MapNode().createFromMapNode(this.data[x][y]));
+                               this.setNode(x, y, data.data ? new dragonblocks.MapNode().createFromMapNode(data.data[x][y]) : new dragonblocks.MapNode("air"));
+               }
+
+               if (data.entities)
+                       for (let entity of data.entities)
+                               new dragonblocks.SpawnedEntity(entity, this);
+       }
 
-               this.initGraphics();
+       setActive()
+       {
+               this.active = true;
+               this.entityContainer.style.visibility = "inherit";
+       }
+
+       setInactive()
+       {
+               this.active = false;
+               this.entityContainer.style.visibility = "hidden";
+       }
+
+       withinBounds(x, y)
+       {
+               return x < this.width && y < this.height && x >= 0 && y >= 0;
+       }
+
+       getNode(x, y)
+       {
+               if (this.withinBounds(x, y))
+                       return this.data[x][y];
        }
 
        setNode(x, y, node)
        {
+               node = new dragonblocks.MapNode(node);
+
                if (this.withinBounds(x, y)) {
                        let oldNode = this.data[x][y];
                        let oldNodeDef = oldNode instanceof dragonblocks.MapNode && oldNode.toNode();
-                       oldNodeDef && oldNodeDef.onremove && oldNodeDef.onremove(x, y);
-
-                       for (let func of dragonblocks.onRemoveNodeCallbacks)
-                               func(x, y);
+                       oldNodeDef && oldNodeDef.onremove && oldNodeDef.onremove(this, x, y);
 
                        this.data[x][y] = node;
 
                        let nodeDef = node.toNode();
-                       nodeDef.onset && nodeDef.onset(x, y);
-
-                       for (let func of dragonblocks.onSetNodeCallbacks)
-                               func(x, y);
+                       nodeDef.onset && nodeDef.onset(this, x, y);
 
-                       this.updateNodeGraphics(x, y);
+                       this.active && dragonblocks.mapDisplay.updateNode(x, y);
                }
        }
 
@@ -71,112 +117,33 @@ dragonblocks.Map = class
                                        continue;
 
                                let nodeDef = node.toNode();
-                               nodeDef.onactivate && nodeDef.onactivate(ix, iy);
+                               nodeDef.onactivate && nodeDef.onactivate(this, ix, iy);
 
-                               for(let func of dragonblocks.onActivateNodeCallbacks)
-                                       func(ix, iy);
+                               for (let func of dragonblocks.onActivateCallbacks)
+                                       func(this, ix, iy);
                        }
                }
        }
 
-       getNode(x, y)
+       addStructure(name, msg, pos)
        {
-               if (this.withinBounds(x, y))
-                       return this.data[x][y];
+               this.structures[name] = this.structures[name] || [];
+               this.structures[name].push({msg, pos});
        }
 
-       withinBounds(x, y)
+       spawnEntity(name, x, y)
        {
-               return x < this.width && y < this.height && x >= 0 && y >= 0;
-       }
+               let def = dragonblocks.entities[name];
 
-       getNodeDisplay(x, y)
-       {
-               return document.getElementById("dragonblocks.map.node[" + (x - this.displayLeft) + "][" + (y - this.displayTop) + "]");
+               if (def)
+                       return new dragonblocks.SpawnedEntity(def, this, x, y);
        }
 
-       getScreenCoordinates(x, y)
-       {
-               return [Math.floor(x / dragonblocks.settings.map.scale) + this.displayLeft, Math.floor(y / dragonblocks.settings.map.scale) + this.displayTop];
-       }
-
-       updateNodeGraphics(x, y)
-       {
-               let nodeDisplay = this.getNodeDisplay(x, y);
-
-               if (! nodeDisplay)
-                       return;
-
-               let nodeDef = this.getNode(x, y).toNode();
-
-               if (! nodeDef)
-                       return;
-
-               nodeDisplay.style.background = dragonblocks.getTexture(nodeDef.texture);
-               nodeDisplay.style.backgroundSize = "cover";
-               nodeDisplay.style.zIndex = nodeDef.zIndex || "1";
-       }
-
-       updateGraphics()
-       {
-               if (this.displayLeft < 0)
-                       this.displayLeft = 0;
-               else if (this.displayLeft + this.displayWidth > this.width)
-                       this.displayLeft = this.width - this.displayWidth;
-
-               if (this.displayTop < 0)
-                       this.displayTop = 0;
-               else if (this.displayTop + this.displayHeight > this.height)
-                       this.displayTop = this.height - this.displayHeight;
-
-               for (let x = 0; x < this.displayWidth; x++) {
-                       for(let y = 0; y < this.displayHeight; y++) {
-                               this.updateNodeGraphics(x + this.displayLeft, y + this.displayTop);
-                       }
-               }
-       }
-
-       initGraphics()
-       {
-               this.displayWidth = Math.min(Math.ceil(innerWidth / dragonblocks.settings.map.scale), this.width);
-               this.displayHeight = Math.min(Math.ceil(innerHeight / dragonblocks.settings.map.scale), this.height);
-
-               let display = document.body.insertBefore(document.createElement("div"), document.body.firstChild);
-               display.id = "dragonblocks.map";
-               display.style.width = this.displayWidth * dragonblocks.settings.map.scale + "px";
-               display.style.height = this.displayHeight * dragonblocks.settings.map.scale + "px";
-               display.style.position = "fixed";
-               display.style.top = "0px";
-               display.style.left = "0px";
-               display.style.backgroundColor = "skyblue";
-               display.style.visibility = "hidden";
-
-               for (let x = 0; x < this.displayWidth; x++){
-                       for (let y = 0; y < this.displayHeight; y++){
-                               let nodeDisplay = display.appendChild(document.createElement("div"));
-                               nodeDisplay.id = "dragonblocks.map.node[" + x + "][" + y + "]";
-                               nodeDisplay.style.position = "absolute";
-                               nodeDisplay.style.top = y * dragonblocks.settings.map.scale + "px";
-                               nodeDisplay.style.left = x * dragonblocks.settings.map.scale + "px";
-                               nodeDisplay.style.width = dragonblocks.settings.map.scale + "px";
-                               nodeDisplay.style.height = dragonblocks.settings.map.scale + "px";
-                       }
-               }
-       }
 };
 
-dragonblocks.setNode = (x, y, node) => {
-       dragonblocks.map.setNode(x, y, new dragonblocks.MapNode(node));
-};
+dragonblocks.mapMgr = new dragonblocks.ContentMgr(dragonblocks.Map);
 
-dragonblocks.getNode = (x, y) => {
-       return dragonblocks.map.getNode(x, y);
+dragonblocks.onActivateCallbacks = [];
+dragonblocks.registerOnActivate = func => {
+       dragonblocks.onActivateCallbacks.push(func);
 };
-
-dragonblocks.registerOnStarted(_ => {
-       document.getElementById("dragonblocks.map").style.visibility = "visible";
-});
-
-dragonblocks.registerOnQuit(_ => {
-       document.getElementById("dragonblocks.map").style.visibility = "hidden";
-});