Improved entity ticking

This commit is contained in:
Shoghi Cervantes 2014-10-06 13:10:59 +02:00
parent d53ba52d32
commit de11cce154
4 changed files with 34 additions and 19 deletions

View File

@ -46,6 +46,8 @@ class DroppedItem extends Entity{
protected $gravity = 0.04;
protected $drag = 0.02;
public $canCollide = false;
protected function initEntity(){
$this->namedtag->id = new String("id", "Item");
$this->setMaxHealth(5);
@ -87,7 +89,7 @@ class DroppedItem extends Entity{
$friction = 1 - $this->drag;
if($this->onGround){
if($this->onGround and ($this->motionX != 0 or $this->motionZ != 0)){
$friction = $this->getLevel()->getBlock(new Vector3($this->getFloorX(), $this->getFloorY() - 1, $this->getFloorZ()))->frictionFactor * $friction;
}

View File

@ -134,6 +134,8 @@ abstract class Entity extends Position implements Metadatable{
public $fireTicks;
public $airTicks;
public $namedtag;
public $canCollide = true;
protected $isStatic = false;
protected $isColliding = false;
@ -555,7 +557,9 @@ abstract class Entity extends Position implements Metadatable{
$pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
];
Server::broadcastPacket($this->hasSpawned, $pk);
foreach($this->hasSpawned as $player){
$player->dataPacket($pk);
}
if($this instanceof Player){
$this->motionX = 0;

View File

@ -43,6 +43,8 @@ class FallingBlock extends Entity{
protected $drag = 0.02;
protected $blockId = 0;
public $canCollide = false;
protected function initEntity(){
$this->namedtag->id = new String("id", "FallingSand");
if(isset($this->namedtag->Tile)){

View File

@ -728,7 +728,10 @@ class Level implements ChunkManager, Metadatable{
for($z = $minZ; $z < $maxZ; ++$z){
for($x = $minX; $x < $maxX; ++$x){
for($y = $minY - 1; $y < $maxY; ++$y){
$this->getBlock(new Vector3($x, $y, $z))->collidesWithBB($bb, $collides);
$block = $this->getBlock(new Vector3($x, $y, $z));
if(!($block instanceof Air)){
$block->collidesWithBB($bb, $collides);
}
}
}
}
@ -767,11 +770,13 @@ class Level implements ChunkManager, Metadatable{
$collides = [];
//TODO: optimize this loop, check collision cube boundaries
for($z = $minZ; $z < $maxZ; ++$z){
for($x = $minX; $x < $maxX; ++$x){
for($y = $minY - 1; $y < $maxY; ++$y){
$this->getBlock(new Vector3($x, $y, $z))->collidesWithBB($bb, $collides);
$block = $this->getBlock(new Vector3($x, $y, $z));
if(!($block instanceof Air)){
$block->collidesWithBB($bb, $collides);
}
}
}
}
@ -1233,7 +1238,7 @@ class Level implements ChunkManager, Metadatable{
}
/**
* Returns the entities near the current one inside the AxisAlignedBB
* Returns the entities colliding the current one inside the AxisAlignedBB
*
* @param AxisAlignedBB $bb
* @param Entity $entity
@ -1243,16 +1248,18 @@ class Level implements ChunkManager, Metadatable{
public function getCollidingEntities(AxisAlignedBB $bb, Entity $entity = null){
$nearby = [];
$minX = ($bb->minX - 2) >> 4;
$maxX = ($bb->maxX + 2) >> 4;
$minZ = ($bb->minZ - 2) >> 4;
$maxZ = ($bb->maxZ + 2) >> 4;
if($entity->canCollide){
$minX = Math::floorFloat(($bb->minX - 2) / 16);
$maxX = Math::floorFloat(($bb->maxX - 2) / 16);
$minZ = Math::floorFloat(($bb->minZ - 2) / 16);
$maxZ = Math::floorFloat(($bb->maxZ - 2) / 16);
for($x = $minX; $x <= $maxX; ++$x){
for($z = $minZ; $z <= $maxZ; ++$z){
foreach($this->getChunkEntities($x, $z) as $ent){
if($ent !== $entity and ($entity === null or $entity->canCollideWith($ent)) and $ent->boundingBox->intersectsWith($bb)){
$nearby[] = $ent;
for($x = $minX; $x <= $maxX; ++$x){
for($z = $minZ; $z <= $maxZ; ++$z){
foreach($this->getChunkEntities($x, $z) as $ent){
if($ent !== $entity and ($entity === null or $entity->canCollideWith($ent)) and $ent->boundingBox->intersectsWith($bb)){
$nearby[] = $ent;
}
}
}
}
@ -1272,10 +1279,10 @@ class Level implements ChunkManager, Metadatable{
public function getNearbyEntities(AxisAlignedBB $bb, Entity $entity = null){
$nearby = [];
$minX = ($bb->minX - 2) >> 4;
$maxX = ($bb->maxX + 2) >> 4;
$minZ = ($bb->minZ - 2) >> 4;
$maxZ = ($bb->maxZ + 2) >> 4;
$minX = Math::floorFloat(($bb->minX - 2) / 16);
$maxX = Math::floorFloat(($bb->maxX - 2) / 16);
$minZ = Math::floorFloat(($bb->minZ - 2) / 16);
$maxZ = Math::floorFloat(($bb->maxZ - 2) / 16);
for($x = $minX; $x <= $maxX; ++$x){
for($z = $minZ; $z <= $maxZ; ++$z){