예제 #1
0
        void BuildPath(int clickedCellIndex)
        {
            // If clicked cell can't be crossed, return
            if (!grid.cells[clickedCellIndex].canCross)
            {
                return;
            }

            // Get Path
            int        cellIndex = grid.CellGetIndex(tokenRow, tokenColumn);
            List <int> path      = grid.FindPath(cellIndex, clickedCellIndex, movePoints);

            if (path != null)
            {
                movePoints -= path.Count;
                // Color the path
                for (int k = 0; k < path.Count; k++)
                {
                    grid.CellFlash(path [k], Color.green, 1f);
                }
                // Start animating/moving the ship
                StartCoroutine(MoveShipAlongPath(path));
                // Annotate new ship row and column
                tokenRow    = grid.CellGetRow(clickedCellIndex);
                tokenColumn = grid.CellGetColumn(clickedCellIndex);
            }
            else
            {
                // Indicate that cell is not reachable
                grid.CellFlash(clickedCellIndex, Color.red, 1f);
            }
        }
예제 #2
0
        void CheckMatchingCells(int cellIndex)
        {
            if (cellIndex < 0)
            {
                return;
            }

            Texture2D selectedFruit = grid.CellGetTexture(cellIndex);

            if (selectedFruit == null)
            {
                return;                                         // empty cell
            }
            // Checks all directions until all matches are selected and no pending cells remains
            List <int> matches = new List <int> ();

            matches.Add(cellIndex);
            List <int> pending = new List <int> ();

            pending.Add(cellIndex);

            while (pending.Count > 0)
            {
                // extract one fruit from the queue
                int p = pending [0];
                pending.RemoveAt(0);
                // check all 4 directions of that cell for new matches
                int row = grid.CellGetRow(p);
                int col = grid.CellGetColumn(p);
                for (int k = 0; k < directions.Length; k++)
                {
                    int row1 = row + directions [k] [0];
                    int col1 = col + directions [k] [1];
                    if (row1 >= 0 && row1 < grid.rowCount && col1 >= 0 && col1 < grid.columnCount)
                    {
                        int       i   = grid.CellGetIndex(row1, col1);
                        Texture2D tex = grid.CellGetTexture(i);
                        if (tex == selectedFruit && !matches.Contains(i))
                        {
                            matches.Add(i);
                            pending.Add(i);
                        }
                    }
                }
            }

            // If there're 3 or more matches remove them
            if (matches.Count >= 3)
            {
                matches.ForEach((int matchingIndex) => {
                    // Remove fruit
                    grid.CellSetTexture(matchingIndex, null);
                    // Produce a nice effect for matching cells
                    grid.CellFlash(matchingIndex, Color.yellow, 0.25f);
                });
                StartCoroutine(MakeFruitsFall());
            }
        }