Beispiel #1
0
    /// <summary>
    /// Check combination of current map
    /// </summary>
    /// <returns>
    /// The combine.
    /// </returns>
    public bool CheckCombine()
    {
        List <Combine> combines = new List <Combine>();

        //Check combinations by type
        for (int itemType = 0; itemType < Enum.GetNames(typeof(ItemType)).Length; itemType++)
        {
            //Check all combination in horizontal direction
            for (int row = 0; row < maxRow; row++)
            {
                List <Item> subCombines = new List <Item>();
                for (int col = 0; col < maxCol; col++)
                {
                    //Get current square
                    Square square = GetSquare(row, col);
                    if (!square.CanMoveItemOut())
                    {
                        if (subCombines.Count >= 3)
                        {
                            Combine combine = new Combine();
                            combine.AddItems(subCombines);
                            combines.Add(combine);
                        }
                        subCombines.Clear();
                        continue;
                    }
                    //If current square is same type with current-checking item type
                    //add it to subCombines
                    if (square.item.type == (ItemType)itemType)
                    {
                        subCombines.Add(square.item);
                    }
                    //Until current square is out of map or current square's item type not
                    //same with checking type, if subCombines is > 3 elements,
                    //add subcombines to list combines
                    if (square.item.type != (ItemType)itemType || col == maxCol - 1)
                    {
                        if (subCombines.Count >= 3)
                        {
                            Combine combine = new Combine();
                            combine.AddItems(subCombines);
                            combines.Add(combine);
                        }
                        subCombines.Clear();
                    }
                }
            }
            //Check all combination in horizontal direction, same with check all horizontal
            for (int col = 0; col < maxCol; col++)
            {
                List <Item> subCombines = new List <Item>();
                for (int row = 0; row < maxRow; row++)
                {
                    Square square = GetSquare(row, col);
                    if (!square.CanMoveItemOut())
                    {
                        if (subCombines.Count >= 3)
                        {
                            Combine combine = new Combine();
                            combine.AddItems(subCombines);
                            combines.Add(combine);
                        }
                        subCombines.Clear();
                        continue;
                    }
                    if (square.item.type == (ItemType)itemType)
                    {
                        subCombines.Add(square.item);
                    }
                    if (square.item.type != (ItemType)itemType || row == maxRow - 1)
                    {
                        if (subCombines.Count >= 3)
                        {
                            Combine combine = new Combine();
                            combine.AddItems(subCombines);
                            combines.Add(combine);
                        }
                        subCombines.Clear();
                    }
                }
            }
        }
        //If can found combinations, disappear hint effect, count explosion,
        //and remove all combinations' item
        if (combines.Count > 0)
        {
            GamePlay.Instance.explosionCount++;
            GamePlay.Instance.UnHint();
            StartCoroutine(RemoveCombines(combines));
            return(true);
        }
        else
        {
            //Reset selected item
            GamePlay.Instance.selectedItem = null;
            GamePlay.Instance.CheckWin();
        }
        return(false);
    }