hopefully fixed yaw/pitch rotation + new ai task can be randomly executed now

This commit is contained in:
GameHerobrine 2022-12-20 19:23:21 +03:00
parent 1b85ffd4c6
commit d9c096357a
7 changed files with 69 additions and 44 deletions

View file

@ -15,20 +15,49 @@ class MobController
if($x === $this->entity->x && $y === $this->entity->y && $z === $this->entity->z){
return false; //failed
}
$this->entity->moveEntityWithOffset(($x > 0 ? 1 : ($x < 0 ? -1 : 0)), ($y > 0 ? 1 : ($y < 0 ? -1 : 0)), ($z > 0 ? 1 : ($z < 0 ? -1 : 0)));
$ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
$oy = ($y > 0 ? 1 : ($y < 0 ? -1 : 0));
$oz = ($z > 0 ? 1 : ($z < 0 ? -1 : 0));
$this->entity->moveEntityWithOffset($ox, $oy, $oz);
$this->faceEntity($this->entity->add($ox, $oy, $oz));
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $this->entity->eid;
$pk->yaw = $this->entity->yaw;
$this->entity->server->api->player->broadcastPacket($this->entity->level->players, $pk);
return true;
}
public function lookAt($x, $y = 0, $z = 0){
if($x instanceof Entity){
return $this->lookAt($x->x, $x->y, $x->z);
public function faceEntity(Vector3 $v){
$d = $v->subtract($this->entity)->normalize();
$dx = $d->x;
$dz = $d->z;
$tan = $dz == 0 ? 0 : (90 - rad2deg($dx / $dz));
$thetaOffset = $dz < 0 ? 90 : 270;
$this->entity->yaw = $this->entity->headYaw = ($thetaOffset + $tan);
}
public function lookOffset($x, $y, $z, $pitch = true){
$tan = $z == 0 ? 0 : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/
$thetaOffset = $z < 0 ? 90 : 270;
$calcYaw = $tan + $thetaOffset;
$this->entity->yaw = $calcYaw;
if($pitch){
$diff = sqrt($x * $x + $z * $z);
$calcPitch = rad2deg(atan($y / $diff));
$this->entity->pitch = $calcPitch;
}
$this->entity->yaw = -atan2($x, $z) * 180 / M_PI;
$this->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2))));
$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
return true;
}
public function lookOn($x, $y = 0, $z = 0, $pitch = true){
if($x instanceof Vector3){
return $this->lookOn($x->x, $x->y + ($x->getEyeHeight() ?: 0), $x->z);
}
return $this->lookOffset($x - $this->entity->x, $this->entity->y + $this->entity->height - $y, $z - $this->entity->z, $pitch);
}
public function __destruct(){
unset($this->entity);