Пример #1
0
        public IEnumerator FixInitialGrid()
        {
            // need this here so coliders have updated from the shuffle (should be done suring fixed update as physics
            yield return(new WaitForFixedUpdate());

            var cells = GetCells();

            for (int x = 0; x < grid.Width; x++)
            {
                for (int y = 0; y < grid.Height; y++)
                {
                    var tile = grid.GetTileAt(x, y);

                    while (CreatesMatch(tile.gameObject))
                    {
                        Debug.Log($"match found in initial grid @({x},{y})");
                        Destroy(tile.gameObject);
                        grid.SetTileAt(generator.Build(new Vector3(x, y, transform.position.x)), x, y);

                        tile = grid.GetTileAt(x, y);
                        // need this here so coliders have updated from the new addition (should be done suring fixed update as physics
                        yield return(new WaitForFixedUpdate());
                    }
                }
            }
            yield return(null);
        }
Пример #2
0
        private System.Collections.IEnumerator DropColumn(int column)
        {
            int gap = 0;

            for (int row = 0; row < grid.Height; row++)
            {
                // if a gap or a blocker
                if (grid.cells[column, row].IsEmpty || !grid.cells[column, row].IsPassable)
                {
                    ++gap;
                }
                // if there are gaps or lockers below
                else if (gap > 0)
                {
                    int y = row - gap;

                    // keep going up the grid until reach a gap or back where we started
                    // if back where we started then it wasall blockers below
                    while (!grid.cells[column, y].IsEmpty && y < row)
                    {
                        ++y;
                    }

                    // need to move down
                    // TODO(chris) - maybe control this via an animation?
                    if (y < row)
                    {
                        var   tile1     = grid.GetTileAt(column, row);
                        var   aPos      = tile1.transform.position;
                        var   bPos      = new Vector3(column, y);
                        float lerpTime  = dropTileLerpTime * (row - y);
                        float timeSpent = 0;
                        while (timeSpent < lerpTime)
                        {
                            timeSpent += Time.deltaTime;
                            tile1.transform.position = Vector3.Lerp(aPos, bPos, timeSpent / lerpTime);
                            yield return(null);
                        }

                        grid.SetTileAt(grid.GetTileAt(column, row), column, y);
                        grid.SetTileAt(null, column, row);
                    }
                }
            }
            yield return(null);
        }
Пример #3
0
 private MatchElement MatchElementAt(RuntimeGridData grid, int i, int j)
 {
     return(new MatchElement
     {
         i = i,
         j = j,
         tile = grid.GetTileAt(i, j).gameObject
     });
 }
Пример #4
0
        private string ProcessTile(List <Match> matches, List <MatchElement> potentialMatch, RuntimeGridData grid, string lastTileTag, int x, int y)
        {
            var tile = grid.GetTileAt(x, y);

            if (tile == null)
            {
                return("");
            }



            // if first element
            if (potentialMatch.Count == 0)
            {
                potentialMatch.Add(MatchElementAt(grid, x, y));
            }
            // if same as the previously seen tiles
            else if (tile.CompareTag(lastTileTag))
            {
                potentialMatch.Add(MatchElementAt(grid, x, y));
            }
            // if this tag does not match that of the previous tile but we have built up enough for a match then add that new match
            else if (potentialMatch.Count > 2)
            {
                matches.Add(new Match(potentialMatch.ToArray()));
                potentialMatch.Clear();

                potentialMatch.Add(MatchElementAt(grid, x, y));
            }
            // not the same tag as before so ditch list we were building and start again
            else
            {
                potentialMatch.Clear();

                potentialMatch.Add(MatchElementAt(grid, x, y));
            }

            return(tile.tag);
        }
Пример #5
0
        private System.Collections.IEnumerator SwapTile(Vector2Int a, Vector2Int b)
        {
            try
            {
                yield return(StartCoroutine(TweenTiles(a, b)));

                var swapData = new SwapEventData
                {
                    swappedPos2 = a,
                    swappedPos1 = b
                };

                // trigger any effects that happen on tile swap (eg bombs exploding)
                var matches = grid.GetTileAt(a.x, a.y).OnSwap(grid.GetTileAt(b.x, b.y));
                matches.AddRange(grid.GetTileAt(b.x, b.y).OnSwap(grid.GetTileAt(a.x, a.y)));

                var matchCount = 0;
                var noMatches  = true;
                do
                {
                    yield return(null);


                    matches.AddRange(matchFinder.FindMatches(grid, swapData));

                    matchCount = matches.Count;

                    if (matchCount > 0)
                    {
                        noMatches = false;
                        // notify of matches found
                        MatchesFound.Invoke(new Matches
                        {
                            matches = matches
                        });

                        yield return(null);

                        destroyer.DestroyMatches(matches, swapData);

                        yield return(StartCoroutine(fixer.FixGrid()));

                        matches.Clear();
                    }
                } while (matchCount > 0);

                // if there were never any matches then want to swap the tiles back
                if (noMatches)
                {
                    yield return(new WaitForSeconds(0.3f));

                    yield return(StartCoroutine(TweenTiles(a, b)));
                }

                yield return(null);
            }
            finally
            {
                MatchesResolved.Invoke();
            }
        }
Пример #6
0
        private List <Match> MatchesCaused(int x, int y, Vector2Int dir)
        {
            var x2    = x + dir.x;
            var y2    = y + dir.y;
            var tile1 = grid.GetTileAt(x, y);
            var tile2 = grid.GetTileAt(x2, y2);

            // if one of them is a blocker we cant swap
            if (!grid.cells[x, y].IsPassable || !grid.cells[x2, y2].IsPassable)
            {
                return(new List <Match>());
            }

            // if anything creates matches on swap then we definietely have a match (tags are probably the wrong way to do this)
            if (tile1.CompareTag("MatchesOnSwap"))
            {
                var elements = new MatchElement[1];
                elements[0] = new MatchElement
                {
                    i    = x,
                    j    = y,
                    tile = tile1.gameObject
                };
                var match = new Match(elements, false, 0);
                match.causeElem = 0;
                var matches = new List <Match>();
                matches.Add(match);
                return(matches);
            }

            if (tile2.CompareTag("MatchesOnSwap"))
            {
                var elements = new MatchElement[1];
                elements[0] = new MatchElement
                {
                    i    = x2,
                    j    = y2,
                    tile = tile2.gameObject
                };
                var match = new Match(elements, false, 0);
                match.causeElem = 0;
                var matches = new List <Match>();
                matches.Add(match);
                return(matches);
            }

            var origionalPos = new Vector2Int(x, y);

            TmpSwap(origionalPos, origionalPos + dir);
            try
            {
                return(matchFinder.FindMatches(grid, new SwapEventData
                {
                    swappedPos1 = origionalPos,
                    swappedPos2 = origionalPos + dir
                }));
            }
            finally
            {
                TmpSwap(origionalPos, origionalPos + dir);
            }
        }