diff --git a/src/Entity.php b/src/Entity.php index 2f0322135..baeae4966 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -20,7 +20,9 @@ */ abstract class Entity extends Position{ - private static $entityCount = 1; + public static $entityCount = 1; + public static $list = array(); + public static $needUpdate = array(); private $id; //public $passenger = null; @@ -34,7 +36,7 @@ abstract class Entity extends Position{ public $pitch; public $lastYaw; public $lastPitch; - //public $boundingBox; + public $boundingBox; public $onGround; public $positionChanged; public $velocityChanged; @@ -52,17 +54,29 @@ abstract class Entity extends Position{ protected $fireProof; private $invulnerable; + public static function getEntity($entityID){ + return isset(Entity::$list[$entityID]) ? Entity::$list[$entityID]:false; + } + public function __construct(Level $level){ $this->id = Entity::$entityCount++; $this->justCreated = true; $this->level = $level; + $this->setPosition(new Vector3(0, 0, 0)); + Entity::$list[$this->id] = $this; } public function getPosition(){ return new Position($this->x, $this->y, $this->z, $this->level); } + public function setPosition(Vector3 $pos){ + $this->x = $pos->x; + $this->y = $pos->y; + $this->z = $pos->z; + } + public function setVelocity(Vector3 $velocity){ $this->velocity = clone $velocity; } diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php new file mode 100644 index 000000000..2db5a362f --- /dev/null +++ b/src/math/AxisAlignedBB.php @@ -0,0 +1,206 @@ +minX = $minX; + $this->minY = $minY; + $this->minZ = $minZ; + $this->maxX = $maxX; + $this->maxY = $maxY; + $this->maxZ = $maxZ; + } + + public function setBounds($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + $this->minX = $minX; + $this->minY = $minY; + $this->minZ = $minZ; + $this->maxX = $maxX; + $this->maxY = $maxY; + $this->maxZ = $maxZ; + } + + public function addCoord($x, $y, $z){ + if($x < 0){ + $this->minX += $x; + }elseif($x > 0){ + $this->maxX += $x; + } + + if($y < 0){ + $this->minY += $y; + }elseif($y > 0){ + $this->maxY += $y; + } + + if($z < 0){ + $this->minZ += $z; + }elseif($z > 0){ + $this->maxZ += $z; + } + } + + public function expand($x, $y, $z){ + $this->minX -= $x; + $this->minY -= $y; + $this->minZ -= $z; + $this->maxX += $x; + $this->maxY += $y; + $this->maxZ += $z; + } + + public function offset($x, $y, $z){ + $this->minX += $x; + $this->minY += $y; + $this->minZ += $z; + $this->maxX += $x; + $this->maxY += $y; + $this->maxZ += $z; + } + + public function contract($x, $y, $z){ + $this->minX += $x; + $this->minY += $y; + $this->minZ += $z; + $this->maxX -= $x; + $this->maxY -= $y; + $this->maxZ -= $z; + } + + public function getOffsetBoundingBox($x, $y, $z){ + return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z); + } + + public function calculateXOffset(AxisAlignedBB $bb, $x){ + if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){ + return $x; + } + if($bb->maxZ <= $this->minZ or $bb->minZ >= $this->maxZ){ + return $x; + } + if($x > 0 and $bb->maxX <= $this->minX){ + $x1 = $this->minX - $bb->maxX; + if($x1 < $x){ + $x = $x1; + } + } + if($x < 0 and $bb->minX >= $this->maxX){ + $x2 = $this->maxX - $bb->minX; + if($x2 > $x){ + $x = $x2; + } + } + return $x; + } + + public function calculateYOffset(AxisAlignedBB $bb, $y){ + if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){ + return $y; + } + if($bb->maxZ <= $this->minZ or $bb->minZ >= $this->maxZ){ + return $y; + } + if($y > 0 and $bb->maxY <= $this->minY){ + $y1 = $this->minY - $bb->maxY; + if($y1 < $y){ + $y = $y1; + } + } + if($y < 0 and $bb->minY >= $this->maxY){ + $y2 = $this->maxY - $bb->minY; + if($y2 > $y){ + $y = $y2; + } + } + return $y; + } + + public function calculateZOffset(AxisAlignedBB $bb, $z){ + if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){ + return $z; + } + if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){ + return $z; + } + if($z > 0 and $bb->maxZ <= $this->minZ){ + $z1 = $this->minZ - $bb->maxZ; + if($z1 < $z){ + $z = $z1; + } + } + if($z < 0 and $bb->minZ >= $this->maxZ){ + $z2 = $this->maxZ - $bb->minZ; + if($z2 > $z){ + $z = $z2; + } + } + return $z; + } + + public function intersectsWith(AxisAlignedBB $bb){ + if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){ + return false; + } + if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){ + return false; + } + return $bb->maxZ > $this->minZ and $bb->minZ < $this->maxZ; + } + + public function isVectorInside(Vector3 $vector){ + if($vector->x <= $this->minX or $vector->x >= $this->maxX){ + return false; + } + if($vector->y <= $this->minY or $vector->y >= $this->maxY){ + return false; + } + return $vector->z > $this->minZ and $vector->z < $this->maxZ; + } + + public function getAverageEdgeLength(){ + return ($this->maxX - $this->minX + $this->maxY - $this->minY + $this->maxZ - $this->minZ) / 3; + } + + public function isVectorInYZ(Vector3 $vector){ + return $vector->y >= $this->minY and $vector->y <= $this->maxY and $vector->z >= $this->minZ and $vector->z <= $this->maxZ; + } + + public function isVectorInXZ(Vector3 $vector){ + return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->z >= $this->minZ and $vector->z <= $this->maxZ; + } + + public function isVectorInXY(Vector3 $vector){ + return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY; + } + + /* + public function calculateIntercept(...){ + + } + */ +} \ No newline at end of file