dragonblocks.SpawnedEntity = class
{
- constructor(def, x, y){
+ constructor(def, map, x, y)
+ {
dblib.copy(this, def);
+ this.tmp = {map};
if (def instanceof dragonblocks.Entity) {
this.id = dragonblocks.getToken();
}
this.restorePhysics();
- this.tmp = {};
-
this.addGraphics();
let self = this;
this.tickInterval = setInterval(_ => {
- self.tick()
+ self.tick();
}, 100);
+
this.physicInterval = setInterval(_=>{
- self.physicsTick()
+ self.physicsTick();
});
let entityDef = this.toEntity();
self.restorePhysics();
});
- dragonblocks.spawnedEntities.push(this);
+ map.entities.push(this);
}
toEntity()
entityDef.ondespawn && entityDef.ondespawn(this);
let id = this.id;
- dragonblocks.spawnedEntities = dragonblocks.spawnedEntities.filter(entity => {
+ this.map.entities = this.map.entities.filter(entity => {
return entity.id != id;
});
clearInterval(this.physicInterval);
clearInterval(this.tickInterval);
- document.getElementById("dragonblocks.entity[" + this.id + "]").remove();
+ let display = document.getElementById("dragonblocks.entity[" + this.id + "]");
+ display && display.remove();
}
restorePhysics()
if (this.x < 0)
return false;
- if (this.x + this.width > dragonblocks.map.width)
+ if (this.x + this.width > this.map.width)
return false;
return this.collision();
if (this.y < 0)
return false;
- if (this.y + this.height > dragonblocks.map.height)
+ if (this.y + this.height > this.map.height)
return false;
return this.collision();
{
for (let ix = Math.floor(this.x); ix <= Math.ceil(this.x + this.width - 0.01) - 1; ix++)
for (let iy = Math.floor(this.y); iy <= Math.ceil(this.y + this.height - 0.01) - 1; iy++)
- if (dragonblocks.getNode(ix, iy).mobstable)
+ if (this.map.getNode(ix, iy).mobstable)
return false;
return true;
}
{
let t = new Date().getTime() / 1000;
- var oldX = this.x;
- var dtx = t - this.tx0;
+ let oldX = this.x;
+ let dtx = t - this.tx0;
if (this.ax)
this.x = this.ax * dtx * dtx + this.vx * dtx + this.x0;
this.toEntity().oncollide && this.toEntity().oncollide(this);
}
- var oldY = this.y;
- var dty = t - this.ty0;
+ let oldY = this.y;
+ let dty = t - this.ty0;
if (this.ay)
this.y = this.ay * dty * dty + this.vy * dty + this.y0;
this.updateGraphics();
}
- touch(x, y)
+ touch(map, x, y)
{
+ if (map != this.map)
+ return false;
for (let ix = Math.floor(this.x); ix <= Math.ceil(this.x + this.width - 0.01) - 1; ix++)
for (let iy = Math.floor(this.y); iy <= Math.ceil(this.y + this.height - 0.01) - 1; iy++)
if (iy == y && ix == x)
addGraphics(obj)
{
- var display = document.getElementById("dragonblocks.map").appendChild(document.createElement("div"));
+ let display = document.getElementById("dragonblocks.map").appendChild(document.createElement("div"));
display.id = "dragonblocks.entity[" + this.id + "]";
display.style.position = "absolute";
display.style.width = this.width * dragonblocks.settings.map.scale + "px";
display.style.height = this.height * dragonblocks.settings.map.scale + "px";
display.style.zIndex = "0";
-
display.addEventListener("mouseover", event => {
event.srcElement.style.boxShadow = "0 0 0 1px black inset";
});
if (! display)
return;
- display.style.left = (this.x - dragonblocks.map.displayLeft) * dragonblocks.settings.map.scale + "px";
- display.style.top = (this.y - dragonblocks.map.displayTop) * dragonblocks.settings.map.scale + "px";
+ display.style.left = (this.x - this.map.displayLeft) * dragonblocks.settings.map.scale + "px";
+ display.style.top = (this.y - this.map.displayTop) * dragonblocks.settings.map.scale + "px";
}
updateTexture()
let y = Math.ceil(entityY);
if (y - entityY <= 0.01) {
- let node = dragonblocks.getNode(x, y);
+ let node = this.map.getNode(x, y);
if (! node || node.mobstable)
return true;
if (this.gravity)
this.gravity = true;
}
-};
-dragonblocks.spawnedEntities = [];
-dragonblocks.registerOnStarted(_ => {
- if (dragonblocks.worldIsLoaded)
- for (let entity of dragonblocks.world.spawnedEntities)
- new dragonblocks.SpawnedEntity(entity);
-});
+ get map()
+ {
+ return this.tmp.map;
+ }
+};