public function onUpdate($currentTick)
{
if ($this->closed !== false) {
return false;
}
if (++$this->switchDirectionTicker === 100) {
$this->switchDirectionTicker = 0;
if (mt_rand(0, 100) < 50) {
$this->swimDirection = null;
}
}
$this->lastUpdate = $currentTick;
$this->timings->startTiming();
$hasUpdate = parent::onUpdate($currentTick);
if ($this->isAlive()) {
if ($this->y > 62 and $this->swimDirection !== null) {
$this->swimDirection->y = -0.5;
}
$inWater = $this->isInsideOfWater();
if (!$inWater) {
$this->motionY -= $this->gravity;
$this->swimDirection = null;
} elseif ($this->swimDirection !== null) {
if ($this->motionX ** 2 + $this->motionY ** 2 + $this->motionZ ** 2 <= $this->swimDirection->lengthSquared()) {
$this->motionX = $this->swimDirection->x * $this->swimSpeed;
$this->motionY = $this->swimDirection->y * $this->swimSpeed;
$this->motionZ = $this->swimDirection->z * $this->swimSpeed;
}
} else {
$this->swimDirection = $this->generateRandomDirection();
$this->swimSpeed = mt_rand(50, 100) / 2000;
}
$expectedPos = new Vector3($this->x + $this->motionX, $this->y + $this->motionY, $this->z + $this->motionZ);
$this->move($this->motionX, $this->motionY, $this->motionZ);
if ($expectedPos->distanceSquared($this) > 0) {
$this->swimDirection = $this->generateRandomDirection();
$this->swimSpeed = mt_rand(50, 100) / 2000;
}
$friction = 1 - $this->drag;
$this->motionX *= $friction;
$this->motionY *= 1 - $this->drag;
$this->motionZ *= $friction;
$f = sqrt($this->motionX ** 2 + $this->motionZ ** 2);
$this->yaw = -atan2($this->motionX, $this->motionZ) * 180 / M_PI;
$this->pitch = -atan2($f, $this->motionY) * 180 / M_PI;
if ($this->onGround) {
$this->motionY *= -0.5;
}
}
$this->timings->stopTiming();
return $hasUpdate or !$this->onGround or abs($this->motionX) > 1.0E-5 or abs($this->motionY) > 1.0E-5 or abs($this->motionZ) > 1.0E-5;
}