mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-10 21:50:33 +00:00
entity plugins
This commit is contained in:
parent
39dc37f94d
commit
94a11951f4
1 changed files with 221 additions and 200 deletions
|
|
@ -1,18 +1,95 @@
|
||||||
var Entity=require("prismarine-entity");
|
var Entity=require("prismarine-entity");
|
||||||
|
var util = require('util');
|
||||||
|
var EventEmitter = require('events').EventEmitter;
|
||||||
|
util.inherits(Entity, EventEmitter);
|
||||||
var blocks=require("minecraft-data")(require("../version")).blocks;
|
var blocks=require("minecraft-data")(require("../version")).blocks;
|
||||||
var mobs=require("minecraft-data")(require("../version")).entitiesByName;
|
var mobs=require("minecraft-data")(require("../version")).entitiesByName;
|
||||||
var vec3 = require("vec3");
|
var vec3 = require("vec3");
|
||||||
var EventEmitter = require('events').EventEmitter;
|
|
||||||
var util = require('util');
|
|
||||||
|
|
||||||
module.exports.server=function(serv) {
|
var path = require('path');
|
||||||
|
var requireIndex = require('requireindex');
|
||||||
|
var plugins = requireIndex(path.join(__dirname,'..', 'plugins'));
|
||||||
|
|
||||||
util.inherits(Entity, EventEmitter);
|
module.exports.server=function(serv,options) {
|
||||||
|
|
||||||
serv.initEntity = (type, entityType, world, position) => {
|
serv.initEntity = (type, entityType, world, position) => {
|
||||||
serv.entityMaxId++;
|
serv.entityMaxId++;
|
||||||
var entity = new Entity(serv.entityMaxId);
|
var entity = new Entity(serv.entityMaxId);
|
||||||
EventEmitter.call(entity);
|
EventEmitter.call(entity);
|
||||||
|
|
||||||
|
Object.keys(plugins)
|
||||||
|
.filter(pluginName => plugins[pluginName].entity!=undefined)
|
||||||
|
.forEach(pluginName => plugins[pluginName].entity(serv, entity, options));
|
||||||
|
|
||||||
|
entity.initEntity(type, entityType, world, position);
|
||||||
|
|
||||||
|
serv.emit("newEntity",entity);
|
||||||
|
|
||||||
|
return entity;
|
||||||
|
};
|
||||||
|
|
||||||
|
serv.spawnObject = (type, world, position, {pitch=0,yaw=0,velocity=vec3(0,0,0),data=1,itemId,itemDamage=0}={}) => {
|
||||||
|
var object = serv.initEntity('object', type, world, position.scaled(32).floored());
|
||||||
|
object.data = data;
|
||||||
|
object.velocity = velocity.scaled(32).floored();
|
||||||
|
object.pitch = pitch;
|
||||||
|
object.yaw = yaw;
|
||||||
|
object.gravity = vec3(0, -20*32, 0);
|
||||||
|
object.terminalvelocity = vec3(27*32, 27*32, 27*32);
|
||||||
|
object.friction = vec3(10*32, 0, 10*32).floored();
|
||||||
|
object.size = vec3(0.25*32, 0.25*32, 0.25*32); // Hardcoded, will be dependent on type!
|
||||||
|
object.deathTime = 60*1000; // 60 seconds
|
||||||
|
object.itemId = itemId;
|
||||||
|
object.itemDamage = itemDamage;
|
||||||
|
|
||||||
|
object.updateAndSpawn();
|
||||||
|
};
|
||||||
|
|
||||||
|
serv.spawnMob = (type, world, position, {pitch=0,yaw=0,headPitch=0,velocity=vec3(0,0,0),metadata=[]}={}) => {
|
||||||
|
var mob = serv.initEntity('mob', type, world, position.scaled(32).floored());
|
||||||
|
mob.velocity = velocity.scaled(32).floored();
|
||||||
|
mob.pitch = pitch;
|
||||||
|
mob.headPitch = headPitch;
|
||||||
|
mob.yaw = yaw;
|
||||||
|
mob.gravity = vec3(0, -20*32, 0);
|
||||||
|
mob.terminalvelocity = vec3(27*32, 27*32, 27*32);
|
||||||
|
mob.friction = vec3(10*32, 0, 10*32);
|
||||||
|
mob.size = vec3(0.75, 1.75, 0.75);
|
||||||
|
mob.metadata = metadata;
|
||||||
|
|
||||||
|
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._writeNearby('entity_destroy', {
|
||||||
|
entityIds: [entity.id]
|
||||||
|
}, {
|
||||||
|
position: entity.position,
|
||||||
|
world: entity.world
|
||||||
|
});
|
||||||
|
delete serv.entities[entity.id];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.entity=function(serv,entity){
|
||||||
|
|
||||||
|
entity.initEntity=(type, entityType, world, position)=>{
|
||||||
entity.type = type;
|
entity.type = type;
|
||||||
entity.spawnPacketName = '';
|
entity.spawnPacketName = '';
|
||||||
entity.entityType = entityType;
|
entity.entityType = entityType;
|
||||||
|
|
@ -28,6 +105,8 @@ module.exports.server=function(serv) {
|
||||||
if (entity.type == 'player') entity.spawnPacketName = 'named_entity_spawn';
|
if (entity.type == 'player') entity.spawnPacketName = 'named_entity_spawn';
|
||||||
else if (entity.type == 'object') entity.spawnPacketName = 'spawn_entity';
|
else if (entity.type == 'object') entity.spawnPacketName = 'spawn_entity';
|
||||||
else if (entity.type == 'mob') entity.spawnPacketName = 'spawn_entity_living';
|
else if (entity.type == 'mob') entity.spawnPacketName = 'spawn_entity_living';
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
entity.on("positionChanged",() => {
|
entity.on("positionChanged",() => {
|
||||||
if(entity.position.distanceTo(entity.lastPositionPlayersUpdated)>2*32)
|
if(entity.position.distanceTo(entity.lastPositionPlayersUpdated)>2*32)
|
||||||
|
|
@ -103,7 +182,7 @@ module.exports.server=function(serv) {
|
||||||
}, entity);
|
}, entity);
|
||||||
|
|
||||||
entity.emit('positionChanged', oldPos);
|
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
|
||||||
|
|
@ -184,95 +263,37 @@ module.exports.server=function(serv) {
|
||||||
entity.nearbyEntities=updatedEntities;
|
entity.nearbyEntities=updatedEntities;
|
||||||
};
|
};
|
||||||
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
serv.spawnObject = (type, world, position, {pitch=0,yaw=0,velocity=vec3(0,0,0),data=1,itemId,itemDamage=0}={}) => {
|
function getMoveAmount(dir, block, entity, delta, sizeSigned) {
|
||||||
var object = serv.initEntity('object', type, world, position.scaled(32).floored());
|
|
||||||
object.data = data;
|
|
||||||
object.velocity = velocity.scaled(32).floored();
|
|
||||||
object.pitch = pitch;
|
|
||||||
object.yaw = yaw;
|
|
||||||
object.gravity = vec3(0, -20*32, 0);
|
|
||||||
object.terminalvelocity = vec3(27*32, 27*32, 27*32);
|
|
||||||
object.friction = vec3(10*32, 0, 10*32).floored();
|
|
||||||
object.size = vec3(0.25*32, 0.25*32, 0.25*32); // Hardcoded, will be dependent on type!
|
|
||||||
object.deathTime = 60*1000; // 60 seconds
|
|
||||||
object.itemId = itemId;
|
|
||||||
object.itemDamage = itemDamage;
|
|
||||||
|
|
||||||
object.updateAndSpawn();
|
|
||||||
}
|
|
||||||
|
|
||||||
serv.spawnMob = (type, world, position, {pitch=0,yaw=0,headPitch=0,velocity=vec3(0,0,0),metadata=[]}={}) => {
|
|
||||||
var mob = serv.initEntity('mob', type, world, position.scaled(32).floored());
|
|
||||||
mob.velocity = velocity.scaled(32).floored();
|
|
||||||
mob.pitch = pitch;
|
|
||||||
mob.headPitch = headPitch;
|
|
||||||
mob.yaw = yaw;
|
|
||||||
mob.gravity = vec3(0, -20*32, 0);
|
|
||||||
mob.terminalvelocity = vec3(27*32, 27*32, 27*32);
|
|
||||||
mob.friction = vec3(10*32, 0, 10*32);
|
|
||||||
mob.size = vec3(0.75, 1.75, 0.75);
|
|
||||||
mob.metadata = metadata;
|
|
||||||
|
|
||||||
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._writeNearby('entity_destroy', {
|
|
||||||
entityIds: [entity.id]
|
|
||||||
}, {
|
|
||||||
position: entity.position,
|
|
||||||
world: entity.world
|
|
||||||
});
|
|
||||||
delete serv.entities[entity.id];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getMoveAmount(dir, block, entity, delta, sizeSigned) {
|
|
||||||
if (block) {
|
if (block) {
|
||||||
entity.velocity[dir] = 0;
|
entity.velocity[dir] = 0;
|
||||||
return Math.floor(-1 * (entity.position[dir] + sizeSigned/2 - floorInDirection(entity.position[dir], -sizeSigned)));
|
return Math.floor(-1 * (entity.position[dir] + sizeSigned/2 - floorInDirection(entity.position[dir], -sizeSigned)));
|
||||||
} else {
|
} else {
|
||||||
return Math.floor(entity.velocity[dir] * delta);
|
return Math.floor(entity.velocity[dir] * delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSign(vec) {
|
function getSign(vec) {
|
||||||
return vec3(Math.sign(vec.x), Math.sign(vec.y), Math.sign(vec.z));
|
return vec3(Math.sign(vec.x), Math.sign(vec.y), Math.sign(vec.z));
|
||||||
}
|
}
|
||||||
|
|
||||||
function floorInDirection(a, b) {
|
|
||||||
|
function floorInDirection(a, b) {
|
||||||
return b < 0 ? Math.floor(a) : Math.ceil(a);
|
return b < 0 ? Math.floor(a) : Math.ceil(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addGravity(entity, dir, delta) {
|
|
||||||
|
function addGravity(entity, dir, delta) {
|
||||||
if (entity.velocity[dir] < entity.terminalvelocity[dir] && entity.velocity[dir] > -entity.terminalvelocity[dir]) {
|
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]);
|
entity.velocity[dir] = clamp(-entity.terminalvelocity[dir], entity.velocity[dir] + entity.gravity[dir] * delta, entity.terminalvelocity[dir]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFriction(vel, fric, delta) {
|
function getFriction(vel, fric, delta) {
|
||||||
return vel > 0 ? Math.max(0, vel - fric*delta) : Math.min(0, vel + fric*delta);
|
return vel > 0 ? Math.max(0, vel - fric*delta) : Math.min(0, vel + fric*delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
function clamp(a, b, c) {
|
function clamp(a, b, c) {
|
||||||
return Math.max(a, Math.min(b, c));
|
return Math.max(a, Math.min(b, c));
|
||||||
}
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue