dragonblocks.Map = class
{
- constructor()
+ constructor(data)
{
- dblib.copy(this, dragonblocks.world.map);
- this.data = this.data || this.content;
- delete this.content;
+ if (data)
+ this.deserialize(data);
+ else
+ this.clear();
+
+ this.initGraphics();
+ this.updateGraphics();
+ }
+
+ serialize()
+ {
+ return {
+ data: this.data,
+ width: this.width,
+ height: this.height,
+ displayLeft: this.displayLeft,
+ displayTop: this.displayTop,
+ 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.displayLeft = data.displayLeft;
+ this.displayTop = data.displayTop;
+ 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, new dragonblocks.MapNode().createFromMapNode(data.data[x][y]));
+ }
- this.initGraphics();
+ for (let entity of data.entities)
+ new dragonblocks.SpawnedEntity(entity);
+ }
+
+ 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;
+ }
+
+ 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);
}
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)
- {
- if (this.withinBounds(x, y))
- return this.data[x][y];
- }
-
- withinBounds(x, y)
- {
- return x < this.width && y < this.height && x >= 0 && y >= 0;
- }
-
getNodeDisplay(x, y)
{
return document.getElementById("dragonblocks.map.node[" + (x - this.displayLeft) + "][" + (y - this.displayTop) + "]");
}
}
}
-};
-dragonblocks.setNode = (x, y, node) => {
- dragonblocks.map.setNode(x, y, new dragonblocks.MapNode(node));
+ addStructure(name, msg, pos)
+ {
+ this.structures[name] = this.structures[name] || [];
+ this.structures[name].push({msg, pos});
+ }
+
+ spawnEntity(name, x, y)
+ {
+ let def = dragonblocks.entities[name];
+
+ if (def)
+ return new dragonblocks.SpawnedEntity(def, this, x, y);
+ }
+
};
-dragonblocks.getNode = (x, y) => {
- return dragonblocks.map.getNode(x, y);
+dragonblocks.onActivateCallbacks = [];
+dragonblocks.registerOnActivate = func => {
+ dragonblocks.onActivateCallbacks.push(func);
};
dragonblocks.registerOnStarted(_ => {