Ejemplo n.º 1
0
        /// <summary>
        /// Creates a classic black and white checkers board
        /// </summary>
        void CreateBoard()
        {
            Texture2D whiteCell = Resources.Load <Texture2D> ("Textures/cellWhite");
            Texture2D blackCell = Resources.Load <Texture2D> ("Textures/cellBlack");

            grid.gridTopology = GRID_TOPOLOGY.Box;
            grid.rowCount     = 8;
            grid.columnCount  = 8;

            bool even = false;

            for (int row = 0; row < grid.rowCount; row++)
            {
                even = !even;
                for (int col = 0; col < grid.columnCount; col++)
                {
                    even = !even;
                    Cell cell      = grid.CellGetAtPosition(col, row);
                    int  cellIndex = grid.CellGetIndex(cell);
                    if (even)
                    {
                        grid.CellToggle(cellIndex, true, whiteCell);
                    }
                    else
                    {
                        grid.CellToggle(cellIndex, true, blackCell);
                    }
                }
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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());
            }
        }
 void Update()
 {
     if (grid.cellHighlighted != null)
     {
         if (Input.GetMouseButtonDown(0))
         {
             Debug.Log("Left clicked on cell #" + grid.CellGetIndex(grid.cellHighlighted));
         }
         else if (Input.GetMouseButtonDown(1))
         {
             Debug.Log("Right clicked on cell #" + grid.CellGetIndex(grid.cellHighlighted));
         }
     }
 }
Ejemplo n.º 5
0
        // Highlight cells sequentially on each frame
        IEnumerator HighlightCell()
        {
            currentCol++;
            // run across the grid row by row
            if (currentCol >= grid.columnCount)
            {
                currentCol = 0;
                currentRow++;
                if (currentRow >= grid.rowCount)
                {
                    currentRow = 0;
                }
            }
            // get cell at current grid position and color it with fade out option
            Cell cell = grid.CellGetAtPosition(currentCol, currentRow);

            if (cell != null)
            {
                int   cellIndex = grid.CellGetIndex(cell);
                float duration  = Random.value * 2.5f + 0.5f;
                Color color     = new Color(Random.value, Random.value, Random.value);
                grid.CellFadeOut(cellIndex, color, duration);
            }
            // trigger next iteration after this frame
            yield return(new WaitForEndOfFrame());

            StartCoroutine(HighlightCell());
        }
Ejemplo n.º 6
0
        void TriggerRandomCell(Color color)
        {
            // We get a random vector from -0.5..0.5 on both X and Y (z is ignored)
            Vector2 localPosition = Random.onUnitSphere * 0.5f;
            Cell    cell          = grid.CellGetAtPosition(localPosition);

            if (cell != null)
            {
                int   cellIndex = grid.CellGetIndex(cell);
                float duration  = Random.value * 2.5f + 0.5f;
                grid.CellFadeOut(cellIndex, color, duration);
            }
        }
Ejemplo n.º 7
0
        // Use this for initialization
        void Start()
        {
            grid = Grid2D.instance;

            // setup GUI resizer - only for the demo
            GUIResizer.Init(800, 500);
            labelStyle                  = new GUIStyle();
            labelStyle.alignment        = TextAnchor.MiddleLeft;
            labelStyle.normal.textColor = Color.white;

            isSelectingStart = true;

            Random.InitState(2);

            // Draw some blocked areas
            for (int i = 0; i < 25; i++)
            {
                int row = Random.Range(2, grid.rowCount - 3);
                int col = Random.Range(2, grid.columnCount - 3);

                for (int j = -2; j <= 2; j++)
                {
                    for (int k = -2; k <= 2; k++)
                    {
                        int cellIndex = grid.CellGetIndex(row + j, col + k);
                        grid.CellSetGroup(cellIndex, CELL_OBSTACLE);
                        grid.CellToggle(cellIndex, true, Color.white);
                    }
                }
            }


            // Example: sets crossing costs for hexagon sides and draw a line
            int   barrierCost  = 100;
            Color barrierColor = Color.blue;
            float barrierWidth = 5f;

            for (int k = 0; k < 10; k++)
            {
                int cellIndex = grid.CellGetIndex(10, k + 20);
                if (!grid.cells [cellIndex].canCross)
                {
                    continue;
                }
                if (grid.cells [cellIndex].column % 2 == 0)
                {
                    grid.CellSetSideCrossCost(cellIndex, CELL_SIDE.Top, barrierCost);
                    grid.CellSetSideCrossCost(cellIndex, CELL_SIDE.TopLeft, barrierCost);
                    grid.CellSetSideCrossCost(cellIndex, CELL_SIDE.TopRight, barrierCost);
                    grid.DrawLine(cellIndex, CELL_SIDE.Top, barrierColor, barrierWidth);
                    grid.DrawLine(cellIndex, CELL_SIDE.TopLeft, barrierColor, barrierWidth);
                    grid.DrawLine(cellIndex, CELL_SIDE.TopRight, barrierColor, barrierWidth);
                }
                else
                {
                    grid.CellSetSideCrossCost(cellIndex, CELL_SIDE.Top, barrierCost);
                    grid.DrawLine(cellIndex, CELL_SIDE.Top, barrierColor, barrierWidth);
                }
            }



            // Hook into cell click event to toggle start selection or draw a computed path using A* path finding algorithm
            grid.OnCellClick += (cellIndex) => BuildPath(cellIndex);

            grid.OnCellEnter += (cellIndex) => ShowLineOfSight(cellIndex);
        }