Example #1
0
 public function onUpdate($currentTick)
 {
     if ($this->knockback) {
         if (time() < $this->knockback) {
             return parent::onUpdate($currentTick);
         }
         $this->knockback = 0;
     }
     // check has target and target still valid else despawn/close
     if (is_null($this->targetPlayer)) {
         Server::getInstance()->getLogger()->info(Main::PREFIX . "Herobrine entity has no target - despawning");
         $this->poofAway();
         return true;
     }
     if (!$this->targetPlayer->isConnected()) {
         $this->poofAway();
         return true;
     }
     if ($this->targetPlayer->getLevel()->getName() != $this->getLevel()->getName()) {
         $this->poofAway();
         return true;
     }
     // get target distance for comparison
     $targetdist = $this->distance($this->targetPlayer);
     // despawn if any player <5 block distance OR change target if another player is closer
     foreach ($this->getLevel()->getPlayers() as $player) {
         $playerdist = $this->distance($player);
         if ($playerdist < 15) {
             Server::getInstance()->getLogger()->info(Main::PREFIX . "Herobrine " . $player->getName() . " got close - despawning");
             // $newundeadplayer = new UndeadPlayer(new dummyChunk, new Compound, $player, $this, $this->plugin);
             $this->poofAway();
             return true;
         }
         if ($playerdist < $targetdist) {
             if ($this->lastNearestPlayer != $player->getName()) {
                 $this->lastNearestPlayer = $player->getName();
                 Server::getInstance()->getLogger()->info(Main::PREFIX . "Herobrine turn to look @" . $this->lastNearestPlayer . " who is closer than " . $this->targetPlayer->getName());
             }
             $this->targetPlayer = $player;
             $targetdist = $playerdist;
         }
     }
     // despawn if target too far away
     if ($targetdist > 30) {
         Server::getInstance()->getLogger()->info(Main::PREFIX . "Herobrine target too far away - despawning");
         $this->poofAway();
         return true;
     }
     // turn to look at target
     $target = $this->targetPlayer;
     if ($targetdist == 0) {
         $targetdist = 0.1;
     }
     $this->targetDist = $targetdist;
     $dir = $target->subtract($this);
     $dir = $dir->divide($targetdist);
     $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->getEyeHeight(), $this->z, $this->yaw, $this->pitch, $this->yaw);
     // bored timer
     if (time() - $this->timespawned > 120) {
         Server::getInstance()->getLogger()->info(Main::PREFIX . "Herobrine got bored (120s no activity)");
         $this->poofAway();
         return true;
     }
     return true;
 }
Example #2
0
 public function onUpdate($currentTick)
 {
     if ($this->knockback) {
         if (time() < $this->knockback) {
             return parent::onUpdate($currentTick);
         }
         $this->knockback = 0;
     }
     // check has target and target still valid else despawn/close
     if (is_null($this->targetPlayer)) {
         $this->poofAway();
         return true;
     }
     if (!$this->targetPlayer->isConnected()) {
         Server::getInstance()->getLogger()->info(Main::PREFIX . $this->thisname . "'s target disconnected");
         $this->poofAway();
         return true;
     }
     if ($this->targetPlayer->getLevel()->getName() != $this->getLevel()->getName()) {
         Server::getInstance()->getLogger()->info(Main::PREFIX . $this->thisname . "'s target left world");
         $this->poofAway();
         return true;
     }
     // get target distance for comparison
     $targetdist = $this->distance($this->targetPlayer);
     // change target if another player is closer
     foreach ($this->getLevel()->getPlayers() as $player) {
         $playerdist = $this->distance($player);
         if ($playerdist < $targetdist) {
             if ($this->lastNearestPlayer != $player->getName()) {
                 $this->lastNearestPlayer = $player->getName();
             }
             Server::getInstance()->getLogger()->info(Main::PREFIX . $this->thisname . "'s changed target from " . $this->targetPlayer->getName() . " to " . $player->getName());
             $this->targetPlayer = $player;
             $targetdist = $playerdist;
         }
     }
     // despawn if target too far away
     if ($targetdist > 30) {
         Server::getInstance()->getLogger()->info(Main::PREFIX . $this->thisname . "'s target got too far away");
         $this->poofAway();
         return true;
     }
     // turn to look at target
     $target = $this->targetPlayer;
     if ($targetdist == 0) {
         $targetdist = 0.1;
     }
     $this->targetDist = $targetdist;
     $dir = $target->subtract($this);
     $dir = $dir->divide($targetdist);
     $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->getEyeHeight(), $this->z, $this->yaw, $this->pitch, $this->yaw);
     // move towards target if distance > attack distance
     $bb = clone $this->getBoundingBox();
     $tickDiff = max(1, $currentTick - $this->lastUpdate);
     $onGround = true;
     if ($onGround && $targetdist > self::$attack) {
         $x = $dir->getX() * self::$speed;
         //$y = $dir->getY() * self::$speed;
         $y = $this->targetPlayer->getY() - $this->getY();
         $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;
         }
         */
         // $motion = new Vector3($x, $y, $z);
         $this->move($x, $y, $z);
     }
     // atttack target if in range
     if ($onGround && $targetdist <= self::$attack) {
         /*
         Server::getInstance()->getLogger()->info(Main::PREFIX  . "undead attacking player");
         $attack_power = mt_rand(0,5);
         if ($attack_power > 2) {
         	$source = new EntityDamageByEntityEvent($this,$this->targetPlayer,EntityDamageEvent::CAUSE_ENTITY_ATTACK,$attack_power,0.3);
         	// $this->targetPlayer->attack($source->getFinalDamage(),$source);
         	$this->targetPlayer->attack(0 ,$source);
         }
         */
         $attacker_effect = Effect::getEffect(Effect::CONFUSION);
         $attacker_effect->setDuration(150);
         $this->targetPlayer->addEffect($attacker_effect);
     }
     return true;
     // always return hasupdated as these despawn when inactive anyway
 }