fixing glass & bars collision detection

so much duplicated code, it would be nice if Fence could inherit from Thin, but that causes too many issues with block connections.
This commit is contained in:
Dylan K. Taylor 2017-10-13 10:54:31 +01:00
parent 423bea4b57
commit c5c74c1898

View File

@ -29,52 +29,82 @@ use pocketmine\math\Vector3;
abstract class Thin extends Transparent{
protected function recalculateBoundingBox() : ?AxisAlignedBB{
$minX = 0.4375;
$maxX = 0.5625;
$minZ = 0.4375;
$maxZ = 0.5625;
$canConnectNorth = $this->canConnect($this->getSide(Vector3::SIDE_NORTH));
$canConnectSouth = $this->canConnect($this->getSide(Vector3::SIDE_SOUTH));
$canConnectWest = $this->canConnect($this->getSide(Vector3::SIDE_WEST));
$canConnectEast = $this->canConnect($this->getSide(Vector3::SIDE_EAST));
if((!$canConnectWest or !$canConnectEast) and ($canConnectWest or $canConnectEast or $canConnectNorth or $canConnectSouth)){
if($canConnectWest and !$canConnectEast){
$minX = 0;
}elseif(!$canConnectWest and $canConnectEast){
$maxX = 1;
}
}else{
$minX = 0;
$maxX = 1;
}
if((!$canConnectNorth or !$canConnectSouth) and ($canConnectWest or $canConnectEast or $canConnectNorth or $canConnectSouth)){
if($canConnectNorth and !$canConnectSouth){
$minZ = 0;
}elseif(!$canConnectNorth and $canConnectSouth){
$maxZ = 1;
}
}else{
$minZ = 0;
$maxZ = 1;
}
$width = 0.5 - 0.125 / 2;
return new AxisAlignedBB(
$this->x + $minX,
$this->x + ($this->canConnect($this->getSide(Vector3::SIDE_WEST)) ? 0 : $width),
$this->y,
$this->z + $minZ,
$this->x + $maxX,
$this->y + 1,
$this->z + $maxZ
$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)
);
}
protected function recalculateCollisionBoxes() : array{
$inset = 0.5 - 0.125 / 2;
public function canConnect(Block $block){
return $block->isSolid() or $block->getId() === $this->getId() or $block->getId() === self::GLASS_PANE or $block->getId() === self::GLASS;
/** @var AxisAlignedBB[] $bbs */
$bbs = [];
$connectWest = $this->canConnect($this->getSide(Vector3::SIDE_WEST));
$connectEast = $this->canConnect($this->getSide(Vector3::SIDE_EAST));
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
);
}
$connectNorth = $this->canConnect($this->getSide(Vector3::SIDE_NORTH));
$connectSouth = $this->canConnect($this->getSide(Vector3::SIDE_SOUTH));
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)
);
}
if(empty($bbs)){
//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
)
];
}
return $bbs;
}
public function canConnect(Block $block) : bool{
if($block instanceof Thin){
return true;
}
//FIXME: currently there's no proper way to tell if a block is a full-block, so we check the bounding box size
$bbs = $block->getCollisionBoxes();
if(count($bbs) === 1){
return $bbs[0]->getAverageEdgeLength() >= 1;
}
return false;
}
}