Cleaned up death animation handling, removed dead ticking from non-Living entities

This commit is contained in:
Dylan K. Taylor 2017-11-04 20:03:40 +00:00
parent 75e469c380
commit e9e22db1e7
3 changed files with 52 additions and 11 deletions

View File

@ -3590,6 +3590,14 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
} }
} }
protected function onDeathUpdate(int $tickDiff) : bool{
if(parent::onDeathUpdate($tickDiff)){
$this->despawnFromAll(); //non-player entities rely on close() to do this for them
}
return false; //never flag players for despawn
}
public function attack(EntityDamageEvent $source){ public function attack(EntityDamageEvent $source){
if(!$this->isAlive()){ if(!$this->isAlive()){
return; return;

View File

@ -393,10 +393,6 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
/** @var bool */ /** @var bool */
public $onGround; public $onGround;
/** @var int */ /** @var int */
public $deadTicks = 0;
/** @var int */
protected $maxDeadTicks = 0;
/** @var int */
protected $age = 0; protected $age = 0;
/** @var float */ /** @var float */
@ -878,6 +874,16 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
$this->scheduleUpdate(); $this->scheduleUpdate();
} }
/**
* Called to tick entities while dead. Returns whether the entity should be flagged for despawn yet.
*
* @param int $tickDiff
* @return bool
*/
protected function onDeathUpdate(int $tickDiff) : bool{
return true;
}
public function isAlive() : bool{ public function isAlive() : bool{
return $this->health > 0; return $this->health > 0;
} }
@ -1267,13 +1273,10 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
} }
if(!$this->isAlive()){ if(!$this->isAlive()){
$this->deadTicks += $tickDiff; if($this->onDeathUpdate($tickDiff)){
if($this->deadTicks >= $this->maxDeadTicks){ $this->flagForDespawn();
$this->despawnFromAll();
if(!$this->isPlayer){
$this->flagForDespawn();
}
} }
return true; return true;
} }

View File

@ -50,6 +50,8 @@ abstract class Living extends Entity implements Damageable{
protected $attackTime = 0; protected $attackTime = 0;
/** @var int */
public $deadTicks = 0;
/** @var int */ /** @var int */
protected $maxDeadTicks = 20; protected $maxDeadTicks = 20;
@ -377,11 +379,19 @@ abstract class Living extends Entity implements Damageable{
$this->setAbsorption(max(0, $this->getAbsorption() + $source->getDamage(EntityDamageEvent::MODIFIER_ABSORPTION))); $this->setAbsorption(max(0, $this->getAbsorption() + $source->getDamage(EntityDamageEvent::MODIFIER_ABSORPTION)));
$this->broadcastEntityEvent($this->getHealth() <= 0 ? EntityEventPacket::DEATH_ANIMATION : EntityEventPacket::HURT_ANIMATION); //Ouch! if($this->isAlive()){
$this->doHitAnimation();
}else{
$this->startDeathAnimation();
}
$this->attackTime = 10; //0.5 seconds cooldown $this->attackTime = 10; //0.5 seconds cooldown
} }
protected function doHitAnimation() : void{
$this->broadcastEntityEvent(EntityEventPacket::HURT_ANIMATION);
}
public function knockBack(Entity $attacker, float $damage, float $x, float $z, float $base = 0.4){ public function knockBack(Entity $attacker, float $damage, float $x, float $z, float $base = 0.4){
$f = sqrt($x * $x + $z * $z); $f = sqrt($x * $x + $z * $z);
if($f <= 0){ if($f <= 0){
@ -421,6 +431,26 @@ abstract class Living extends Entity implements Damageable{
} }
} }
protected function onDeathUpdate(int $tickDiff) : bool{
if($this->deadTicks < $this->maxDeadTicks){
$this->deadTicks += $tickDiff;
if($this->deadTicks >= $this->maxDeadTicks){
$this->endDeathAnimation();
//TODO: spawn experience orbs here
}
}
return $this->deadTicks >= 20;
}
protected function startDeathAnimation() : void{
$this->broadcastEntityEvent(EntityEventPacket::DEATH_ANIMATION);
}
protected function endDeathAnimation() : void{
//TODO
}
public function entityBaseTick(int $tickDiff = 1) : bool{ public function entityBaseTick(int $tickDiff = 1) : bool{
Timings::$timerLivingEntityBaseTick->startTiming(); Timings::$timerLivingEntityBaseTick->startTiming();