public function onUpdate($tick) { parent::onUpdate($tick); if ($this->age > 20) { $this->kill(); $this->close(); } return true; }
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; }
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; }
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; }