示例#1
0
        ///<summary>
        ///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 
        ///</summary>
        ///<param name="graph"></param>
        ///<param name="cySize"></param>
        ///<param name="cxSize"></param>
        ///<param name="numCellsY"></param>
        ///<param name="numCellsX"></param>
        public static void CreateGrid(
            SparseGraph graph,
            int cySize,
            int cxSize,
            int numCellsY,
            int numCellsX)
        {
            //need some temporaries to help calculate each node center
            float cellWidth = (float) cySize/numCellsX;
            float cellHeight = (float) cxSize/numCellsY;

            float midX = cellWidth/2;
            float midY = cellHeight/2;

            //first create all the nodes
            for (int row = 0; row < numCellsY; ++row)
            {
                for (int col = 0; col < numCellsX; ++col)
                {
                    graph.AddNode(
                        new NavGraphNode(
                            graph.NextFreeNodeIndex,
                            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 neigbors.
            for (int row = 0; row < numCellsY; ++row)
            {
                for (int col = 0; col < numCellsX; ++col)
                {
                    AddAllNeighborsToGridNode(
                        graph,
                        row,
                        col,
                        numCellsX,
                        numCellsY);
                }
            }
        }