flying-squid/src/lib/plugins/pvp.js
2015-12-09 01:12:04 +01:00

91 lines
No EOL
2.6 KiB
JavaScript

var Vec3 = require("vec3").Vec3;
module.exports.player=function(player,serv)
{
player.updateHealth = (health) => {
player.health = health;
player._client.write('update_health', {
food: player.food,
foodSaturation: 0.0,
health: player.health
});
};
function attackEntity(entityId)
{
var attackedEntity = serv.entities[entityId];
if(!attackedEntity || (attackedEntity.gameMode != 0 && attackedEntity.type == 'player')) return;
player.behavior('attack', {
attackedEntity: attackedEntity,
velocity: attackedEntity.position.minus(player.position).plus(new Vec3(0, 0.5, 0)).scaled(5)
}, (o) => o.attackedEntity.takeDamage(o));
}
player._client.on("use_entity", ({mouse,target} = {}) => {
if(!serv.entities[target])
{
var dragon;
for(dragon=target-1;dragon>=target-7 && !serv.entities[dragon];dragon--){}
if(serv.entities[dragon] && serv.entities[dragon].entityType==63)
target=dragon;
}
if(mouse == 1)
attackEntity(target);
});
player.commands.add({
base: 'kill',
info: 'Kill entities',
usage: '/kill <selector>',
op: true,
parse(str) {
return str || false;
},
action(sel) {
var arr = serv.selectorString(sel, player.position.scaled(1/32), player.world);
if (arr instanceof Error) return arr.toString();
if (arr == null) return 'Could not find player';
arr.map(entity => entity.takeDamage({damage:20}));
}
});
};
module.exports.entity=function(entity,serv)
{
entity.takeDamage=({sound='game.player.hurt', damage=1, velocity=new Vec3(0,0,0), maxVelocity=new Vec3(4, 4, 4), animation=true}) => {
entity.updateHealth(entity.health - damage);
serv.playSound(sound, entity.world, entity.position.scaled(1/32));
entity.sendVelocity(velocity.scaled(1/32), maxVelocity);
if(entity.health<=0) {
if(animation)
entity._writeOthers('entity_status', {
entityId: entity.id,
entityStatus: 3
});
if(entity.type!="player") {
delete serv.entities[entity.id];
setTimeout(() => {
entity.nearbyPlayers().forEach(otherPlayer => otherPlayer.despawnEntities([entity]));
},2000);
// this is the duration of the despawning animation, it should change for every entity (see EnderDragon)
}
}
else if (animation)
entity._writeOthers('animation',{
entityId:entity.id,
animation:1
});
};
if (entity.type != 'player') {
entity.updateHealth = (health) => {
entity.health = health;
}
}
};