Entity collisions, bounding boxes for entites and blocks

This commit is contained in:
GameHerobrine 2022-10-18 21:40:03 +03:00
parent 9154c0fc3d
commit 1448fd21b9
19 changed files with 265 additions and 118 deletions

View file

@ -10,7 +10,7 @@ abstract class Creature extends Living{
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:1, "generic");
$this->update();
//$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type));
$this->size = 1;
$this->width = 1;
}
public function spawn($player){

View file

@ -4,8 +4,12 @@ class Entity extends Position{
const TYPE = -1;
const CLASS_TYPE = -1;
public $isCollidable;
public $canBeAttacked;
/**
* @var AxisAlignedBB
*/
public $boundingBox;
public $age;
public $air;
public $spawntime;
@ -43,7 +47,8 @@ class Entity extends Position{
public $lastUpdate;
public $linkedEntity = null;
public $check = true;
public $size = 1;
public $width = 1;
public $height = 1;
public $random;
public $inAction = false;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
@ -83,11 +88,13 @@ class Entity extends Position{
$this->yaw = isset($this->data["yaw"]) ? (float) $this->data["yaw"]:0;
$this->pitch = isset($this->data["pitch"]) ? (float) $this->data["pitch"]:0;
$this->position = array("level" => $this->level, "x" => &$this->x, "y" => &$this->y, "z" => &$this->z, "yaw" => &$this->yaw, "pitch" => &$this->pitch);
$this->height = 0.98;
switch($this->class){
case ENTITY_PLAYER:
$this->player = $this->data["player"];
$this->setHealth($this->health, "generic");
$this->size = 1.2;
$this->width = 1.2;
$this->height = 1.9;
$this->canBeAttacked = true;
break;
case ENTITY_ITEM:
@ -100,13 +107,13 @@ class Entity extends Position{
}
$this->setHealth(5, "generic");
$this->server->schedule(6010, array($this, "update")); //Despawn
$this->update();
$this->size = 0.75;
$this->height = 0.75;
$this->width = 0.75;
break;
case ENTITY_FALLING:
$this->setHealth(PHP_INT_MAX, "generic");
$this->update();
$this->size = 0.98;
$this->height = 0.98;
$this->width = 0.98;
break;
case ENTITY_OBJECT:
$this->x = isset($this->data["TileX"]) ? $this->data["TileX"]:$this->x;
@ -114,13 +121,17 @@ class Entity extends Position{
$this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z;
$this->setHealth(1, "generic");
//$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type));
$this->size = 1;
$this->width = 1;
$this->height = 1;
if($this->type === OBJECT_SNOWBALL){
$this->server->schedule(1210, array($this, "update")); //Despawn
$this->update();
//$this->update();
}
break;
}
$this->radius = &$this->width / 2;
$this->boundingBox = new AxisAlignedBB($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius);
$this->update();
$this->updateLast();
$this->updatePosition();
if($this->y < 0 and $this->class !== ENTITY_PLAYER){
@ -131,24 +142,59 @@ class Entity extends Position{
/**
* @param mixed $e Entity instance or EID
* @return string|false if failed
*/
public static function getNameOf($e){
if($e instanceof Entity){
return $e->getName();
}elseif(($ent = ServerAPI::request()->api->entity->get($e)) != false){
return $ent->getName();
}
}
public static function getSizeOf($e){
if($e instanceof Entity){
return $e->getSize();
}elseif(($ent = ServerAPI::request()->api->entity->get($e)) != false){
return $ent->getSize();
}elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){
return $e->getName();
}
return false;
}
public function getSize(){
return $this->size;
/**
* @param mixed $e Entity instance or EID
* @return number|false if failed
*/
public static function getHeightOf($e){
if($e instanceof Entity){
return $e->getHeight();
}elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){
return $e->getHeight();
}
return false;
}
/**
* @param mixed $e Entity instance or EID
* @return number|false if failed
*/
public static function getWidthOf($e){
if($e instanceof Entity){
return $e->getWidth();
}elseif(($e = ServerAPI::request()->api->entity->get($e)) != false){
return $e->getWidth();
}
return false;
}
/**
* Get an entity height
* @return number
*/
public function getHeight($a){
return $this->height;
}
/**
* Get an entity width
* @return number
*/
public function getWidth(){
return $this->width;
}
public function lookOn($target){
@ -349,20 +395,20 @@ class Entity extends Position{
}
if($this->isStatic === false){
$startX = floor($this->x - 0.5 - $this->size - 1);
$startX = floor($this->x - 0.5 - $this->width - 1);
//prefix for flying when player on fence
$y = floor($this->y - 1);
$yC = ceil($this->y - 1);
$startZ = floor($this->z - 0.5 - $this->size - 1);
$endX = ceil($this->x - 0.5 + $this->size + 1);
$endZ = ceil($this->z - 0.5 + $this->size + 1);
$startZ = floor($this->z - 0.5 - $this->width - 1);
$endX = ceil($this->x - 0.5 + $this->width + 1);
$endZ = ceil($this->z - 0.5 + $this->width + 1);
$support = false;
$isFlying = true;
for($z = $startZ; $z <= $endZ; ++$z){
for($x = $startX; $x <= $endX; ++$x){
$v = new Vector3($x, $y, $z);
$v1 = new Vector3($x, $yC, $z);
if($this->isSupport($v, $this->size)){
if($this->isSupport($v, $this->width)){
$b = $this->level->getBlock($v);
if($b->isSolid === true){
$support = true;
@ -371,7 +417,7 @@ class Entity extends Position{
}elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
$isFlying = false;
}
}elseif($this->isSupport($v1, $this->size)){
}elseif($this->isSupport($v1, $this->width)){
$b = $this->level->getBlock($v1);
if($b->isSolid === true){
$support = true;
@ -390,6 +436,14 @@ class Entity extends Position{
$update = false;
if(($this->class !== ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $support === false){
$drag = 0.2;
$blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ));
foreach($blocks as $b){
$this->speedX = $b->calculateXOffset($this->boundingBox, $this->speedX);
$this->speedY = $b->calculateYOffset($this->boundingBox, $this->speedY);
$this->speedZ = $b->calculateZOffset($this->boundingBox, $this->speedZ);
}
if($this->speedX != 0){
$this->speedX -= $this->speedX * $drag;
$this->x += $this->speedX;
@ -430,10 +484,13 @@ class Entity extends Position{
$this->y = $ny;
$update = true;
}
}
if($support === false){
$this->speedY -= 0.8;
$this->speedY -= 0.08;
$update = true;
}else{
//$this->speedX = 0;
@ -742,13 +799,18 @@ class Entity extends Position{
$this->pitch += $pitch;
$this->pitch %= 90;
$this->server->query("UPDATE entities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z.", pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";");
$this->updateAABB();
}
public function updateAABB(){
$this->boundingBox = new AxisAlignedBB($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius);
}
public function updatePosition(){
$this->server->query("UPDATE entities SET level = '".$this->level->getName()."', x = ".$this->x.", y = ".$this->y.", z = ".$this->z.", pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";");
$this->sendMoveUpdate();
$this->sendMotion();
$this->updateAABB();
}
public function setPosition(Vector3 $pos, $yaw = false, $pitch = false){
@ -900,8 +962,6 @@ class Entity extends Position{
$this->knockBack($d, $d1);
$this->sendMotion();
//$this->move(new Vector3($this->speedX, $this->speedY, $this->speedZ));
//$this->sendMoveUpdate();
}
if($this->class === ENTITY_PLAYER and ($this->player instanceof Player)){
$points = 0;
@ -997,7 +1057,10 @@ class Entity extends Position{
return false;
}
public function setSize($w, $h){
$this->width = $w;
$this->height = $h;
}
public function knockBack($d, $d1)
{
@ -1022,5 +1085,27 @@ class Entity extends Position{
public function __toString(){
return "Entity(x={$this->x},y={$this->y},z={$this->z},level=".$this->level->getName().",class={$this->class},type={$this->type})";
}
/*
* Deprecated methods.
* Those methods were left only for compability with older plugins
*/
/**
* @deprecated Use {@link getHeightOf} or {@link getWidthOf} instead
* @throws Exception
*/
public static function getSizeOf($e){
throw new Exception("Use getHeightOf or getWidthOf method instead of this.");
}
/**
* @deprecated Use {@link getHeight} or {@link getWidth} instead
* @throws Exception
*/
public function getSize(){
throw new Exception("Use getHeight or getWidth method instead of this.");
}
}

View file

@ -7,9 +7,9 @@ class Chicken extends Animal{
$server = ServerAPI::request();
$server->schedule(mt_rand(0,6000) + 6000, array($this, "dropAnEgg"));
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic");
$this->update();
$this->setName('Chicken');
$this->size = $this->isBaby() ? 0.35 : 0.7;
$this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7);
$this->update();
}
public function isFood($id){
return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS;

View file

@ -4,10 +4,10 @@ class Cow extends Animal{
const TYPE = MOB_COW;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic");
$this->update();
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic");
$this->setSize($this->isBaby() ? 0.45 : 0.9 , $this->isBaby() ? 0.7 : 1.4);
$this->setName('Cow');
$this->size = $this->isBaby() ? 0.65 : 1.4;
$this->update();
}
public function isFood($id){
return $id === WHEAT;

View file

@ -5,12 +5,10 @@ class Pig extends Animal{
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic");
$this->pathFinder = new Pathfinder($this);
$this->update();
$this->server = ServerAPI::request();
//$this->server->schedule(60, array($this, "testMovement"));
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9);
$this->setName('Pig');
$this->size = $this->isBaby() ? 0.60 : 1.1875;
$this->update();
}
public function isFood($id){
return $id === POTATO || $id === CARROT || $id === BEETROOT;
@ -21,43 +19,4 @@ class Pig extends Animal{
array(($this->fire > 0 ? COOKED_PORKCHOP:RAW_PORKCHOP), 0, mt_rand(0,2)),
);
}
public function testMovement(){ //test movement, move 1 block each 10 seconds, looks terrible ingame
$this->pathFinder->updateEntityPos(ceil($this->x), ceil($this->y), ceil($this->z));
$this->path = $this->pathFinder->buildTo(new Vector3(ceil($this->x) + mt_rand(0,4), ceil($this->y), ceil($this->z) + mt_rand(0,4)));
$this->followPath();
}
public function followPath(){
$point = null;
if($this->path instanceof Path){
$point = $this->path->increaseIndexAndGetPoint();
}else{
$this->server->schedule(40, array($this, "testMovement"), false);
return;
}
if($point != null){
console($point);
$xDiff = $point->x - $this->x;
$zDiff = $point->z - $this->z;
console($this);
$this->move(new Vector3($xDiff, 0, $zDiff), $this->yaw, $this->pitch);
$this->updateMovement();
$this->handleUpdate($this);
$this->server->schedule(10, array($this, "followPath"), false);
}else{
$this->server->schedule(40, array($this, "testMovement"), false);
}
}
public function handleUpdate($entity){
$players = $this->server->api->player->getAll($entity->level);
$pk = new MoveEntityPacket_PosRot;
$pk->eid = $entity->eid;
$pk->x = $entity->x;
$pk->y = $entity->y;
$pk->z = $entity->z;
$pk->yaw = $entity->yaw;
$pk->pitch = $entity->pitch;
$this->server->api->player->broadcastPacket($players, $pk);
}
}

View file

@ -7,9 +7,9 @@ class Sheep extends Animal{
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic");
$this->update();
$this->setName('Sheep');
$this->size = $this->isBaby() ? 0.65 : 1.3;
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.675 : 1.3);
$this->update();
}
public function getDrops(){

View file

@ -4,10 +4,11 @@ class Creeper extends Monster{
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:20, "generic");
$this->update();
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:16, "generic");
$this->setName('Creeper');
$this->size = 1.6; //not original
$this->setSize(0.6, 1.7);
$this->update();
}
public function getDrops(){

View file

@ -3,10 +3,10 @@ class PigZombie extends Monster{
const TYPE = MOB_PIGMAN;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 20, "generic");
$this->update();
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 12, "generic");
$this->setSize(0.3, 1.95);
$this->setName('Pigman');
$this->size = 1.8; //not original
$this->update();
}
public function getDrops(){

View file

@ -4,10 +4,10 @@ class Skeleton extends Monster{
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 20, "generic");
$this->update();
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic");
$this->setName('Skeleton');
$this->size = 1.9;
$this->setSize(0.6, 1.99);
$this->update();
}
public function getDrops(){

View file

@ -4,10 +4,10 @@ class Spider extends Monster{
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:20, "generic");
$this->update();
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic");
$this->setName('Spider');
$this->size = 0.6; //not original
$this->setSize(1.4, 0.9);
$this->update();
}
public function getDrops(){

View file

@ -4,10 +4,10 @@ class Zombie extends Monster{
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 20, "generic");
$this->update();
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 12, "generic");
$this->setName('Zombie');
$this->size = 1.95;
$this->setSize(0.6, 1.95);
$this->update();
}
public function getDrops(){

View file

@ -16,7 +16,7 @@ class Minecart extends Vehicle{
$this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z;
$this->setHealth(1, "generic");
//$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type));
$this->size = 1;
$this->width = 1;
/*$this->moveVector[Entity::NORTH] = new Vector3(-1, 0, 0);
$this->moveVector[Entity::SOUTH] = new Vector3(1, 0, 0);
$this->moveVector[Entity::EAST] = new Vector3(0, 0, -1);

View file

@ -8,7 +8,7 @@ class Painting extends Hanging{
$this->y = isset($this->data["TileY"]) ? $this->data["TileY"]:$this->y;
$this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z;
$this->setHealth(1, "generic");
$this->size = 1;
$this->width = 1;
$this->isStatic = true;
}