/** * Method to add a minion to the battlefield * * @param \PHPHearthSim\Model\Entity $entity * @param \PHPHearthSim\Model\Player $player * @param int|null $newPosition The new entity position, if null we place it at the end * * @throws \PHPHearthSim\Exception\Board\InvalidBattlefieldOwnerException when player is not instance of Player * @throws \PHPHearthSim\Exception\Board\InvalidBattlefieldEntityException when entity is not instance of Minion * @return boolean "true" if entity was placed on battlefield, "false" if entity was not placed on battlefield */ public function addToBattlefield(Entity $entity, Player $player, $newPosition = null) { // Make sure $player is a Player if (!$player instanceof Player) { throw new InvalidBattlefieldOwnerException('Owner of entity passed is not an instance of type Player'); } // Make sure entity is a minion, can't play spell on the battlefield! if (!$entity instanceof Minion) { throw new InvalidBattlefieldEntityException('Only entities of type Minion can be placed on the battlefield'); } // Update entity references $entity->setBoard($this); $entity->setOwner($player); // Get minions $minions = $this->getBattlefieldForPlayer($player); // Make sure we have room for another minion if (count($minions) < Board::MAX_BATTLEFIELD_SIZE) { // If not position is provided, we generate a new index if ($newPosition == null) { $newPosition = count($minions); } // Insert minion at position array_splice($minions, $newPosition + 1, 0, [$entity]); // Update battlefield for player $this->battlefield[$player->getid()] = $minions; return true; } return false; }