Example #1
0
 public void DestroyGem(Gem gem)
 {
     gemPoolDict[gem.type].Release(gem.gameObject);
 }
        /// <summary>
        /// Detect possible swaps.
        /// </summary>
        public MatchSwap?DetectSwap(LevelMap map)
        {
            for (int i = 1; i < map.Height - 1; ++i)
            {
                for (int j = 0; j < map.Width; ++j)
                {
                    // Get gem type of neighbour gems.
                    GemType self  = map[i, j];
                    GemType left  = (j > 0)             ? map[i, j - 1] : GemType.None;
                    GemType right = (j < map.Width - 1) ? map[i, j + 1] : GemType.None;
                    GemType up    = map[i - 1, j];
                    GemType down  = map[i + 1, j];

                    bool upDownMatched   = Gem.IsMatched(up, down);
                    bool upSelfMatched   = Gem.IsMatched(up, self);
                    bool downSelfMatched = Gem.IsMatched(down, self);

                    // up X O X down
                    if (upDownMatched && !upSelfMatched)
                    {
                        GemType matchedType = Gem.GetMatchedGemType(up, down);
                        if (Gem.IsMatched(matchedType, left))
                        {
                            return(CreateMatchSwap(j, i, j - 1, i, matchedType));
                        }

                        if (Gem.IsMatched(matchedType, right))
                        {
                            return(CreateMatchSwap(j, i, j + 1, i, matchedType));
                        }
                    }

                    // Magic Gem may apply to both cases below
                    // so we have to check both cases separately

                    // up X X O down
                    if (upSelfMatched)
                    {
                        GemType   matchedType = Gem.GetMatchedGemType(up, self);
                        MatchSwap?swap        = DetectGemSwap(map, j, i + 1, matchedType, new Vector2Int(0, -1));
                        if (swap.HasValue)
                        {
                            return(swap);
                        }
                    }

                    // up O X X down
                    if (downSelfMatched)
                    {
                        GemType   matchedType = Gem.GetMatchedGemType(down, self);
                        MatchSwap?swap        = DetectGemSwap(map, j, i - 1, matchedType, new Vector2Int(0, 1));
                        if (swap.HasValue)
                        {
                            return(swap);
                        }
                    }
                }
            }

            return(null);
        }