Esempio n. 1
0
 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) {
             $dir = $target->subtract($this);
             $dir = $dir->divide($dist);
             $this->yaw = rad2deg(atan2(-$dir->getX(), $dir->getZ()));
             $this->pitch = rad2deg(atan(-$dir->getY()));
             if ($dist > self::$attack) {
                 //
                 $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;
             } else {
                 $attack = mt_rand(0, 4);
                 if ($attack < 2 && $attack > 0) {
                     $source = new EntityDamageByEntityEvent($this, $target, EntityDamageEvent::CAUSE_ENTITY_ATTACK, $attack);
                     $target->attack($attack, $source);
                 }
             }
         }
     }
     $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;
 }