public function onAttack(BattleEvent $event) { if ($event->getLocHelper()->objectExists($event->getToObjectId())) { $event->getLocHelper()->addJournal($event->getSelfObjectId() . "->" . $event->getToObjectId(), Game::init()->getService('player.helper')->getCollection()); $event->getLocHelper()->update(); } }
/** * * @return mixed */ public function ai() { $npc = $this->getNpcHelper()->getNpc(); $this->_addListeners($npc); $state = isset($npc["state"]) ? $npc["state"] : self::STATE_STAND; //обработка в зависимости от текущего состояния switch ($state) { //обычное состояние case self::STATE_AGRESSY: $target = $this->getNpcHelper()->getTarget(); //если агрессивный и нет таргета, то находим самую слабую цель if (false === $target) { $this->getLocHelper()->eachObjects("loc", function ($objId, $obj) { if ($objId != $this->getNpcHelper()->getNpcId()) { $players = []; if (substr($objId, 0, 7) == "player_") { //считаем сумму параметров $calcService = new CalculateHelper($this->getNpcHelper()->getNpc()); $statsSumm = $calcService->getStatSumm(); $playerHelper = Game::init()->getService('player.helper')->factory($objId); $playerCalculateService = new CalculateHelper($playerHelper->getPlayer()); $playerStatsSumm = $playerCalculateService->getStatSumm(); $pr = $statsSumm / $playerStatsSumm * 100; //$players[$objId] = ; if ($pr > 5) { //если статы не меньше 1/2 то нападаем $this->getNpcHelper()->setTarget($objId); $battleEvent = new BattleEvent(); $battleEvent->setLocHelper($this->getLocHelper()); $battleEvent->setAttackType(BattleEvent::AUTO_ATTAK); $battleEvent->setToObjectId($objId); $battleEvent->setSelfObjectId($this->getNpcHelper()->getNpcId()); Game::init()->getDispatcher()->dispatch("battle.attack", $battleEvent); } } } }); } if ($this->getLocHelper()->objectExists($target)) { //атакуем } else { $this->supervision->eachLocations(function ($locHelper) use($target) { if ($locHelper->objectExists($target)) { //нашли объект, идем $this->locHelper->moveLocObject($this->getNpcHelper()->getNpcId(), $this->getNpcHelper()->getNpcId()); } }); } break; //обычное состояние //обычное состояние default: break; } }