private void TmpSwap(Vector2Int a, Vector2Int b)
        {
            var tmp = grid.GetTileAt(a.x, a.y);

            grid.SetTileAt(grid.GetTileAt(b.x, b.y), a.x, a.y);
            grid.SetTileAt(tmp, b.x, b.y);
        }
Exemple #2
0
        // suffers from primative obsession, if this becomes public use Vector2Int to
        // help make order of parameters less error prone
        private void SwapTiles(int x, int y, int x2, int y2)
        {
            var tmp = grid.cells[x, y].Child;

            grid.SetTileAt(grid.cells[x2, y2].Child, x, y);
            grid.SetTileAt(tmp, x2, y2);
            grid.cells[x, y].Child.transform.localPosition   = Vector3.zero;
            grid.cells[x2, y2].Child.transform.localPosition = Vector3.zero;
        }
Exemple #3
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);
        }
        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);
        }
        // move to an object that can be swapped out for an animation
        private System.Collections.IEnumerator TweenTiles(Vector2Int a, Vector2Int b)
        {
            // tween the 2 tiles
            var   tile1     = grid.GetTileAt(a.x, a.y);
            var   tile2     = grid.GetTileAt(b.x, b.y);
            var   aPos      = tile1.transform.position;
            var   bPos      = tile2.transform.position;
            float lerpTime  = .1f;
            float timeSpent = 0;

            while (timeSpent < lerpTime)
            {
                timeSpent += Time.deltaTime;
                tile1.transform.position = Vector3.Lerp(aPos, bPos, timeSpent / lerpTime);
                tile2.transform.position = Vector3.Lerp(bPos, aPos, timeSpent / lerpTime);
                yield return(null);
            }

            grid.SetTileAt(tile2, a.x, a.y);
            grid.SetTileAt(tile1, b.x, b.y);

            // last update
            yield return(null);
        }