Пример #1
0
        //
        //  creates a graph based on a grid layout. This function requires the
        //  dimensions of the environment and the number of cells required horizontally
        //  and vertically
        public static void CreateGrid(NavGraph graph, int cySize, int cxSize, int numCellsY, int numCellsX)
        {
            //need some temporaries to help calculate each node center
            float cellWidth  = (float)cxSize / (float)numCellsX;
            float cellHeight = (float)cySize / (float)numCellsY;

            float midX = cellWidth / 2.0f;
            float midY = cellHeight / 2.0f;

            //first create all the nodes
            for (int row = 0; row < numCellsY; ++row)
            {
                for (int col = 0; col < numCellsX; ++col)
                {
                    graph.AddNode(new NavGraphNode(graph.GetNextFreeNodeIndex(), new Vector2(midX + col * cellWidth, midY + row * cellHeight)));
                }
            }

            //now to calculate the edges. (A position in a 2d array [x][y] is the
            //same as [y*numCellsX + x] in a 1d array). Each cell has up to eight
            //neighbours.
            for (int row = 0; row < numCellsY; ++row)
            {
                for (int col = 0; col < numCellsX; ++col)
                {
                    AddAllNeighboursToGridNode(graph, row, col, numCellsX, numCellsY);
                }
            }
        }