get ticking back to server plugin (more efficient not to listen lot of time on an event)

This commit is contained in:
Romain Beaumont 2015-11-17 00:56:32 +01:00
parent 4cefdedf75
commit 5530f7b1de

View file

@ -67,7 +67,23 @@ module.exports.server=function(serv,options) {
world: entity.world
});
delete serv.entities[entity.id];
}
};
serv.on('tick', function(delta) {
Promise.all(
Object.keys(serv.entities).map(async (id) => {
var entity = serv.entities[id];
if (entity.deathTime && Date.now() - entity.bornTime >= entity.deathTime) {
entity.destroy();
return;
}
if (!entity.velocity || !entity.size) return;
var oldPosAndOnGround = await entity.calculatePhysics(delta);
if (!oldPosAndOnGround.oldPos.equals(new Vec3(0,0,0)))
if (entity.type == 'mob') entity.sendPosition(oldPosAndOnGround);
})
).catch((err)=> setTimeout(() => {throw err;},0));
});
};
module.exports.player=function(player,serv){
@ -145,24 +161,6 @@ module.exports.entity=function(entity,serv){
};
serv.on('tick', async function(delta) {
if (entity.deathTime && Date.now() - entity.bornTime >= entity.deathTime) {
entity.destroy();
return;
}
if (!entity.velocity || !entity.size) return;
var oldPosAndOnGround;
try {
oldPosAndOnGround = await entity.calculatePhysics(delta);
}
catch(err){
setTimeout(() => {throw err;},0)
}
if (!oldPosAndOnGround.oldPos.equals(new Vec3(0,0,0)))
if (entity.type == 'mob') entity.sendPosition(oldPosAndOnGround);
});
entity.on("positionChanged",() => {
if(entity.position.distanceTo(entity.lastPositionPlayersUpdated)>2*32)
entity.updateAndSpawn();