small changes to entity ai(again =/)

This commit is contained in:
GameHerobrine 2022-11-11 20:13:08 +03:00
parent c29d8dab9d
commit 978447cdbb
9 changed files with 44 additions and 41 deletions

View file

@ -21,23 +21,11 @@ class TaskRandomWalk extends TaskBase
$this->z = mt_rand(-7, 7);
$this->newPos = $ai->entity->add($this->x, 0, $this->z);
$ai->entity->moveTime = floor(5 * Utils::distance($ai->entity, $this->newPos));
$this->looking = true;
}
public function onUpdate(EntityAI $ai)
{
if($this->delay > 0 && !$this->looking){
--$this->delay;
try{
return;
}finally{
$this->testLook($ai, ($this->x > 0 ? 0.1 : ($this->x < 0 ? -0.1 : 0)), ($this->z > 0 ? 0.1 : ($this->z < 0 ? -0.1 : 0)));
$pk = new RotateHeadPacket();
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->looking = true;
}
}
if($ai->entity->moveTime <= 1){
$ai->entity->idleTime = mt_rand(10,40);
$this->x = 0;
@ -45,16 +33,21 @@ class TaskRandomWalk extends TaskBase
$this->looking = false;
}
$ai->entity->moveEntityWithOffset(($this->x > 0 ? 1 : ($this->x < 0 ? -1 : 0)), 0, ($this->z > 0 ? 1 : ($this->z < 0 ? -1 : 0)));
if($this->looking){
$this->testLook($ai, $ai->entity->speedX, $ai->entity->speedZ);
$pk = new RotateHeadPacket();
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->looking = false;
}
}
public function updateLook($ai){
$ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";");
}
public function testLook($ai, $vX, $vZ){
$x = $this->newPos->x - $ai->entity->x;
$y = $this->newPos->y - $ai->entity->y;
$z = $this->newPos->z - $ai->entity->z;
$ai->entity->yaw = -atan2($x, $z) * 180 / M_PI;
$ai->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2))));
$ai->entity->yaw = -atan2($vX, $vZ) * 180 / M_PI;
//nopitch $ai->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2))));
$ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";");
}
}