mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-06 11:40:46 +00:00
move some of entities.js in other files
This commit is contained in:
parent
94a11951f4
commit
a1a260c1da
3 changed files with 120 additions and 112 deletions
|
|
@ -2,8 +2,6 @@ var Entity=require("prismarine-entity");
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
util.inherits(Entity, EventEmitter);
|
util.inherits(Entity, EventEmitter);
|
||||||
var blocks=require("minecraft-data")(require("../version")).blocks;
|
|
||||||
var mobs=require("minecraft-data")(require("../version")).entitiesByName;
|
|
||||||
var vec3 = require("vec3");
|
var vec3 = require("vec3");
|
||||||
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
@ -60,22 +58,6 @@ module.exports.server=function(serv,options) {
|
||||||
mob.updateAndSpawn();
|
mob.updateAndSpawn();
|
||||||
};
|
};
|
||||||
|
|
||||||
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(vec3(0,0,0)))
|
|
||||||
if (entity.type == 'mob') entity.sendPosition(oldPosAndOnGround);
|
|
||||||
})
|
|
||||||
).catch((err)=> setTimeout(() => {throw err;},0));
|
|
||||||
});
|
|
||||||
|
|
||||||
serv.destroyEntity = entity => {
|
serv.destroyEntity = entity => {
|
||||||
serv._writeNearby('entity_destroy', {
|
serv._writeNearby('entity_destroy', {
|
||||||
entityIds: [entity.id]
|
entityIds: [entity.id]
|
||||||
|
|
@ -108,6 +90,24 @@ module.exports.entity=function(serv,entity){
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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(vec3(0,0,0)))
|
||||||
|
if (entity.type == 'mob') entity.sendPosition(oldPosAndOnGround);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
entity.on("positionChanged",() => {
|
entity.on("positionChanged",() => {
|
||||||
if(entity.position.distanceTo(entity.lastPositionPlayersUpdated)>2*32)
|
if(entity.position.distanceTo(entity.lastPositionPlayersUpdated)>2*32)
|
||||||
entity.updateAndSpawn();
|
entity.updateAndSpawn();
|
||||||
|
|
@ -124,66 +124,6 @@ module.exports.entity=function(serv,entity){
|
||||||
serv.destroyEntity(entity);
|
serv.destroyEntity(entity);
|
||||||
};
|
};
|
||||||
|
|
||||||
entity.calculatePhysics = async (delta) => {
|
|
||||||
if (entity.gravity) {
|
|
||||||
addGravity(entity, 'x', delta);
|
|
||||||
addGravity(entity, 'y', delta);
|
|
||||||
addGravity(entity, 'z', delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
var vSign = getSign(entity.velocity);
|
|
||||||
var sizeSigned = vec3(vSign.x * entity.size.x, vSign.y * entity.size.y, vSign.z * entity.size.z);
|
|
||||||
|
|
||||||
var xVec = entity.position.offset(entity.velocity.x*delta + sizeSigned.x/2, 0, 0).scaled(1/32).floored();
|
|
||||||
var yVec = entity.position.offset(0, entity.velocity.y*delta + sizeSigned.y/2, 0).scaled(1/32).floored();
|
|
||||||
var zVec = entity.position.offset(0, 0, entity.velocity.z*delta + sizeSigned.z/2).scaled(1/32).floored();
|
|
||||||
|
|
||||||
// Get block for each (x/y/z)Vec, check to avoid duplicate getBlockTypes
|
|
||||||
var xBlock = blocks[await entity.world.getBlockType(xVec)].boundingBox == 'block';
|
|
||||||
var yBlock = yVec.equals(xVec) ? xBlock : blocks[await entity.world.getBlockType(yVec)].boundingBox == 'block';
|
|
||||||
var zBlock = zVec.equals(yVec) ? yBlock : (zVec.equals(xVec) ? xBlock : blocks[await entity.world.getBlockType(zVec)].boundingBox == 'block');
|
|
||||||
|
|
||||||
var old = entity.position.clone();
|
|
||||||
|
|
||||||
if (xBlock || yBlock || zBlock) {
|
|
||||||
entity.velocity.x = getFriction(entity.velocity.x, entity.friction.x, delta);
|
|
||||||
entity.velocity.z = getFriction(entity.velocity.x, entity.friction.x, delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
var oldPos = entity.position.clone();
|
|
||||||
|
|
||||||
entity.position.x += getMoveAmount('x', xBlock, entity, delta, sizeSigned.x);
|
|
||||||
entity.position.y += getMoveAmount('y', yBlock, entity, delta, sizeSigned.y);
|
|
||||||
entity.position.z += getMoveAmount('z', zBlock, entity, delta, sizeSigned.z);
|
|
||||||
|
|
||||||
//serv.emitParticle(30, serv.overworld, entity.position.scaled(1/32), { size: vec3(0, 0, 0) });
|
|
||||||
return { oldPos: oldPos, onGround: yBlock}
|
|
||||||
};
|
|
||||||
|
|
||||||
entity.sendPosition = ({oldPos,onGround}) => {
|
|
||||||
var diff = entity.position.minus(oldPos);
|
|
||||||
|
|
||||||
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
|
||||||
serv._writeNearby('entity_teleport', {
|
|
||||||
entityId: entity.id,
|
|
||||||
x: entity.position.x,
|
|
||||||
y: entity.position.y,
|
|
||||||
z: entity.position.z,
|
|
||||||
yaw: entity.yaw,
|
|
||||||
pitch: entity.pitch,
|
|
||||||
onGround: onGround
|
|
||||||
}, entity);
|
|
||||||
else serv._writeNearby('rel_entity_move', {
|
|
||||||
entityId: entity.id,
|
|
||||||
dX: diff.x,
|
|
||||||
dY: diff.y,
|
|
||||||
dZ: diff.z,
|
|
||||||
onGround: onGround
|
|
||||||
}, entity);
|
|
||||||
|
|
||||||
entity.emit('positionChanged', oldPos);
|
|
||||||
};
|
|
||||||
|
|
||||||
entity.getSpawnPacket = () => {
|
entity.getSpawnPacket = () => {
|
||||||
var scaledVelocity = entity.velocity.scaled(8000/32/20).floored(); // from fixed-position/second to unit => 1/8000 blocks per tick
|
var scaledVelocity = entity.velocity.scaled(8000/32/20).floored(); // from fixed-position/second to unit => 1/8000 blocks per tick
|
||||||
if (entity.type == 'player') {
|
if (entity.type == 'player') {
|
||||||
|
|
@ -262,38 +202,4 @@ module.exports.entity=function(serv,entity){
|
||||||
|
|
||||||
entity.nearbyEntities=updatedEntities;
|
entity.nearbyEntities=updatedEntities;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function getMoveAmount(dir, block, entity, delta, sizeSigned) {
|
|
||||||
if (block) {
|
|
||||||
entity.velocity[dir] = 0;
|
|
||||||
return Math.floor(-1 * (entity.position[dir] + sizeSigned/2 - floorInDirection(entity.position[dir], -sizeSigned)));
|
|
||||||
} else {
|
|
||||||
return Math.floor(entity.velocity[dir] * delta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSign(vec) {
|
|
||||||
return vec3(Math.sign(vec.x), Math.sign(vec.y), Math.sign(vec.z));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function floorInDirection(a, b) {
|
|
||||||
return b < 0 ? Math.floor(a) : Math.ceil(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function addGravity(entity, dir, delta) {
|
|
||||||
if (entity.velocity[dir] < entity.terminalvelocity[dir] && entity.velocity[dir] > -entity.terminalvelocity[dir]) {
|
|
||||||
entity.velocity[dir] = clamp(-entity.terminalvelocity[dir], entity.velocity[dir] + entity.gravity[dir] * delta, entity.terminalvelocity[dir]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getFriction(vel, fric, delta) {
|
|
||||||
return vel > 0 ? Math.max(0, vel - fric*delta) : Math.min(0, vel + fric*delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
function clamp(a, b, c) {
|
|
||||||
return Math.max(a, Math.min(b, c));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
74
src/lib/plugins/physics.js
Normal file
74
src/lib/plugins/physics.js
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
var blocks=require("minecraft-data")(require("../version")).blocks;
|
||||||
|
var vec3 = require("vec3");
|
||||||
|
|
||||||
|
module.exports.entity=function(serv,entity){
|
||||||
|
entity.calculatePhysics = async (delta) => {
|
||||||
|
if (entity.gravity) {
|
||||||
|
addGravity(entity, 'x', delta);
|
||||||
|
addGravity(entity, 'y', delta);
|
||||||
|
addGravity(entity, 'z', delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
var vSign = getSign(entity.velocity);
|
||||||
|
var sizeSigned = vec3(vSign.x * entity.size.x, vSign.y * entity.size.y, vSign.z * entity.size.z);
|
||||||
|
|
||||||
|
var xVec = entity.position.offset(entity.velocity.x*delta + sizeSigned.x/2, 0, 0).scaled(1/32).floored();
|
||||||
|
var yVec = entity.position.offset(0, entity.velocity.y*delta + sizeSigned.y/2, 0).scaled(1/32).floored();
|
||||||
|
var zVec = entity.position.offset(0, 0, entity.velocity.z*delta + sizeSigned.z/2).scaled(1/32).floored();
|
||||||
|
|
||||||
|
// Get block for each (x/y/z)Vec, check to avoid duplicate getBlockTypes
|
||||||
|
var xBlock = blocks[await entity.world.getBlockType(xVec)].boundingBox == 'block';
|
||||||
|
var yBlock = yVec.equals(xVec) ? xBlock : blocks[await entity.world.getBlockType(yVec)].boundingBox == 'block';
|
||||||
|
var zBlock = zVec.equals(yVec) ? yBlock : (zVec.equals(xVec) ? xBlock : blocks[await entity.world.getBlockType(zVec)].boundingBox == 'block');
|
||||||
|
|
||||||
|
var old = entity.position.clone();
|
||||||
|
|
||||||
|
if (xBlock || yBlock || zBlock) {
|
||||||
|
entity.velocity.x = getFriction(entity.velocity.x, entity.friction.x, delta);
|
||||||
|
entity.velocity.z = getFriction(entity.velocity.x, entity.friction.x, delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
var oldPos = entity.position.clone();
|
||||||
|
|
||||||
|
entity.position.x += getMoveAmount('x', xBlock, entity, delta, sizeSigned.x);
|
||||||
|
entity.position.y += getMoveAmount('y', yBlock, entity, delta, sizeSigned.y);
|
||||||
|
entity.position.z += getMoveAmount('z', zBlock, entity, delta, sizeSigned.z);
|
||||||
|
|
||||||
|
//serv.emitParticle(30, serv.overworld, entity.position.scaled(1/32), { size: vec3(0, 0, 0) });
|
||||||
|
return { oldPos: oldPos, onGround: yBlock}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function getMoveAmount(dir, block, entity, delta, sizeSigned) {
|
||||||
|
if (block) {
|
||||||
|
entity.velocity[dir] = 0;
|
||||||
|
return Math.floor(-1 * (entity.position[dir] + sizeSigned/2 - floorInDirection(entity.position[dir], -sizeSigned)));
|
||||||
|
} else {
|
||||||
|
return Math.floor(entity.velocity[dir] * delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSign(vec) {
|
||||||
|
return vec3(Math.sign(vec.x), Math.sign(vec.y), Math.sign(vec.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function floorInDirection(a, b) {
|
||||||
|
return b < 0 ? Math.floor(a) : Math.ceil(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function addGravity(entity, dir, delta) {
|
||||||
|
if (entity.velocity[dir] < entity.terminalvelocity[dir] && entity.velocity[dir] > -entity.terminalvelocity[dir]) {
|
||||||
|
entity.velocity[dir] = clamp(-entity.terminalvelocity[dir], entity.velocity[dir] + entity.gravity[dir] * delta, entity.terminalvelocity[dir]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFriction(vel, fric, delta) {
|
||||||
|
return vel > 0 ? Math.max(0, vel - fric*delta) : Math.min(0, vel + fric*delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
function clamp(a, b, c) {
|
||||||
|
return Math.max(a, Math.min(b, c));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -83,4 +83,32 @@ module.exports.player=function(serv,player)
|
||||||
flags: 0x00
|
flags: 0x00
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.entity=function(serv,entity){
|
||||||
|
|
||||||
|
|
||||||
|
entity.sendPosition = ({oldPos,onGround}) => {
|
||||||
|
var diff = entity.position.minus(oldPos);
|
||||||
|
|
||||||
|
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
||||||
|
serv._writeNearby('entity_teleport', {
|
||||||
|
entityId: entity.id,
|
||||||
|
x: entity.position.x,
|
||||||
|
y: entity.position.y,
|
||||||
|
z: entity.position.z,
|
||||||
|
yaw: entity.yaw,
|
||||||
|
pitch: entity.pitch,
|
||||||
|
onGround: onGround
|
||||||
|
}, entity);
|
||||||
|
else serv._writeNearby('rel_entity_move', {
|
||||||
|
entityId: entity.id,
|
||||||
|
dX: diff.x,
|
||||||
|
dY: diff.y,
|
||||||
|
dZ: diff.z,
|
||||||
|
onGround: onGround
|
||||||
|
}, entity);
|
||||||
|
|
||||||
|
entity.emit('positionChanged', oldPos);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
Loading…
Reference in a new issue