Entity: Require declaration of gravity and drag via abstract methods

this guarantees that subclasses will actually declare them.

[bc break]
This commit is contained in:
Dylan K. Taylor 2022-06-01 20:51:18 +01:00
parent f3c9b59856
commit b7e2b3e94a
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
11 changed files with 52 additions and 30 deletions

View File

@ -222,6 +222,8 @@ abstract class Entity{
$this->timings = Timings::getEntityTimings($this);
$this->size = $this->getInitialSizeInfo();
$this->drag = $this->getInitialDragMultiplier();
$this->gravity = $this->getInitialGravity();
$this->id = self::nextRuntimeId();
$this->server = $location->getWorld()->getServer();
@ -257,6 +259,21 @@ abstract class Entity{
abstract protected function getInitialSizeInfo() : EntitySizeInfo;
/**
* Returns the percentage by which the entity's velocity is reduced per tick when moving through air.
* The entity's velocity is multiplied by 1 minus this value.
*
* @return float 0-1
*/
abstract protected function getInitialDragMultiplier() : float;
/**
* Returns the downwards acceleration of the entity when falling, in blocks/tick².
*
* @return float minimum 0
*/
abstract protected function getInitialGravity() : float;
public function getNameTag() : string{
return $this->nameTag;
}

View File

@ -76,9 +76,6 @@ use const M_PI;
abstract class Living extends Entity{
protected const DEFAULT_BREATH_TICKS = 300;
protected $gravity = 0.08;
protected $drag = 0.02;
/** @var int */
protected $attackTime = 0;
@ -121,6 +118,10 @@ abstract class Living extends Entity{
/** @var bool */
protected $swimming = false;
protected function getInitialDragMultiplier() : float{ return 0.02; }
protected function getInitialGravity() : float{ return 0.08; }
abstract public function getName() : string;
protected function initEntity(CompoundTag $nbt) : void{

View File

@ -78,9 +78,6 @@ class ExperienceOrb extends Entity{
return $result;
}
public $gravity = 0.04;
public $drag = 0.02;
/** @var int */
protected $age = 0;
@ -106,6 +103,10 @@ class ExperienceOrb extends Entity{
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.25, 0.25); }
protected function getInitialDragMultiplier() : float{ return 0.02; }
protected function getInitialGravity() : float{ return 0.04; }
protected function initEntity(CompoundTag $nbt) : void{
parent::initEntity($nbt);

View File

@ -46,9 +46,6 @@ class FallingBlock extends Entity{
public static function getNetworkTypeId() : string{ return EntityIds::FALLING_BLOCK; }
protected $gravity = 0.04;
protected $drag = 0.02;
/** @var Block */
protected $block;
@ -61,6 +58,10 @@ class FallingBlock extends Entity{
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.98, 0.98); }
protected function getInitialDragMultiplier() : float{ return 0.02; }
protected function getInitialGravity() : float{ return 0.04; }
public static function parseBlockNBT(BlockFactory $factory, CompoundTag $nbt) : Block{
$blockId = 0;

View File

@ -59,9 +59,6 @@ class ItemEntity extends Entity{
/** @var Item */
protected $item;
protected $gravity = 0.04;
protected $drag = 0.02;
public $canCollide = false;
/** @var int */
@ -77,6 +74,10 @@ class ItemEntity extends Entity{
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.25, 0.25); }
protected function getInitialDragMultiplier() : float{ return 0.02; }
protected function getInitialGravity() : float{ return 0.04; }
protected function initEntity(CompoundTag $nbt) : void{
parent::initEntity($nbt);

View File

@ -56,11 +56,6 @@ class Painting extends Entity{
Facing::EAST => 3
];
/** @var float */
protected $gravity = 0.0;
/** @var float */
protected $drag = 1.0;
/** @var Vector3 */
protected $blockIn;
/** @var int */
@ -80,6 +75,10 @@ class Painting extends Entity{
return new EntitySizeInfo(0.5, 0.5);
}
protected function getInitialDragMultiplier() : float{ return 1.0; }
protected function getInitialGravity() : float{ return 0.0; }
protected function initEntity(CompoundTag $nbt) : void{
$this->setMaxHealth(1);
$this->setHealth(1);

View File

@ -41,9 +41,6 @@ class PrimedTNT extends Entity implements Explosive{
public static function getNetworkTypeId() : string{ return EntityIds::TNT; }
protected $gravity = 0.04;
protected $drag = 0.02;
/** @var int */
protected $fuse;
@ -53,6 +50,10 @@ class PrimedTNT extends Entity implements Explosive{
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.98, 0.98); }
protected function getInitialDragMultiplier() : float{ return 0.02; }
protected function getInitialGravity() : float{ return 0.04; }
public function getFuse() : int{
return $this->fuse;
}

View File

@ -52,9 +52,6 @@ class Arrow extends Projectile{
private const TAG_PICKUP = "pickup"; //TAG_Byte
public const TAG_CRIT = "crit"; //TAG_Byte
protected $gravity = 0.05;
protected $drag = 0.01;
/** @var float */
protected $damage = 2.0;
@ -77,6 +74,10 @@ class Arrow extends Projectile{
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.25, 0.25); }
protected function getInitialDragMultiplier() : float{ return 0.01; }
protected function getInitialGravity() : float{ return 0.05; }
protected function initEntity(CompoundTag $nbt) : void{
parent::initEntity($nbt);

View File

@ -32,7 +32,7 @@ use function mt_rand;
class ExperienceBottle extends Throwable{
public static function getNetworkTypeId() : string{ return EntityIds::XP_BOTTLE; }
protected $gravity = 0.07;
protected function getInitialGravity() : float{ return 0.07; }
public function getResultDamage() : int{
return -1;

View File

@ -52,9 +52,6 @@ class SplashPotion extends Throwable{
public static function getNetworkTypeId() : string{ return EntityIds::SPLASH_POTION; }
protected $gravity = 0.05;
protected $drag = 0.01;
/** @var bool */
protected $linger = false;
protected PotionType $potionType;
@ -64,6 +61,8 @@ class SplashPotion extends Throwable{
parent::__construct($location, $shootingEntity, $nbt);
}
protected function getInitialGravity() : float{ return 0.05; }
public function saveNBT() : CompoundTag{
$nbt = parent::saveNBT();
$nbt->setShort("PotionId", PotionTypeIdMap::getInstance()->toId($this->getPotionType()));

View File

@ -29,11 +29,12 @@ use pocketmine\math\RayTraceResult;
abstract class Throwable extends Projectile{
protected $gravity = 0.03;
protected $drag = 0.01;
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.25, 0.25); }
protected function getInitialDragMultiplier() : float{ return 0.01; }
protected function getInitialGravity() : float{ return 0.03; }
protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{
parent::onHitBlock($blockHit, $hitResult);
$this->flagForDespawn();