]> git.lizzy.rs Git - dragonblocks.git/commitdiff
Add Map Manager
authorElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 1 Jul 2021 13:38:18 +0000 (15:38 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 1 Jul 2021 13:38:18 +0000 (15:38 +0200)
- Adds extendible ContentMgr that can be reused later for item definitions
- Save multiple maps in world
- Fix entity map changing
- Fix (non-game) mod loading

engine/builtin.js
engine/content_mgr.js [new file with mode: 0644]
engine/init.js
engine/mainmenu.js
engine/map.js
engine/map_display.js
engine/spawned_entity.js
engine/world.js

index b33c8d93da4d719b0fcfec4ab6be94e9d4cd65ab..f5e011d78a834d3fca664747cfc3d5fdef3d1e81 100644 (file)
@@ -78,3 +78,16 @@ dragonblocks.registerGroup({
                place: "",
        }
 });
+
+dragonblocks.mapMgr.register("dragonblocks:map", class extends dragonblocks.Map
+{
+       constructor(data, mapgen)
+       {
+               if (data) {
+                       super(data, mapgen);
+               } else {
+                       super(dragonblocks.settings.map, mapgen);
+                       dragonblocks.mapgen.generate(mapgen, this);
+               }
+       }
+});
diff --git a/engine/content_mgr.js b/engine/content_mgr.js
new file mode 100644 (file)
index 0000000..2dc147f
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * content_mgr.js
+ *
+ * Copyright 2021 Elias Fleckenstein <eliasfleckenstein@web.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ *
+ */
+
+dragonblocks.ContentMgr = class
+{
+       constructor(baseClass)
+       {
+               this.baseClass = baseClass;
+               this.clear();
+       }
+
+       register(name, defClass, override)
+       {
+               if (! name)
+                       throw new Error("Missing name");
+
+               if (! name.search(":"))
+                       throw new Error("Non-namespaced name");
+
+               if (! defClass)
+                       throw new Error("Missing definition class");
+
+               if (! (defClass.prototype instanceof this.baseClass))
+                       throw new Error("Definition class does not extend base class");
+
+               let oldDef = this.getDef(name);
+
+               if (oldDef && ! override)
+                       throw new Error("Already registered");
+
+               if (! oldDef && override)
+                       throw new Error("Not registered");
+
+               this.defs[name] = defClass;
+       }
+
+       override(name, def)
+       {
+               this.register(name, def, true);
+       }
+
+       getDef(name)
+       {
+               return this.defs[name];
+       }
+
+       create(name, ...args)
+       {
+               let defClass = this.getDef(name);
+
+               if (! defClass)
+                       throw new Error("Not defined");
+
+               return new defClass(...args);
+       }
+
+       clear()
+       {
+               this.defs = {};
+       }
+};
index e8daae4db7d877803d86165d9ec5b636c169bd00..f07d3ae0beb151882904ebd67b9860c16029659e 100644 (file)
                loadingMods[modname] = false;
        };
 
-       dragonblocks.loadMods = selectedMods = _ => {
+       dragonblocks.loadMods = selectedMods => {
                dragonblocks.loadedMods = {};
 
                for (let mod in selectedMods)
        };
 
        let modules = [
+               "content_mgr",
                "assets",
                "key_handler",
                "gui",
                "node",
                "tool",
                "group",
-               "builtin",
                "map_node",
                "map_display",
                "map",
+               "builtin",
                "item_stack",
                "inventory",
                "out_stack",
index a525c0a9ee76ca7a522e342c7b61a783a16e3204..dc574140b733245a98517acd4a3a6fd5c49a9a2b 100644 (file)
                // Mods
                createWorldGUI.create("h2").innerHTML = "&ensp;Mods";
 
-               let modlistDisplay;
+               let modlistDisplay = createWorldGUI.create("ul");
 
                let updateModlist = _ => {
-                       if (modlistDisplay)
-                               clearChildren(modlistDisplay);
-                       else
-                               modlistDisplay = createWorldGUI.create("ul");
+                       clearChildren(modlistDisplay);
 
                        let oldSelectedMods = worldProperties.mods;
                        worldProperties.mods = {};
 
                                let modDisplay = modlistDisplay.appendChild(document.createElement("li"));
                                modDisplay.style.fontSize = "20px";
-                               modDisplay.innerHTML = mod;
+                               modDisplay.innerHTML = modname;
                                modDisplay.style.postion = "relative";
                                modDisplay.title = modinfo.description;
 
                                checkbox.style.right = "5px";
 
                                checkbox.addEventListener("input", _ => {
-                                       worldProperties.mods[mod] = checkbox.checked;
+                                       worldProperties.mods[modname] = checkbox.checked;
                                });
 
-                               worldProperties.mods[mod] = checkbox.checked = oldSelectedMods[mod];
+                               worldProperties.mods[modname] = checkbox.checked = oldSelectedMods[modname];
                        }
                };
 
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);
index 58c64ffcfb6265a66028fd60f3fbee405a419cc4..5e72d9ee677841a1526ee69dbc567972b6601d6f 100644 (file)
@@ -97,9 +97,9 @@ dragonblocks.MapDisplay = class
                this.autoScroll();
        }
 
-       setSkyColor(color)
+       setSky(sky)
        {
-               this.element.style.backgroundColor = color;
+               this.element.style.background = sky;
        }
 
        getActiveEntityContainer()
@@ -190,8 +190,8 @@ dragonblocks.registerOnQuit(_ => {
 });
 
 dragonblocks.registerOnStarted(_ => {
-       dragonblocks.mapDisplay.setSkyColor("skyblue");
-       dragonblocks.mapDisplay.setMap(dragonblocks.world.map);
+       dragonblocks.mapDisplay.setSky("skyblue");
+       dragonblocks.mapDisplay.setMap(dragonblocks.player.map);
        dragonblocks.mapDisplay.setAnchor(dragonblocks.player);
 
        dragonblocks.mapDisplay.setActive();
index bdc5153469cfec9df22250710c53c5ca87744395..56b8903df39bd1c24fe0c4814e559dc18a462c1a 100644 (file)
@@ -86,7 +86,7 @@ dragonblocks.SpawnedEntity = class
                map.entities.push(this);
 
                this.tmp.map = map;
-               this.tmp.display = map.entityContainer.appendChild(this.display);
+               this.tmp.display = map.entityContainer.appendChild(this.tmp.display);
 
                this.teleport(x, y);
        }
index 2579788dd4e255a47c2c3ef2fdc9f4ac0aadde7e..689d3b64d9448633f8def1c5d52ea0f1145d4c95 100644 (file)
@@ -32,35 +32,42 @@ dragonblocks.World = class
                        this.load();
                } else {
                        this.mods = properties.mods;
-
                        this.loadMods();
 
-                       this.map = new dragonblocks.Map();
+                       this.maps = {};
+                       this.mapgen = properties.mapgen;
+                       this.loadMaps({});
 
-                       this.player = new dragonblocks.Player(null, this.map);
+                       this.player = new dragonblocks.Player(null, this.maps["dragonblocks:map"]);
                        this.player.setGamemode(properties.gamemode);
-
-                       dragonblocks.mapgen.generate(properties.mapgen, this.map);
                }
        }
 
        serialize()
        {
-               return {
+               let data = {
                        mods: this.mods,
-                       map: this.map.serialize(),
                        player: this.player.serialize(),
+                       maps: {},
+                       mapgen: this.mapgen,
                };
+
+               for (let name in this.maps)
+                       data.maps[name] = this.maps[name].serialize();
+
+               return data;
        }
 
        deserialize(data)
        {
                this.mods = data.mods;
-
                this.loadMods();
 
-               this.map = new dragonblocks.Map(data.map);
-               this.player = new dragonblocks.Player(data.player, this.map);
+               this.maps = {};
+               this.mapgen = data.mapgen;
+               this.loadMaps(data.maps);
+
+               this.player = new dragonblocks.Player(data.player, this.maps["dragonblocks:map"]);
        }
 
        save()
@@ -78,6 +85,12 @@ dragonblocks.World = class
        {
                dragonblocks.loadMods(this.mods);
        }
+
+       loadMaps(data)
+       {
+               for (let name in dragonblocks.mapMgr.defs)
+                       this.maps[name] = dragonblocks.mapMgr.create(name, data[name], this.mapgen);
+       }
 };
 
 dragonblocks.World.Properties = class