]> git.lizzy.rs Git - dragonblocks.git/blobdiff - engine/map.js
Add Map Manager
[dragonblocks.git] / engine / map.js
index db4211abb6c3d5a27b2c439abfdb21b33ee3be79..7dff2bc09ead0d5b997cf27519aa272ab0e50b49 100644 (file)
@@ -31,10 +31,7 @@ dragonblocks.Map = class
                this.entityContainer.style.position = "absolute";
                this.entityContainer.style.visibility = "hidden";
 
-               if (data)
-                       this.deserialize(data);
-               else
-                       this.clear();
+               this.deserialize(data);
        }
 
        serialize()
@@ -43,8 +40,6 @@ dragonblocks.Map = class
                        data: this.data,
                        width: this.width,
                        height: this.height,
-                       displayLeft: this.displayLeft,
-                       displayTop: this.displayTop,
                        structures: this.structures,
                        entities: dblib.removeTmp(this.entities),
                };
@@ -53,21 +48,22 @@ dragonblocks.Map = class
        deserialize(data)
        {
                this.data = [];
+
                this.width = data.width;
                this.height = data.height;
-               this.displayLeft = data.displayLeft;
-               this.displayTop = data.displayTop;
+
                this.entities = [];
-               this.structures = data.structures;
+               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(data.data[x][y]));
+                               this.setNode(x, y, data.data ? new dragonblocks.MapNode().createFromMapNode(data.data[x][y]) : new dragonblocks.MapNode("air"));
                }
 
-               for (let entity of data.entities)
-                       new dragonblocks.SpawnedEntity(entity);
+               if (data.entities)
+                       for (let entity of data.entities)
+                               new dragonblocks.SpawnedEntity(entity, this);
        }
 
        setActive()
@@ -82,23 +78,6 @@ dragonblocks.Map = class
                this.entityContainer.style.visibility = "hidden";
        }
 
-       clear()
-       {
-               this.data = [];
-               this.width = dragonblocks.settings.map.width;
-               this.height = dragonblocks.settings.map.height;
-               this.displayTop = dragonblocks.settings.map.height / 2;
-               this.displayLeft = dragonblocks.settings.map.width / 2 - 5;
-               this.entities = [];
-               this.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("air"));
-               }
-       }
-
        withinBounds(x, y)
        {
                return x < this.width && y < this.height && x >= 0 && y >= 0;
@@ -162,6 +141,8 @@ dragonblocks.Map = class
 
 };
 
+dragonblocks.mapMgr = new dragonblocks.ContentMgr(dragonblocks.Map);
+
 dragonblocks.onActivateCallbacks = [];
 dragonblocks.registerOnActivate = func => {
        dragonblocks.onActivateCallbacks.push(func);