本文整理汇总了PHP中pocketmine\entity\Animal::onUpdate方法的典型用法代码示例。如果您正苦于以下问题:PHP Animal::onUpdate方法的具体用法?PHP Animal::onUpdate怎么用?PHP Animal::onUpdate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pocketmine\entity\Animal
的用法示例。
在下文中一共展示了Animal::onUpdate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onUpdate
public function onUpdate($tick)
{
parent::onUpdate($tick);
if ($this->age > 20) {
$this->kill();
$this->close();
}
return true;
}
示例2: onUpdate
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->isInsideOfAir();
if(!$inWater){
//$this->motionY -= $this->gravity;
$this->swimDirection = null;
}else*/
if ($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);
}
$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);
}
$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->updateMovement();
}
$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;
}
示例3: onUpdate
public function onUpdate($currentTick)
{
if ($this->knockback) {
if (time() < $this->knockback) {
return parent::onUpdate($currentTick);
}
$this->knockback = 0;
}
$hasUpdate = false;
$this->timings->startTiming();
// Handle flying objects...
$tickDiff = max(1, $currentTick - $this->lastUpdate);
$bb = clone $this->getBoundingBox();
$onGround = count($this->level->getCollisionBlocks($bb->offset(0, -$this->gravity, 0))) > 0;
if (!$onGround) {
// falling or jumping...
$this->motionY -= $this->gravity;
$this->x += $this->motionX * $tickDiff;
$this->y += $this->motionY * $tickDiff;
$this->z += $this->motionZ * $tickDiff;
//echo ("Falling...\n");
} else {
$this->motionX = 0;
// No longer jumping/falling
$this->motionY = 0;
$this->motionZ = 0;
if ($this->y != floor($this->y)) {
$this->y = floor($this->y);
}
// Try to attack a player
list($target, $dist) = $this->findTarget();
if ($target !== null && $dist > 0 && $dist > self::$mindist) {
$dir = $target->subtract($this);
$dir = $dir->divide($dist);
$this->yaw = rad2deg(atan2(-$dir->getX(), $dir->getZ()));
$this->pitch = rad2deg(atan(-$dir->getY()));
$x = $dir->getX() * self::$speed;
$y = 0;
$z = $dir->getZ() * self::$speed;
} else {
$x = (mt_rand() / mt_getrandmax() * 2 - 1) * self::$speed;
$y = 0;
$z = (mt_rand() / mt_getrandmax() * 2 - 1) * self::$speed;
$this->yaw = rad2deg(atan2(-$x, $z));
}
$isJump = count($this->level->getCollisionBlocks($bb->offset($x, 1.2, $z))) <= 0;
if (count($this->level->getCollisionBlocks($bb->offset(0, 0.1, $z))) > 0) {
if ($isJump) {
$y = self::$jump;
$this->motionZ = $z;
}
$z = 0;
}
if (count($this->level->getCollisionBlocks($bb->offset($x, 0.1, 0))) > 0) {
if ($isJump) {
$y = self::$jump;
$this->motionX = $x;
}
$x = 0;
}
//if ($y) echo "Jumping\n";
$ev = new \pocketmine\event\entity\EntityMotionEvent($this, new \pocketmine\math\Vector3($x, $y, $z));
$this->server->getPluginManager()->callEvent($ev);
if ($ev->isCancelled()) {
return false;
}
$this->x += $x;
$this->y += $y;
$this->z += $z;
}
$bb = clone $this->getBoundingBox();
$onGround = count($this->level->getCollisionBlocks($bb->offset(0, -$this->gravity, 0))) > 0;
$this->onGround = $onGround;
$this->timings->stopTiming();
$hasUpdate = parent::onUpdate($currentTick) || $hasUpdate;
return $hasUpdate;
}
示例4: onUpdate
public function onUpdate($currentTick)
{
$hasUpdate = false;
$this->timings->startTiming();
// Handle flying objects...
$tickDiff = max(1, $currentTick - $this->lastUpdate);
$bb = clone $this->getBoundingBox();
$onGround = count($this->level->getCollisionBlocks($bb->offset(0, -$this->gravity, 0))) > 0;
if (!$onGround) {
// falling or jumping...
$this->motionY -= $this->gravity;
$this->x += $this->motionX * $tickDiff;
$this->y += $this->motionY * $tickDiff;
$this->z += $this->motionZ * $tickDiff;
//echo ("Falling...\n");
} else {
$this->motionX = 0;
// No longer jumping/falling
$this->motionY = 0;
$this->motionZ = 0;
if ($this->y != floor($this->y)) {
$this->y = floor($this->y);
}
// Try to attack a player
$target = null;
if ($this->petOwner->player) {
$target = $this->petOwner->player;
if ($target) {
if ($target->getLevel() != $this->level) {
// Pet is in a different level...
$target = null;
} else {
$dist = $this->distance($target);
}
}
}
// if no target despawn
if ($target === null) {
$this->close();
return;
}
if ($target !== null && $dist != 0) {
$dir = $target->getLevel()->getSafeSpawn($target)->subtract($this);
$dir = $dir->divide($dist);
$this->yaw = rad2deg(atan2(-$dir->getX(), $dir->getZ()));
$this->pitch = rad2deg(atan(-$dir->getY()));
// if dist to owner < move dist or pet is being told to stay just look at owner
if ($dist <= self::$dist || $this->isSitting) {
//$this->yaw = rad2deg(atan2(-$dir->getX(),$dir->getZ()));
//$this->pitch = rad2deg(atan(-$dir->getY()));
$this->updateMovement();
$this->level->addEntityMovement($this->chunk->getX(), $this->chunk->getZ(), $this->id, $this->x, $this->y, $this->z, $this->yaw, $this->pitch, $this->yaw);
}
// otherwise do a full movement
if ($dist > self::$dist && !$this->isSitting) {
$x = $dir->getX() * self::$speed;
$y = 0;
$z = $dir->getZ() * self::$speed;
$isJump = count($this->level->getCollisionBlocks($bb->offset($x, 1.2, $z))) <= 0;
if (count($this->level->getCollisionBlocks($bb->offset(0, 0.1, $z))) > 0) {
if ($isJump) {
$y = self::$jump;
$this->motionZ = $z;
}
$z = 0;
}
if (count($this->level->getCollisionBlocks($bb->offset($x, 0.1, 0))) > 0) {
if ($isJump) {
$y = self::$jump;
$this->motionX = $x;
}
$x = 0;
}
//if ($y) echo "Jumping\n";
$ev = new \pocketmine\event\entity\EntityMotionEvent($this, new \pocketmine\math\Vector3($x, $y, $z));
$this->server->getPluginManager()->callEvent($ev);
if ($ev->isCancelled()) {
return false;
}
$this->x += $x;
$this->y += $y;
$this->z += $z;
}
if ($dist > 40) {
$target->sendMessage($this->petName_unformatted . " could not keep up - use /pet spawn again to bring them back");
$this->close();
}
}
}
$bb = clone $this->getBoundingBox();
$onGround = count($this->level->getCollisionBlocks($bb->offset(0, -$this->gravity, 0))) > 0;
$this->onGround = $onGround;
$this->timings->stopTiming();
$hasUpdate = parent::onUpdate($currentTick) || $hasUpdate;
return $hasUpdate;
}