mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-23 21:19:44 +00:00
a slightly better pathnavigator
This commit is contained in:
parent
6ee84717f8
commit
916a06c161
3 changed files with 60 additions and 27 deletions
|
|
@ -19,6 +19,17 @@ abstract class Creature extends Living{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function update(){
|
||||||
|
i/*f($this->path === false || count($this->path) <= 0){
|
||||||
|
$this->path = $this->pathFinder->findPath(MinecraftNode::fromArray([$this->x, $this->y, $this->z]), new MinecraftNode($this->x + mt_rand(-7, 7), $this->y, $this->z + mt_rand(-7, 7)));
|
||||||
|
}
|
||||||
|
$state = $this->pathNavigator->followPath($this->path);
|
||||||
|
if($state === PathNavigator::PATH_COMPLETED){
|
||||||
|
$this->path = false;
|
||||||
|
}*/
|
||||||
|
parent::update();
|
||||||
|
}
|
||||||
|
|
||||||
public function createSaveData(){
|
public function createSaveData(){
|
||||||
$data = parent::createSaveData();
|
$data = parent::createSaveData();
|
||||||
$data["State"] = @$this->getState();
|
$data["State"] = @$this->getState();
|
||||||
|
|
|
||||||
|
|
@ -1,46 +1,64 @@
|
||||||
<?php
|
<?php
|
||||||
class PathNavigator
|
class PathNavigator
|
||||||
{
|
{
|
||||||
|
const PATH_COMPLETED = 0x1;
|
||||||
|
const PATH_CONTINUE = 0x2;
|
||||||
|
const PATH_CANT_CONTINUE = 0x3;
|
||||||
public $entity;
|
public $entity;
|
||||||
|
/**
|
||||||
|
* @var Node
|
||||||
|
*/
|
||||||
|
private $getTo;
|
||||||
public $currentPointIndex;
|
public $currentPointIndex;
|
||||||
public function __construct(Creature &$entity){
|
public function __construct(Creature &$entity){
|
||||||
$this->entity = $entity;
|
$this->entity = $entity;
|
||||||
$this->currentPointIndex = 0;
|
$this->currentPointIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function reset(){
|
public function reset(){
|
||||||
$this->currentPointIndex = 0;
|
$this->currentPointIndex = 0;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @param Node[] $path
|
||||||
|
*/
|
||||||
public function followPath($path){ //TODO remove local var
|
public function followPath($path){ //TODO remove local var
|
||||||
if($this->currentPointIndex >= count($path)){
|
if(!$this->canContinue()){
|
||||||
$this->entity->moveTime = mt_rand(100, 200);
|
console($this->getTo);
|
||||||
try{
|
console($this->entity);
|
||||||
return true; //path finished
|
$diffX = $this->getTo->x - $this->entity->x;
|
||||||
}finally {
|
$diffY = 0; //todo ?
|
||||||
$this->reset();
|
$diffZ = $this->getTo->z - $this->entity->z;
|
||||||
}
|
|
||||||
|
$this->entity->moveEntityWithOffset(Utils::getSign($diffX), Utils::getSign($diffY), Utils::getSign($diffZ));
|
||||||
|
return self::PATH_CANT_CONTINUE;
|
||||||
}
|
}
|
||||||
$point = $path[$this->currentPointIndex];
|
$index = $this->currentPointIndex++;
|
||||||
$eX = ceil($this->entity->x);
|
if($index >= count($path)){
|
||||||
$eY = ceil($this->entity->y);
|
$this->onPathEnd();
|
||||||
$eZ = ceil($this->entity->z);
|
console("COMPLETED");
|
||||||
//console($point.":::".$eX.":".$eY.":".$eZ."::::".$this->entity->moveTime);
|
return self::PATH_COMPLETED;
|
||||||
if($this->entity->moveTime <= 0 && $point->x === $eX && $point->y === $eY && $point->z === $eZ){
|
|
||||||
++$this->currentPointIndex;
|
|
||||||
}elseif($this->entity->moveTime <= 0 && !$this->entity->isMoving()){
|
|
||||||
//console($point.":::".$eX.":".$eY.":".$eZ);
|
|
||||||
$vX = $point->x - $eX;
|
|
||||||
$vY = 0;//$point->y - $this->entity->y; //vY is always 0 in current pathfinder\
|
|
||||||
$vZ = $point->z - $eZ;
|
|
||||||
//console($vX."::".$vZ);
|
|
||||||
$this->entity->addVelocity(($vX > 0 ? 0.1 : ($vX < 0 ? -0.1 : 0)), $vY, ($vZ > 0 ? 0.1 : ($vZ < 0 ? -0.1 : 0)));
|
|
||||||
$this->testLook($point);
|
|
||||||
//console($this->entity->yaw);
|
|
||||||
$this->entity->moveTime = 5; //use 20 for debug
|
|
||||||
}
|
}
|
||||||
//$this->entity->moveTime = 5*
|
$node = $path[$index];
|
||||||
return false;
|
|
||||||
|
$this->getTo = $node;
|
||||||
|
|
||||||
|
$diffX = $node->x - $this->entity->x;
|
||||||
|
$diffY = 0; //todo ?
|
||||||
|
$diffZ = $node->z - $this->entity->z;
|
||||||
|
|
||||||
|
$this->entity->moveEntityWithOffset(Utils::getSign($diffX), Utils::getSign($diffY), Utils::getSign($diffZ));
|
||||||
|
|
||||||
|
return self::PATH_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canContinue(){
|
||||||
|
return empty($this->getTo) || (floor($this->entity->x) == floor($this->getTo->x) && floor($this->entity->z) == floor($this->getTo->z));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onPathEnd(){
|
||||||
|
$this->currentPointIndex = 0;
|
||||||
|
unset($this->getTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLook($pos){
|
public function testLook($pos){
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,10 @@ class MinecraftNode extends Node
|
||||||
parent::__construct($x, $y, $z);
|
parent::__construct($x, $y, $z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function fromArray($arr){
|
||||||
|
return new MinecraftNode($arr[0], $arr[1], $arr[2]);
|
||||||
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
{
|
{
|
||||||
return "MinecraftNode({$this->x}, {$this->y}, {$this->z})";
|
return "MinecraftNode({$this->x}, {$this->y}, {$this->z})";
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue