void Start()
 {
     grid = Grid2D.instance;
 }
예제 #2
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);
        }