Esempio n. 1
0
 /// <summary>
 /// Changes the current block to block at the given position.
 /// Calls activate and deactivate methods.
 /// </summary>
 /// <param name="position"></param>
 public void SetCurrentBlock(IntVector2 position)
 {
     currentBlock?.Deactivate();
     currentBlock         = blocks[position.x, position.y];
     currentBlockPosition = position;
     currentBlock?.Activate();
 }
        /// <summary>
        /// Creates copy of board for AI
        /// </summary>
        public IBoardStorage CloneBoardStorage()
        {
            var simulationStorage = new SingleBoardStorage(GetBoardWidth(), GetBoardHeight(), board)
            {
                cells = cells, indexByCell = indexByCell, bonusTable = bonusTable
            };

            for (var col = 1; col <= GetBoardWidth(); col++)
            {
                for (var row = 1; row <= GetBoardHeight(); row++)
                {
                    var item = boardTable[col, row];
                    if (item is ArmyStorageItem storageItem)
                    {
                        simulationStorage.boardTable[col, row] = storageItem.CloneWithoutIcon();
                    }
                    else
                    {
                        simulationStorage.boardTable[col, row] = null;
                    }
                }
            }

            return(simulationStorage);
        }
        /// <summary>
        /// Returns the army that is the result of the action after the chosen army reaches the target cell.
        /// Note, that the target position may be a pass, so this method recursively looks at the cell
        /// outside this pass.
        /// Moreover, the outside cell may also contain a pass, so the searching is repeated.
        /// </summary>
        private ArmyStorageItem GetResultItem(SingleBoardStorage targetBlock, IntVector2 targetPosition)
        {
            //Searching for the first cell on the possible sequence of passes starting at the target position,
            //that is not a pass.
            while (true)
            {
                if (targetBlock.GetItem(targetPosition) is ArmyStorageItem)
                {
                    //The target position contains an army.
                    //Get that army item.
                    var clickedArmyItem = targetBlock.GetItem(targetPosition) as ArmyStorageItem;
                    //Perform an action between our army and an army on the target cell.
                    var resultArmy = clickedArmyItem.Army.PerformAction(chosenArmyItem.Army);

                    if (resultArmy.PlayerType == playerType)
                    {
                        //The result of the action is our army. Delete another army and return ours.
                        clickedArmyItem.StoredObject.SetActive(false);
                        targetBlock.SetItem(targetPosition, null);
                        chosenArmyItem.Army = resultArmy;
                        return(chosenArmyItem);
                    }
                    //The result is not our army. Delete our army and return another.
                    chosenArmyItem.StoredObject.SetActive(false);
                    targetBlock.SetItem(targetPosition, null);
                    clickedArmyItem.Army = resultArmy;
                    return(clickedArmyItem);
                }

                if (!(targetBlock.GetBonusItem(targetPosition) is Pass))
                {
                    //The target position is not a pass, so our army simply transfers, and the result is our army.
                    return(chosenArmyItem);
                }

                //The target position contains a pass. Update the target block and position and continue.
                var pass = targetBlock.GetBonusItem(targetPosition) as Pass;
                targetBlock    = boardStorage.GetBlock(pass.ToBlock);
                targetPosition = pass.ToPosition;
            }
        }