mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-11 06:00:37 +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 util = require('util');
|
||||
var EventEmitter = require('events').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 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.entityMaxId++;
|
||||
var entity = new Entity(serv.entityMaxId);
|
||||
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.spawnPacketName = '';
|
||||
entity.entityType = entityType;
|
||||
|
|
@ -28,6 +105,8 @@ module.exports.server=function(serv) {
|
|||
if (entity.type == 'player') entity.spawnPacketName = 'named_entity_spawn';
|
||||
else if (entity.type == 'object') entity.spawnPacketName = 'spawn_entity';
|
||||
else if (entity.type == 'mob') entity.spawnPacketName = 'spawn_entity_living';
|
||||
};
|
||||
|
||||
|
||||
entity.on("positionChanged",() => {
|
||||
if(entity.position.distanceTo(entity.lastPositionPlayersUpdated)>2*32)
|
||||
|
|
@ -103,7 +182,7 @@ module.exports.server=function(serv) {
|
|||
}, entity);
|
||||
|
||||
entity.emit('positionChanged', oldPos);
|
||||
}
|
||||
};
|
||||
|
||||
entity.getSpawnPacket = () => {
|
||||
var scaledVelocity = entity.velocity.scaled(8000/32/20).floored(); // from fixed-position/second to unit => 1/8000 blocks per tick
|
||||
|
|
@ -184,67 +263,6 @@ module.exports.server=function(serv) {
|
|||
entity.nearbyEntities=updatedEntities;
|
||||
};
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
function getMoveAmount(dir, block, entity, delta, sizeSigned) {
|
||||
if (block) {
|
||||
|
|
@ -259,10 +277,12 @@ 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]);
|
||||
|
|
@ -276,3 +296,4 @@ function getFriction(vel, fric, delta) {
|
|||
function clamp(a, b, c) {
|
||||
return Math.max(a, Math.min(b, c));
|
||||
}
|
||||
};
|
||||
Loading…
Reference in a new issue