Implement swimming/gliding including AABB recalculation (#4446)

- The following events have been added:
  - PlayerToggleGlideEvent
  - PlayerToggleSwimEvent
- The following API methods have been added:
  - Entity->getSize()
  - Living->isSwimming()
  - Living->setSwimming()
  - Living->isGliding()
  - Living->setSwimming()
  - Player->toggleSwim()
  - Player->toggleGlide()
This commit is contained in:
XenialDan
2021-12-19 18:10:41 +01:00
committed by GitHub
parent 65dabefa3b
commit d41f933e7b
6 changed files with 166 additions and 9 deletions

View File

@ -116,6 +116,10 @@ abstract class Living extends Entity{
protected $sprinting = false;
/** @var bool */
protected $sneaking = false;
/** @var bool */
protected $gliding = false;
/** @var bool */
protected $swimming = false;
abstract public function getName() : string;
@ -227,6 +231,37 @@ abstract class Living extends Entity{
}
}
public function isGliding() : bool{
return $this->gliding;
}
public function setGliding(bool $value = true) : void{
$this->gliding = $value;
$this->networkPropertiesDirty = true;
$this->recalculateSize();
}
public function isSwimming() : bool{
return $this->swimming;
}
public function setSwimming(bool $value = true) : void{
$this->swimming = $value;
$this->networkPropertiesDirty = true;
$this->recalculateSize();
}
private function recalculateSize() : void{
$size = $this->getInitialSizeInfo();
if($this->isSwimming() || $this->isGliding()){
$width = $size->getWidth();
//we don't actually know an appropriate eye height for a swimming mob, but 2/3 should be good enough.
$this->setSize((new EntitySizeInfo($width, $width, $width * 2 / 3))->scale($this->getScale()));
}else{
$this->setSize($size->scale($this->getScale()));
}
}
public function getMovementSpeed() : float{
return $this->moveSpeedAttr->getValue();
}
@ -800,6 +835,8 @@ abstract class Living extends Entity{
$properties->setGenericFlag(EntityMetadataFlags::BREATHING, $this->breathing);
$properties->setGenericFlag(EntityMetadataFlags::SNEAKING, $this->sneaking);
$properties->setGenericFlag(EntityMetadataFlags::SPRINTING, $this->sprinting);
$properties->setGenericFlag(EntityMetadataFlags::GLIDING, $this->gliding);
$properties->setGenericFlag(EntityMetadataFlags::SWIMMING, $this->swimming);
}
protected function onDispose() : void{