Entity: extract getBlocksIntersected() from getBlocksAroundWithEntityInsideActions()

This commit is contained in:
Dylan K. Taylor 2021-12-29 23:04:54 +00:00
parent 207f7ec309
commit 62afa2f28d
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -1219,32 +1219,41 @@ abstract class Entity{
$this->onGround = ($wantedY != $dy and $wantedY < 0);
}
/**
* Yields all the blocks whose full-cube areas are intersected by the entity's AABB.
*
* @phpstan-return \Generator<int, Block, void, void>
*/
protected function getBlocksIntersected(float $inset) : \Generator{
$minX = (int) floor($this->boundingBox->minX + $inset);
$minY = (int) floor($this->boundingBox->minY + $inset);
$minZ = (int) floor($this->boundingBox->minZ + $inset);
$maxX = (int) floor($this->boundingBox->maxX - $inset);
$maxY = (int) floor($this->boundingBox->maxY - $inset);
$maxZ = (int) floor($this->boundingBox->maxZ - $inset);
$world = $this->getWorld();
for($z = $minZ; $z <= $maxZ; ++$z){
for($x = $minX; $x <= $maxX; ++$x){
for($y = $minY; $y <= $maxY; ++$y){
yield $world->getBlockAt($x, $y, $z);
}
}
}
}
/**
* @return Block[]
*/
protected function getBlocksAroundWithEntityInsideActions() : array{
if($this->blocksAround === null){
$inset = 0.001; //Offset against floating-point errors
$minX = (int) floor($this->boundingBox->minX + $inset);
$minY = (int) floor($this->boundingBox->minY + $inset);
$minZ = (int) floor($this->boundingBox->minZ + $inset);
$maxX = (int) floor($this->boundingBox->maxX - $inset);
$maxY = (int) floor($this->boundingBox->maxY - $inset);
$maxZ = (int) floor($this->boundingBox->maxZ - $inset);
$this->blocksAround = [];
$world = $this->getWorld();
for($z = $minZ; $z <= $maxZ; ++$z){
for($x = $minX; $x <= $maxX; ++$x){
for($y = $minY; $y <= $maxY; ++$y){
$block = $world->getBlockAt($x, $y, $z);
if($block->hasEntityCollision()){
$this->blocksAround[] = $block;
}
}
$inset = 0.001; //Offset against floating-point errors
foreach($this->getBlocksIntersected($inset) as $block){
if($block->hasEntityCollision()){
$this->blocksAround[] = $block;
}
}
}