Added Living->getTargetBlock(), Living->getLineOfSight(), Vector3 side constants, Vector3::getOppositeSide()

This commit is contained in:
Shoghi Cervantes
2014-10-09 17:57:25 +02:00
parent 6424934df6
commit 5c4e7b6ee0
3 changed files with 384 additions and 6 deletions

View File

@ -22,6 +22,7 @@
namespace pocketmine\entity;
use pocketmine\block\Block;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityDeathEvent;
@ -32,6 +33,7 @@ use pocketmine\math\Vector3;
use pocketmine\nbt\tag\Short;
use pocketmine\network\protocol\EntityEventPacket;
use pocketmine\Server;
use pocketmine\utils\BlockIterator;
abstract class Living extends Entity implements Damageable{
@ -168,4 +170,63 @@ abstract class Living extends Entity implements Damageable{
public function getDrops(){
return [];
}
/**
* @param int $maxDistance
* @param int $maxLength
* @param array $transparent
*
* @return Block[]
*/
public function getLineOfSight($maxDistance, $maxLength = 0, array $transparent = []){
if($maxDistance > 120){
$maxDistance = 120;
}
if(count($transparent) === 0){
$transparent = null;
}
$blocks = [];
$itr = new BlockIterator($this->level, $this->getPosition(), $this->getEyeHeight(), $maxDistance);
while($itr->valid()){
$itr->next();
$block = $itr->current();
$blocks[] = $block;
if($maxLength !== 0 and count($blocks) > $maxLength){
array_shift($blocks);
}
$id = $block->getID();
if($transparent === null){
if($id !== 0){
break;
}
}else{
if(!isset($transparent[$id])){
break;
}
}
}
return $blocks;
}
/**
* @param int $maxDistance
* @param array $transparent
*
* @return Block
*/
public function getTargetBlock($maxDistance, array $transparent = []){
$block = array_shift($this->getLineOfSight($maxDistance, 1, $transparent));
if($block instanceof Block){
return $block;
}
return null;
}
}