From cc7f12739dbe92a0c3fecfe1f702ab2b40378b7d Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 4 Nov 2014 12:23:42 +0100 Subject: [PATCH] Added vertical and horizontal collision detection to Entities --- src/pocketmine/entity/Entity.php | 14 ++++++++------ src/pocketmine/entity/Projectile.php | 12 ++++++++++-- src/pocketmine/entity/Snowball.php | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 5583e0259..49fc6eec7 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -140,7 +140,10 @@ abstract class Entity extends Location implements Metadatable{ public $canCollide = true; protected $isStatic = false; - protected $isColliding = false; + + public $isCollided = false; + public $isCollidedHorizontally = false; + public $isCollidedVertically = false; public $noDamageTicks; private $justCreated; @@ -811,11 +814,6 @@ abstract class Entity extends Location implements Metadatable{ return false; } - public function collision(){ - $this->isColliding = true; - $this->fallDistance = 0; - } - public function move($dx, $dy, $dz){ if($dx == 0 and $dz == 0 and $dy == 0){ @@ -1009,7 +1007,11 @@ abstract class Entity extends Location implements Metadatable{ $this->onGround = false; } } + $this->isCollided = $this->onGround; }else{ + $this->isCollidedVertically = $movY != $dy; + $this->isCollidedHorizontally = ($movX != $dx or $movZ != $dz); + $this->isCollided = ($this->isCollidedHorizontally or $this->isCollidedVertically); $this->onGround = ($movY != $dy and $movY < 0); } $this->updateFallState($dy, $this->onGround); diff --git a/src/pocketmine/entity/Projectile.php b/src/pocketmine/entity/Projectile.php index f926afdad..33a4872b1 100644 --- a/src/pocketmine/entity/Projectile.php +++ b/src/pocketmine/entity/Projectile.php @@ -37,6 +37,8 @@ abstract class Projectile extends Entity{ public $shootingEntity = null; protected $damage = 0; + private $hadCollision = false; + protected function initEntity(){ $this->setMaxHealth(1); $this->setHealth(1); @@ -90,7 +92,9 @@ abstract class Projectile extends Entity{ $movingObjectPosition = null; - $this->motionY -= $this->gravity; + if(!$this->isCollided){ + $this->motionY -= $this->gravity; + } $this->keepMovement = $this->checkObstruction($this->x, ($this->boundingBox->minY + $this->boundingBox->maxY) / 2, $this->z); @@ -159,12 +163,16 @@ abstract class Projectile extends Entity{ $this->move($this->motionX, $this->motionY, $this->motionZ); - if($this->onGround and ($this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0)){ + if($this->isCollided and !$this->hadCollision){ + $this->hadCollision = true; + $this->motionX = 0; $this->motionY = 0; $this->motionZ = 0; $this->server->getPluginManager()->callEvent(new ProjectileHitEvent($this)); + }elseif(!$this->isCollided and !$this->hadCollision){ + $this->hadCollision = false; } if(!$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0){ diff --git a/src/pocketmine/entity/Snowball.php b/src/pocketmine/entity/Snowball.php index 3248b8c7b..db1daba4f 100644 --- a/src/pocketmine/entity/Snowball.php +++ b/src/pocketmine/entity/Snowball.php @@ -52,7 +52,7 @@ class Snowball extends Projectile{ $hasUpdate = parent::onUpdate($currentTick); - if($this->age > 1200 or $this->onGround){ + if($this->age > 1200 or $this->isCollided){ $this->kill(); $hasUpdate = true; }