diff --git a/doc/API.md b/doc/API.md index c248b27..f4b6a64 100644 --- a/doc/API.md +++ b/doc/API.md @@ -84,6 +84,7 @@ - [entity.getNearby()](#entitygetnearby) - [entity.getNearbyPlayers()](#entitygetnearbyplayers) - [entity.nearbyPlayers()](#entitynearbyplayers) + - [entity.takeDamage({sound='game.player.hurt', damage=1, velocity=new Vec3(0,0,0), maxVelocity=new Vec3(4, 4, 4), animation=true})](#entitytakedamagesoundgameplayerhurt-damage1-velocitynew-vec3000-maxvelocitynew-vec34-4-4-animationtrue) - [Low level Methods](#low-level-methods) - [entity._writeOthers(packetName, packetFields)](#entity_writeotherspacketname-packetfields) - [entity._writeOthersNearby(packetName, packetFields)](#entity_writeothersnearbypacketname-packetfields) @@ -129,7 +130,6 @@ - [player.updateHealth(health)](#playerupdatehealthhealth) - [player.changeWorld(world, opt)](#playerchangeworldworld-opt) - [player.spawnAPlayer(spawnedPlayer)](#playerspawnaplayerspawnedplayer) - - [player.despawnPlayers(despawnedPlayers)](#playerdespawnplayersdespawnedplayers) - [player.updateAndSpawnNearbyPlayers()](#playerupdateandspawnnearbyplayers) - [player.playSound(sound, opt)](#playerplaysoundsound-opt) - [Low level properties](#low-level-properties) @@ -575,6 +575,14 @@ Gets all nearby players regardless of what client thinks Gets all nearby players that client can see +#### entity.takeDamage({sound='game.player.hurt', damage=1, velocity=new Vec3(0,0,0), maxVelocity=new Vec3(4, 4, 4), animation=true}) + +* sound: Sound to play (default is game.player.hurt) +* damage: Damage to deal (default is based off player's weapon, player's potions, attackEntity's potions, and attackedEntity armor) +* velocity: Which way should attackedEntity move when hit +* maxVelocity: maxVelocity from consecutive hits +* animation: Play death/hit animation + ### Low level Methods #### entity._writeOthers(packetName, packetFields) @@ -860,10 +868,6 @@ The world object which the player is in (use serv.overworld, serv.netherworld, s Spawn `spawnedPlayer` for `player`. -#### player.despawnPlayers(despawnedPlayers) - -Despawn `despawnedPlayers` for `player`. - #### player.updateAndSpawnNearbyPlayers() Spawn and despawn the correct players depending on distance for `player`. diff --git a/src/lib/plugins/logout.js b/src/lib/plugins/logout.js index 97fe191..526c8e1 100644 --- a/src/lib/plugins/logout.js +++ b/src/lib/plugins/logout.js @@ -1,11 +1,5 @@ module.exports.player=function(player,serv) { - player.despawnPlayers = despawnedPlayers => { - player._client.write('entity_destroy', { - 'entityIds': despawnedPlayers.map(p => p.id) - }); - }; - player.despawnEntities = entities => player._client.write('entity_destroy', { 'entityIds': entities.map(e => e.id) }); @@ -19,7 +13,7 @@ module.exports.player=function(player,serv) UUID: player._client.uuid }] }); - player.nearbyPlayers().forEach(otherPlayer => otherPlayer.despawnPlayers([player])); + player.nearbyPlayers().forEach(otherPlayer => otherPlayer.despawnEntities([player])); delete serv.entities[player.id]; player.emit('disconnected'); var index = serv.players.indexOf(player); diff --git a/src/lib/plugins/pvp.js b/src/lib/plugins/pvp.js index 621addd..5103e02 100644 --- a/src/lib/plugins/pvp.js +++ b/src/lib/plugins/pvp.js @@ -19,29 +19,8 @@ module.exports.player=function(player,serv) player.behavior('attack', { attackedEntity: attackedEntity, - sound: 'game.player.hurt', - playSound: true, - damage: 1, - velocity: attackedEntity.position.minus(player.position).plus(new Vec3(0, 0.5, 0)).scaled(5), - maxVelocity: new Vec3(4, 4, 4), - animation: true - }, ({attackedEntity, sound, damage, velocity, maxVelocity, animation}) => { - attackedEntity.updateHealth(attackedEntity.health - damage); - serv.playSound(sound, player.world, attackedEntity.position.scaled(1/32)); - - attackedEntity.sendVelocity(velocity.scaled(1/32), maxVelocity); - - if(attackedEntity.health<=0 && animation) - attackedEntity._writeOthers('entity_status',{ - entityId:attackedEntity.id, - entityStatus:3 - }); - else if (animation) - attackedEntity._writeOthers('animation',{ - entityId:attackedEntity.id, - animation:1 - }); - }); + 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} = {}) => { @@ -49,10 +28,45 @@ module.exports.player=function(player,serv) attackEntity(target); }); + player.commands.add({ + base: 'killall', + info: 'Kill everything', + usage: '/killall', + action() { + Object.keys(serv.entities).forEach(key => serv.entities[key].takeDamage({damage:20})); + } + }); + }; -module.exports.entity=function(entity) +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 && 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;