Skip to content

Instantly share code, notes, and snippets.

@yuyaprgrm
Last active November 1, 2021 12:07
Show Gist options
  • Select an option

  • Save yuyaprgrm/f904e7a3b8a2a054791dc5298b9154d6 to your computer and use it in GitHub Desktop.

Select an option

Save yuyaprgrm/f904e7a3b8a2a054791dc5298b9154d6 to your computer and use it in GitHub Desktop.
method for whether there is an entity but a player within n block-length.
<?php
use pocketmine\Player;
/**
* $entity の周囲 $maxDistance ブロック以内にプレイヤー以外のエンティティがいるかを返す
*/
public function isThereEntityNonPlayerWithinCircle(Entity $target, float $maxDistance, bool $includeDead = false): bool {
$minX = ((int)floor($target->x - $maxDistance)) >> 4;
$maxX = ((int)floor($target->x + $maxDistance)) >> 4;
$minZ = ((int)floor($target->z - $maxDistance)) >> 4;
$maxZ = ((int)floor($target->z + $maxDistance)) >> 4;
$currentTargetDistSq = $maxDistance**2;
$level = $target->getLevelNonNull();
for ($x = $minX;$x <= $maxX;++$x) {
for ($z = $minZ;$z <= $maxZ;++$z) {
foreach ($level->getChunkEntities($x, $z) as $entity) {
if ($entity instanceof Player or $entity->isClosed() or $entity->isFlaggedForDespawn() or (!$includeDead and !$entity->isAlive())) {
continue;
}
$distSq = $entity->distanceSquared($target);
if ($distSq < $currentTargetDistSq) {
return true;
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment