Block: Make recalculation of BB non-dependent on block position

This now computes BBs relative to 0,0,0 and then offsets them as appropriate. This requires less boilerplate code and also furthers the goal of separating block types from instances.
This commit is contained in:
Dylan K. Taylor 2018-06-21 19:58:28 +01:00
parent 56b04fa0bb
commit 99a0c2a188
25 changed files with 149 additions and 407 deletions

View File

@ -76,23 +76,9 @@ class Anvil extends Fallable{
$inset = 0.125;
if($this->meta & 0x01){ //east/west
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z + $inset,
$this->x + 1,
$this->y + 1,
$this->z + 1 - $inset
);
return new AxisAlignedBB(0, 0, $inset, 1, 1, 1 - $inset);
}else{
return new AxisAlignedBB(
$this->x + $inset,
$this->y,
$this->z,
$this->x + 1 - $inset,
$this->y + 1,
$this->z + 1
);
return new AxisAlignedBB($inset, 0, 0, 1 - $inset, 1, 1);
}
}

View File

@ -55,14 +55,7 @@ class Bed extends Transparent{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 0.5625,
$this->z + 1
);
return new AxisAlignedBB(0, 0, 0, 1, 0.5625, 1);
}
public function isHeadPart() : bool{

View File

@ -657,6 +657,9 @@ class Block extends Position implements BlockIds, Metadatable{
public function getCollisionBoxes() : array{
if($this->collisionBoxes === null){
$this->collisionBoxes = $this->recalculateCollisionBoxes();
foreach($this->collisionBoxes as $bb){
$bb->offset($this->x, $this->y, $this->z);
}
}
return $this->collisionBoxes;
@ -679,6 +682,9 @@ class Block extends Position implements BlockIds, Metadatable{
public function getBoundingBox() : ?AxisAlignedBB{
if($this->boundingBox === null){
$this->boundingBox = $this->recalculateBoundingBox();
if($this->boundingBox !== null){
$this->boundingBox->offset($this->x, $this->y, $this->z);
}
}
return $this->boundingBox;
}
@ -687,14 +693,7 @@ class Block extends Position implements BlockIds, Metadatable{
* @return AxisAlignedBB|null
*/
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
return new AxisAlignedBB(0, 0, 0, 1, 1, 1);
}
/**

View File

@ -54,15 +54,8 @@ class Cactus extends Transparent{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x + 0.0625,
$this->y + 0.0625,
$this->z + 0.0625,
$this->x + 0.9375,
$this->y + 0.9375,
$this->z + 0.9375
);
static $shrinkSize = 0.0625;
return new AxisAlignedBB($shrinkSize, $shrinkSize, $shrinkSize, 1 - $shrinkSize, 1 - $shrinkSize, 1 - $shrinkSize);
}
public function onEntityCollide(Entity $entity) : void{

View File

@ -48,16 +48,15 @@ class Cake extends Transparent implements FoodSource{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
$f = $this->getDamage() * 0.125; //1 slice width
return new AxisAlignedBB(
$this->x + 0.0625 + $f,
$this->y,
$this->z + 0.0625,
$this->x + 1 - 0.0625,
$this->y + 0.5,
$this->z + 1 - 0.0625
0.0625 + $f,
0,
0.0625,
1 - 0.0625,
0.5,
1 - 0.0625
);
}

View File

@ -50,15 +50,7 @@ class Carpet extends Flowable{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 0.0625,
$this->z + 1
);
return new AxisAlignedBB(0, 0, 0, 1, 0.0625, 1);
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{

View File

@ -52,14 +52,7 @@ class Chest extends Transparent{
protected function recalculateBoundingBox() : ?AxisAlignedBB{
//these are slightly bigger than in PC
return new AxisAlignedBB(
$this->x + 0.025,
$this->y,
$this->z + 0.025,
$this->x + 0.975,
$this->y + 0.95,
$this->z + 0.975
);
return new AxisAlignedBB(0.025, 0, 0.025, 0.975, 0.95, 0.975);
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{

View File

@ -78,12 +78,12 @@ class CobblestoneWall extends Transparent{
}
return new AxisAlignedBB(
$this->x + ($west ? 0 : $inset),
$this->y,
$this->z + ($north ? 0 : $inset),
$this->x + 1 - ($east ? 0 : $inset),
$this->y + 1.5,
$this->z + 1 - ($south ? 0 : $inset)
($west ? 0 : $inset),
0,
($north ? 0 : $inset),
1 - ($east ? 0 : $inset),
1.5,
1 - ($south ? 0 : $inset)
);
}

View File

@ -54,18 +54,10 @@ abstract class Door extends Transparent{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
$f = 0.1875;
$damage = $this->getFullDamage();
$bb = new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 2,
$this->z + 1
);
$bb = new AxisAlignedBB(0, 0, 0, 1, 2, 1);
$j = $damage & 0x03;
$isOpen = (($damage & 0x04) > 0);
@ -74,126 +66,42 @@ abstract class Door extends Transparent{
if($j === 0){
if($isOpen){
if(!$isRight){
$bb->setBounds(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + $f
);
$bb->setBounds(0, 0, 0, 1, 1, $f);
}else{
$bb->setBounds(
$this->x,
$this->y,
$this->z + 1 - $f,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(0, 0, 1 - $f, 1, 1, 1);
}
}else{
$bb->setBounds(
$this->x,
$this->y,
$this->z,
$this->x + $f,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(0, 0, 0, $f, 1, 1);
}
}elseif($j === 1){
if($isOpen){
if(!$isRight){
$bb->setBounds(
$this->x + 1 - $f,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(1 - $f, 0, 0, 1, 1, 1);
}else{
$bb->setBounds(
$this->x,
$this->y,
$this->z,
$this->x + $f,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(0, 0, 0, $f, 1, 1);
}
}else{
$bb->setBounds(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + $f
);
$bb->setBounds(0, 0, 0, 1, 1, $f);
}
}elseif($j === 2){
if($isOpen){
if(!$isRight){
$bb->setBounds(
$this->x,
$this->y,
$this->z + 1 - $f,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(0, 0, 1 - $f, 1, 1, 1);
}else{
$bb->setBounds(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + $f
);
$bb->setBounds(0, 0, 0, 1, 1, $f);
}
}else{
$bb->setBounds(
$this->x + 1 - $f,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(1 - $f, 0, 0, 1, 1, 1);
}
}elseif($j === 3){
if($isOpen){
if(!$isRight){
$bb->setBounds(
$this->x,
$this->y,
$this->z,
$this->x + $f,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(0, 0, 0, $f, 1, 1);
}else{
$bb->setBounds(
$this->x + 1 - $f,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(1 - $f, 0, 0, 1, 1, 1);
}
}else{
$bb->setBounds(
$this->x,
$this->y,
$this->z + 1 - $f,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(0, 0, 1 - $f, 1, 1, 1);
}
}

View File

@ -57,12 +57,12 @@ class EndPortalFrame extends Solid{
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + (($this->getDamage() & 0x04) > 0 ? 1 : 0.8125),
$this->z + 1
0,
0,
0,
1,
(($this->getDamage() & 0x04) > 0 ? 1 : 0.8125),
1
);
}
}

View File

@ -68,30 +68,30 @@ class EndRod extends Flowable{
switch($m){
case 0x00: //up/down
return new AxisAlignedBB(
$this->x + $width,
$this->y,
$this->z + $width,
$this->x + 1 - $width,
$this->y + 1,
$this->z + 1 - $width
$width,
0,
$width,
1 - $width,
1,
1 - $width
);
case 0x02: //north/south
return new AxisAlignedBB(
$this->x,
$this->y + $width,
$this->z + $width,
$this->x + 1,
$this->y + 1 - $width,
$this->z + 1 - $width
0,
$width,
$width,
1,
1 - $width,
1 - $width
);
case 0x04: //east/west
return new AxisAlignedBB(
$this->x + $width,
$this->y + $width,
$this->z,
$this->x + 1 - $width,
$this->y + 1 - $width,
$this->z + 1
$width,
$width,
0,
1 - $width,
1 - $width,
1
);
}

View File

@ -48,16 +48,8 @@ class Farmland extends Transparent{
return BlockToolType::TYPE_SHOVEL;
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1, //TODO: this should be 0.9375, but MCPE currently treats them as a full block (https://bugs.mojang.com/browse/MCPE-12109)
$this->z + 1
);
return new AxisAlignedBB(0, 0, 0, 1, 1, 1); //TODO: y max should be 0.9375, but MCPE currently treats them as a full block (https://bugs.mojang.com/browse/MCPE-12109)
}
public function onNearbyBlockChange() : void{

View File

@ -40,12 +40,12 @@ abstract class Fence extends Transparent{
$width = 0.5 - $this->getThickness() / 2;
return new AxisAlignedBB(
$this->x + ($this->canConnect($this->getSide(Vector3::SIDE_WEST)) ? 0 : $width),
$this->y,
$this->z + ($this->canConnect($this->getSide(Vector3::SIDE_NORTH)) ? 0 : $width),
$this->x + 1 - ($this->canConnect($this->getSide(Vector3::SIDE_EAST)) ? 0 : $width),
$this->y + 1.5,
$this->z + 1 - ($this->canConnect($this->getSide(Vector3::SIDE_SOUTH)) ? 0 : $width)
($this->canConnect($this->getSide(Vector3::SIDE_WEST)) ? 0 : $width),
0,
($this->canConnect($this->getSide(Vector3::SIDE_NORTH)) ? 0 : $width),
1 - ($this->canConnect($this->getSide(Vector3::SIDE_EAST)) ? 0 : $width),
1.5,
1 - ($this->canConnect($this->getSide(Vector3::SIDE_SOUTH)) ? 0 : $width)
);
}
@ -61,12 +61,12 @@ abstract class Fence extends Transparent{
if($connectWest or $connectEast){
//X axis (west/east)
$bbs[] = new AxisAlignedBB(
$this->x + ($connectWest ? 0 : $inset),
$this->y,
$this->z + $inset,
$this->x + 1 - ($connectEast ? 0 : $inset),
$this->y + 1.5,
$this->z + 1 - $inset
($connectWest ? 0 : $inset),
0,
$inset,
1 - ($connectEast ? 0 : $inset),
1.5,
1 - $inset
);
}
@ -76,12 +76,12 @@ abstract class Fence extends Transparent{
if($connectNorth or $connectSouth){
//Z axis (north/south)
$bbs[] = new AxisAlignedBB(
$this->x + $inset,
$this->y,
$this->z + ($connectNorth ? 0 : $inset),
$this->x + 1 - $inset,
$this->y + 1.5,
$this->z + 1 - ($connectSouth ? 0 : $inset)
$inset,
0,
($connectNorth ? 0 : $inset),
1 - $inset,
1.5,
1 - ($connectSouth ? 0 : $inset)
);
}
@ -89,12 +89,12 @@ abstract class Fence extends Transparent{
//centre post AABB (only needed if not connected on any axis - other BBs overlapping will do this if any connections are made)
return [
new AxisAlignedBB(
$this->x + $inset,
$this->y,
$this->z + $inset,
$this->x + 1 - $inset,
$this->y + 1.5,
$this->z + 1 - $inset
$inset,
0,
$inset,
1 - $inset,
1.5,
1 - $inset
)
];
}

View File

@ -41,7 +41,6 @@ class FenceGate extends Transparent{
protected function recalculateBoundingBox() : ?AxisAlignedBB{
if(($this->getDamage() & 0x04) > 0){
return null;
}
@ -49,21 +48,21 @@ class FenceGate extends Transparent{
$i = ($this->getDamage() & 0x03);
if($i === 2 or $i === 0){
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z + 0.375,
$this->x + 1,
$this->y + 1.5,
$this->z + 0.625
0,
0,
0.375,
1,
1.5,
0.625
);
}else{
return new AxisAlignedBB(
$this->x + 0.375,
$this->y,
$this->z,
$this->x + 0.625,
$this->y + 1.5,
$this->z + 1
0.375,
0,
0,
0.625,
1.5,
1
);
}
}

View File

@ -47,14 +47,8 @@ class FlowerPot extends Flowable{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x + 0.3125,
$this->y,
$this->z + 0.3125,
$this->x + 0.6875,
$this->y + 0.375,
$this->z + 0.6875
);
static $f = 0.3125;
return new AxisAlignedBB($f, 0, $f, 1 - $f, 0.375, 1 - $f);
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{

View File

@ -45,14 +45,7 @@ class GrassPath extends Transparent{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1, //TODO: this should be 0.9375, but MCPE currently treats them as a full block (https://bugs.mojang.com/browse/MCPE-12109)
$this->z + 1
);
return new AxisAlignedBB(0, 0, 0, 1, 1, 1); //TODO: y max should be 0.9375, but MCPE currently treats them as a full block (https://bugs.mojang.com/browse/MCPE-12109)
}
public function getHardness() : float{

View File

@ -79,12 +79,12 @@ class Ladder extends Transparent{
}
return new AxisAlignedBB(
$this->x + $minX,
$this->y,
$this->z + $minZ,
$this->x + $maxX,
$this->y + 1,
$this->z + $maxZ
$minX,
0,
$minZ,
$maxX,
1,
$maxZ
);
}

View File

@ -49,14 +49,8 @@ class Skull extends Flowable{
protected function recalculateBoundingBox() : ?AxisAlignedBB{
//TODO: different bounds depending on attached face (meta)
return new AxisAlignedBB(
$this->x + 0.25,
$this->y,
$this->z + 0.25,
$this->x + 0.75,
$this->y + 0.5,
$this->z + 0.75
);
static $f = 0.25;
return new AxisAlignedBB($f, 0, $f, 1 - $f, 0.5, 1 - $f);
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{

View File

@ -107,23 +107,9 @@ abstract class Slab extends Transparent{
protected function recalculateBoundingBox() : ?AxisAlignedBB{
if(($this->meta & 0x08) > 0){
return new AxisAlignedBB(
$this->x,
$this->y + 0.5,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
return new AxisAlignedBB(0, 0.5, 0, 1, 1, 1);
}else{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 0.5,
$this->z + 1
);
return new AxisAlignedBB(0, 0, 0, 1, 0.5, 1);
}
}
}

View File

@ -46,14 +46,6 @@ class SoulSand extends Solid{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1 - 0.125,
$this->z + 1
);
return new AxisAlignedBB(0, 0, 0, 1, 1 - 0.125, 1);
}
}

View File

@ -37,14 +37,7 @@ abstract class Stair extends Transparent{
$maxYSlab = $minYSlab + 0.5;
$bbs = [
new AxisAlignedBB(
$this->x,
$this->y + $minYSlab,
$this->z,
$this->x + 1,
$this->y + $maxYSlab,
$this->z + 1
)
new AxisAlignedBB(0, $minYSlab, 0, 1, $maxYSlab, 1)
];
$minY = ($this->meta & 0x04) === 0 ? 0.5 : 0;
@ -70,14 +63,7 @@ abstract class Stair extends Transparent{
break;
}
$bbs[] = new AxisAlignedBB(
$this->x + $minX,
$this->y + $minY,
$this->z + $minZ,
$this->x + $maxX,
$this->y + $maxY,
$this->z + $maxZ
);
$bbs[] = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
return $bbs;
}

View File

@ -32,12 +32,12 @@ abstract class Thin extends Transparent{
$width = 0.5 - 0.125 / 2;
return new AxisAlignedBB(
$this->x + ($this->canConnect($this->getSide(Vector3::SIDE_WEST)) ? 0 : $width),
$this->y,
$this->z + ($this->canConnect($this->getSide(Vector3::SIDE_NORTH)) ? 0 : $width),
$this->x + 1 - ($this->canConnect($this->getSide(Vector3::SIDE_EAST)) ? 0 : $width),
$this->y + 1,
$this->z + 1 - ($this->canConnect($this->getSide(Vector3::SIDE_SOUTH)) ? 0 : $width)
($this->canConnect($this->getSide(Vector3::SIDE_WEST)) ? 0 : $width),
0,
($this->canConnect($this->getSide(Vector3::SIDE_NORTH)) ? 0 : $width),
1 - ($this->canConnect($this->getSide(Vector3::SIDE_EAST)) ? 0 : $width),
1,
1 - ($this->canConnect($this->getSide(Vector3::SIDE_SOUTH)) ? 0 : $width)
);
}
@ -53,12 +53,12 @@ abstract class Thin extends Transparent{
if($connectWest or $connectEast){
//X axis (west/east)
$bbs[] = new AxisAlignedBB(
$this->x + ($connectWest ? 0 : $inset),
$this->y,
$this->z + $inset,
$this->x + 1 - ($connectEast ? 0 : $inset),
$this->y + 1,
$this->z + 1 - $inset
($connectWest ? 0 : $inset),
0,
$inset,
1 - ($connectEast ? 0 : $inset),
1,
1 - $inset
);
}
@ -68,12 +68,12 @@ abstract class Thin extends Transparent{
if($connectNorth or $connectSouth){
//Z axis (north/south)
$bbs[] = new AxisAlignedBB(
$this->x + $inset,
$this->y,
$this->z + ($connectNorth ? 0 : $inset),
$this->x + 1 - $inset,
$this->y + 1,
$this->z + 1 - ($connectSouth ? 0 : $inset)
$inset,
0,
($connectNorth ? 0 : $inset),
1 - $inset,
1,
1 - ($connectSouth ? 0 : $inset)
);
}
@ -81,12 +81,12 @@ abstract class Thin extends Transparent{
//centre post AABB (only needed if not connected on any axis - other BBs overlapping will do this if any connections are made)
return [
new AxisAlignedBB(
$this->x + $inset,
$this->y,
$this->z + $inset,
$this->x + 1 - $inset,
$this->y + 1,
$this->z + 1 - $inset
$inset,
0,
$inset,
1 - $inset,
1,
1 - $inset
)
];
}

View File

@ -59,64 +59,22 @@ class Trapdoor extends Transparent{
$f = 0.1875;
if(($damage & self::MASK_UPPER) > 0){
$bb = new AxisAlignedBB(
$this->x,
$this->y + 1 - $f,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb = new AxisAlignedBB(0, 1 - $f, 0, 1, 1, 1);
}else{
$bb = new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + $f,
$this->z + 1
);
$bb = new AxisAlignedBB(0, 0, 0, 1, $f, 1);
}
if(($damage & self::MASK_OPENED) > 0){
if(($damage & 0x03) === self::MASK_SIDE_NORTH){
$bb->setBounds(
$this->x,
$this->y,
$this->z + 1 - $f,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(0, 0, 1 - $f, 1, 1, 1);
}elseif(($damage & 0x03) === self::MASK_SIDE_SOUTH){
$bb->setBounds(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + $f
);
$bb->setBounds(0, 0, 0, 1, 1, $f);
}
if(($damage & 0x03) === self::MASK_SIDE_WEST){
$bb->setBounds(
$this->x + 1 - $f,
$this->y,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(1 - $f, 0, 0, 1, 1, 1);
}
if(($damage & 0x03) === self::MASK_SIDE_EAST){
$bb->setBounds(
$this->x,
$this->y,
$this->z,
$this->x + $f,
$this->y + 1,
$this->z + 1
);
$bb->setBounds(0, 0, 0, $f, 1, 1);
}
}

View File

@ -70,7 +70,6 @@ class Vine extends Flowable{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
$minX = 1;
$minY = 1;
$minZ = 1;
@ -121,14 +120,7 @@ class Vine extends Flowable{
$maxZ = 1;
}
return new AxisAlignedBB(
$this->x + $minX,
$this->y + $minY,
$this->z + $minZ,
$this->x + $maxX,
$this->y + $maxY,
$this->z + $maxZ
);
return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{

View File

@ -45,17 +45,10 @@ class WaterLily extends Flowable{
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return new AxisAlignedBB(
$this->x + 0.0625,
$this->y,
$this->z + 0.0625,
$this->x + 0.9375,
$this->y + 0.015625,
$this->z + 0.9375
);
static $f = 0.0625;
return new AxisAlignedBB($f, 0, $f, 1 - $f, 0.015625, 1 - $f);
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
if($blockClicked instanceof Water){
$up = $blockClicked->getSide(Vector3::SIDE_UP);