Esempio n. 1
0
        public void PrepareSwapTileWithSelected(TileGridCell other)
        {
            var up    = "MoveUp";
            var right = "MoveRight";
            var down  = "MoveDown";
            var left  = "MoveLeft";

            if (other.Data.x > selectedCell.Data.x)
            {
                other.AnimateMove(left);
                selectedCell.AnimateMove(right);
            }
            else if (other.Data.x < selectedCell.Data.x)
            {
                other.AnimateMove(right);
                selectedCell.AnimateMove(left);
            }
            else if (other.Data.y > selectedCell.Data.y)
            {
                other.AnimateMove(down);
                selectedCell.AnimateMove(up);
            }
            else if (other.Data.y < selectedCell.Data.y)
            {
                other.AnimateMove(up);
                selectedCell.AnimateMove(down);
            }
        }
Esempio n. 2
0
    private void checkForFalling()
    {
        // TODO This is gonna get weird with rotation
        if (GridCell == null)
        {
            return;
        }

        var cellBelow = GridCell.GetCellBelow();

        if (fallToCell == null)
        {
            fallThroughTiles = new List <TileGridCell>();
        }

        while (cellBelow != null && cellBelow.Tile == null && cellBelow.awaitingFallingTile == null)
        {
            fallThroughTiles.Add(cellBelow);
            fallToCell = cellBelow;
            cellBelow  = cellBelow.GetCellBelow();
        }

        if (fallToCell != null && fallRoutine == null)
        {
            fallRoutine = StartCoroutine(fall());
        }

        if (fallToCell == null)
        {
            LockedIn = true;
        }
    }
Esempio n. 3
0
    public bool IsNeighbor(TileGridCell other, bool requireCardinal)
    {
        if (other == null || other == this)
        {
            return(false);
        }

        if (requireCardinal)
        {
            foreach (var neighbor in CardinalNeighbors())
            {
                if (neighbor == other)
                {
                    return(true);
                }
            }
        }
        else
        {
            foreach (var neighbor in AllNeighbors())
            {
                if (neighbor == other)
                {
                    return(true);
                }
            }
        }

        return(false);
    }
Esempio n. 4
0
        public bool CanSwapTileWithSelected(TileGridCell other)
        {
            if (other == null || other == selectedCell || !other.TileReady || !selectedCell.TileReady)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 5
0
        public void ChooseSwapTarget(TileGridCell cell)
        {
            if (selectedCell != null)
            {
                selectedCell.UnmakeSwapTarget();
            }

            selectedCell = cell;
        }
Esempio n. 6
0
    private void parentToCell(TileGridCell cell, bool destroyIfConflict = false)
    {
        /*if (destroyIfConflict && cell.tileContainer.transform.childCount > 0)
         * {
         *      Destroy(cell.transform.GetChild(0).gameObject);
         *      cell.Tile = this;
         * }*/

        transform.SetParent(GridCell.tileContainer.transform);
    }
Esempio n. 7
0
    private bool IsValidMatch(TileGridCell other)
    {
        if ((other != null && other != this) &&
            (other.Tile != null && other.TileReady) &&
            (Tile != null && TileReady))
        {
            return(other.Tile.IsMatch(Tile));
        }

        return(false);
    }
Esempio n. 8
0
    public void removeFromCell()
    {
        LockedIn = false;
        transform.SetParent(GridCell.Grid.orphanTileContainer);

        if (GridCell != null)
        {
            GridCell.Tile = null;
        }

        GridCell = null;
    }
Esempio n. 9
0
    public void BuildDestructionLists(TileGridCell starter, int order)
    {
        // TODO This method thing is very expensive... maybe we can optimize it.

        if (Grid == null)
        {
            return;
        }


        if (starter == null)
        {
            if (Grid.PrepareToDetonate(this, 0, this))
            {
                order   = 0;
                starter = this;
            }
            else
            {
                // A list starting with cell already exists, don't try starting a new one.
                return;
            }
        }

        var recurseNeighbors = new List <TileGridCell>();

        foreach (var neighbor in CardinalNeighbors())
        {
            if (neighbor != null && neighbor.IsValidMatch(this))
            {
                if (MatchInLine(neighbor))
                {
                    if (Grid.PrepareToDetonate(neighbor, order + 1, starter))
                    {
                        recurseNeighbors.Add(neighbor);
                    }
                }
            }
        }

        foreach (var neighbor in AllNeighbors())
        {
            if (neighbor != null && neighbor.Tile != null)
            {
            }
        }

        foreach (var neighbor in recurseNeighbors)
        {
            neighbor.BuildDestructionLists(starter, order++);
        }
    }
Esempio n. 10
0
        public bool PrepareToDetonate(TileGridCell toDetonate, int order, TileGridCell startingCell)
        {
            if (startingCell == null)
            {
                return(false);
            }

            if (!DetonateCells.ContainsKey(startingCell))
            {
                DetonateCells.Add(startingCell, new List <DetonateCellData>());
                DetonateCells[startingCell].Add(new DetonateCellData()
                {
                    cell  = startingCell,
                    tile  = startingCell.Tile,
                    order = 0
                });

                return(true);
            }

            bool alreadyTracked = false;
            bool alteredList    = false;

            foreach (var cellData in DetonateCells[startingCell])
            {
                if (cellData.cell == toDetonate)
                {
                    alreadyTracked = true;
                    if (order < cellData.order)
                    {
                        cellData.order = order;
                        alteredList    = true;
                    }

                    break;
                }
            }

            if (!alreadyTracked)
            {
                alteredList = true;
                DetonateCells[startingCell].Add(new DetonateCellData()
                {
                    cell  = toDetonate,
                    tile  = toDetonate.Tile,
                    order = order
                });
            }

            return(alteredList);
        }
Esempio n. 11
0
        public bool CanBeginSwap(TileGridCell cell)
        {
            if (cell != null)
            {
                if (selectedCell == null)
                {
                    return(true);
                }

                // If the cell is a cardinal neighbor, we'll want to swap with it, not let it take our swap.
                if (selectedCell.IsNeighbor(cell, true))
                {
                    return(false);
                }

                // The cell is not a relevant neighbor, so it can take the swap.
                return(true);
            }

            return(false);
        }
Esempio n. 12
0
    public bool MatchInLine(TileGridCell other)
    {
        if (other == this || other == null)
        {
            return(false);
        }

        int xDiff = Data.x - other.Data.x;
        int yDiff = Data.y - other.Data.y;

        // Only count nearby neigbors.
        if (Mathf.Abs(xDiff) + Mathf.Abs(yDiff) > 1)
        {
            return(false);
        }

        var oppositeNeighbor = Grid.Cell(Data.x + xDiff, Data.y + yDiff);

        if (oppositeNeighbor != null)
        {
            if (oppositeNeighbor.IsValidMatch(other))
            {
                return(true);
            }
        }

        var otherOppositeNeighbor = Grid.Cell(other.Data.x - xDiff, other.Data.y - yDiff);

        if (otherOppositeNeighbor)
        {
            if (otherOppositeNeighbor.IsValidMatch(this))
            {
                return(true);
            }
        }

        return(false);
    }
Esempio n. 13
0
        public TileGridCell GetCellBelow(TileGridCell cell)
        {
            if (cell == null || cell.Data == null)
            {
                return(null);
            }

            //TODO Handle rotation;
            switch (topSide)
            {
            case TopSide.Left:
                return(Cell(cell.Data.x + 1, cell.Data.y));

            case TopSide.Right:
                return(Cell(cell.Data.x - 1, cell.Data.y));

            case TopSide.Down:
                return(Cell(cell.Data.x, cell.Data.y + 1));

            case TopSide.Up:
            default:
                return(Cell(cell.Data.x, cell.Data.y - 1));
            }
        }
Esempio n. 14
0
        public void SwapTileWithSelected(TileGridCell other)
        {
            if (other == selectedCell || selectedCell == null)
            {
                return;
            }

            var tempTile = other.Tile;

            other.Tile        = selectedCell.Tile;
            selectedCell.Tile = tempTile;

            selectedCell.Tile.MoveToGridCell();
            other.Tile.MoveToGridCell();

            selectedCell.CheckForTriplet();
            other.CheckForTriplet();

            StartCoroutine(DetonateAndBurn(selectedCell));
            StartCoroutine(DetonateAndBurn(other));

            ChooseSwapTarget(null);
            Swapper.HideSides();
        }
Esempio n. 15
0
        public IEnumerator DetonateAndBurn(TileGridCell starter)
        {
            if (starter != null && DetonateCells.ContainsKey(starter))
            {
                List <DetonateCellData> detonateData = DetonateCells[starter];
                DetonateCells.Remove(starter);

                int  order = 0;
                bool done  = false;

                while (!done)
                {
                    var toDetonate    = new List <DetonateCellData>();
                    var detonateCells = new List <TileGridCell>();


                    for (int i = 0; i < detonateData.Count; i++)
                    {
                        // TODO Remove detonatedCells from list (make sure this doesn't break list checks when adding news (like weird loops)

                        var cell = detonateData[i];
                        if (cell != null && cell.order == order)
                        {
                            toDetonate.Add(cell);
                            detonateCells.Add(cell.cell);
                            detonateData.RemoveAt(i);
                            i--;
                        }
                    }

                    if (toDetonate.Count > 0)
                    {
                        for (int i = 0; i < toDetonate.Count; i++)
                        {
                            var cell = toDetonate[i];
                            if (cell == null || cell.cell == null || cell.cell.Tile == null || cell.tile != cell.cell.Tile)
                            {
                                continue;
                            }

                            cell.cell.Tile.Detonate();

                            foreach (var neighbor in cell.cell.CardinalNeighbors())
                            {
                                var burn = neighbor.TileReady;
                                if (burn && !detonateCells.Contains(neighbor))
                                {
                                    if (neighbor.Tile != null)
                                    {
                                        neighbor.Tile.Burn();
                                    }
                                }
                            }
                        }

                        yield return(null);

                        yield return(null);

                        foreach (var cell in cells)
                        {
                            if (cell != null)
                            {
                                cell.HandleGridChange();
                            }
                        }

                        for (int i = 0; i < detonateOrderDelay; i++)
                        {
                            yield return(null);
                        }

                        order++;
                    }
                    else
                    {
                        done = true;
                    }
                }

                detonateData.Clear();
            }
        }
Esempio n. 16
0
    private IEnumerator fall()
    {
        for (int i = 0; i < waitFramesBeforeFall; i++)
        {
            yield return(null);
        }

        if (GridCell != null)
        {
            removeFromCell();
            fallToCell.awaitingFallingTile = this;
        }

        bool stillFalling = true;

        while (stillFalling)
        {
            if (fallToCell.Grid.Rotating)
            {
                float        bestDist = -1;
                TileGridCell bestCell = null;

                while (bestCell == null)
                {
                    foreach (var cell in fallThroughTiles)
                    {
                        var sqrDist = (transform.position - cell.transform.position).sqrMagnitude;
                        if (cell.Tile == null && cell.awaitingFallingTile && (bestDist < 0 || sqrDist < bestDist))
                        {
                            bestDist = sqrDist;
                            bestCell = cell;
                        }
                    }

                    if (bestCell == null)
                    {
                        yield return(null);
                    }
                }

                fallToCell.awaitingFallingTile = null;
                bestCell.Tile = this;
                parentToCell(bestCell, true);
                transform.localPosition = Vector3.zero;

                while (fallToCell.Grid.Rotating)
                {
                    yield return(null);
                }

                fallRoutine = null;
                fallToCell  = null;

                yield break;
            }

            // TODO Handle rotation.
            var localPos = transform.localPosition;

            //localPos.y -= maxFallSpeed;
            localPos -= transform.InverseTransformDirection(Vector3.up) * maxFallSpeed;

            if (transform.position.y < fallToCell.transform.position.y)
            {
                localPos     = fallToCell.transform.localPosition;
                stillFalling = false;
            }

            transform.localPosition = localPos;
            yield return(null);
        }

        bool nowhereToFall = false;

        if ((fallToCell.awaitingFallingTile != null && fallToCell.awaitingFallingTile != this) ||
            (fallToCell.Tile && fallToCell.Tile != this))
        {
            nowhereToFall = true;
        }

        if (!nowhereToFall)
        {
            fallToCell.awaitingFallingTile = null;
            fallToCell.Tile = this;
            parentToCell(fallToCell, true);

            fallRoutine = null;
            fallToCell  = null;
            LockedIn    = true;

            HandleGridChange();

            data.FallIntoPlace(this);

            if (rematchAfterFall)
            {
                yield return(null);

                if (GridCell != null)
                {
                    GridCell.CheckForTriplet();
                    GridCell.Grid.StartCoroutine(GridCell.Grid.DetonateAndBurn(GridCell));
                }
            }
        }
        else
        {
            Destroy(gameObject);
        }
    }