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

@ -1220,12 +1220,11 @@ abstract class Entity{
}
/**
* @return Block[]
* Yields all the blocks whose full-cube areas are intersected by the entity's AABB.
*
* @phpstan-return \Generator<int, Block, void, void>
*/
protected function getBlocksAroundWithEntityInsideActions() : array{
if($this->blocksAround === null){
$inset = 0.001; //Offset against floating-point errors
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);
@ -1233,21 +1232,31 @@ abstract class Entity{
$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);
yield $world->getBlockAt($x, $y, $z);
}
}
}
}
/**
* @return Block[]
*/
protected function getBlocksAroundWithEntityInsideActions() : array{
if($this->blocksAround === null){
$this->blocksAround = [];
$inset = 0.001; //Offset against floating-point errors
foreach($this->getBlocksIntersected($inset) as $block){
if($block->hasEntityCollision()){
$this->blocksAround[] = $block;
}
}
}
}
}
return $this->blocksAround;
}