Ejemplo n.º 1
0
    public TokenChain FindMatches(SwapPuzzleTokenController cont, bool mustAddToChain = false)
    {
        TokenChain chain   = new TokenChain();
        bool       isAdded = !mustAddToChain;

        Vector2Int[] directions = new Vector2Int[] {
            Vector2Int.right, Vector2Int.up, Vector2Int.left, Vector2Int.down
        };

        foreach (Vector2Int dir in directions)
        {
            Vector2Int nearPos = cont.token.gridPosition + dir;
            if (grid.IsInsideGrid(nearPos))
            {
                var near = grid.tokens[nearPos.y, nearPos.x].GetComponent <SwapPuzzleTokenController>();
                // check nearest token
                if (!near.isChecking && near.token.elementId == cont.token.elementId)
                {
                    Vector2Int furtherPos = nearPos + dir;
                    Vector2Int backPos    = cont.token.gridPosition - dir;

                    if (grid.IsInsideGrid(furtherPos))
                    {
                        var further = grid.tokens[furtherPos.y, furtherPos.x].GetComponent <SwapPuzzleTokenController>();
                        if (further.token.elementId == cont.token.elementId)
                        {
                            if (!isAdded)
                            {
                                isAdded = true;
                                chain.Add(cont);
                            }

                            cont.isMatched    = true;
                            near.isMatched    = true;
                            further.isMatched = true;

                            bool checkNear = false, checkFurther = false;
                            if (!near.isChecking)
                            {
                                chain.Add(near);
                                near.isChecking = true;
                                checkNear       = true;
                            }

                            if (!further.isChecking)
                            {
                                chain.Add(further);
                                further.isChecking = true;
                                checkFurther       = true;
                            }

                            if (checkNear)
                            {
                                chain.Add(FindMatches(near));
                            }

                            if (checkFurther)
                            {
                                chain.Add(FindMatches(further));
                            }
                        }
                    }

                    if (grid.IsInsideGrid(backPos))
                    {
                        var back = grid.tokens[backPos.y, backPos.x].GetComponent <SwapPuzzleTokenController>();
                        if (back.token.elementId == cont.token.elementId)
                        {
                            if (!isAdded)
                            {
                                isAdded = true;
                                chain.Add(cont);
                            }

                            cont.isMatched = true;
                            near.isMatched = true;
                            back.isMatched = true;

                            bool checkNear = false, checkBack = false;
                            if (!near.isChecking)
                            {
                                chain.Add(near);
                                near.isChecking = true;
                                checkNear       = true;
                            }

                            if (!back.isChecking)
                            {
                                chain.Add(back);
                                back.isChecking = true;
                                checkBack       = true;
                            }

                            if (checkNear)
                            {
                                chain.Add(FindMatches(near));
                            }

                            if (checkBack)
                            {
                                chain.Add(FindMatches(back));
                            }
                        }
                    }
                }
            }
        }
        return(chain);
    }