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);
+ }
+ }
+});
--- /dev/null
+/*
+ * 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 = {};
+ }
+};
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",
// Mods
createWorldGUI.create("h2").innerHTML = " 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];
}
};
this.entityContainer.style.position = "absolute";
this.entityContainer.style.visibility = "hidden";
- if (data)
- this.deserialize(data);
- else
- this.clear();
+ this.deserialize(data);
}
serialize()
data: this.data,
width: this.width,
height: this.height,
- displayLeft: this.displayLeft,
- displayTop: this.displayTop,
structures: this.structures,
entities: dblib.removeTmp(this.entities),
};
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()
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;
};
+dragonblocks.mapMgr = new dragonblocks.ContentMgr(dragonblocks.Map);
+
dragonblocks.onActivateCallbacks = [];
dragonblocks.registerOnActivate = func => {
dragonblocks.onActivateCallbacks.push(func);
this.autoScroll();
}
- setSkyColor(color)
+ setSky(sky)
{
- this.element.style.backgroundColor = color;
+ this.element.style.background = sky;
}
getActiveEntityContainer()
});
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();
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);
}
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()
{
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