// Use this for initialization
        public WaypointGraph GenerateGraph()
        {
            Debug.Log("Generating graph...");

            grid = new WaypointNode[gridSizeX, gridSizeZ];
            List <WaypointNode> nodes = new List <WaypointNode>();

            edges = new List <WaypointEdge>();

            for (int i = 0; i < gridSizeX; i++)
            {
                for (int j = 0; j < gridSizeZ; j++)
                {
                    WaypointNode node = new WaypointNode(new Vector3(gridOffset.x + i * cellSize, gridOffset.y, gridOffset.z + j * cellSize));
                    grid[i, j] = node;
                    nodes.Add(node);
                }
            }

            for (int i = 0; i < gridSizeX; i++)
            {
                for (int j = 0; j < gridSizeZ; j++)
                {
                    CheckNeighbours(i, j);
                }
            }



            Debug.Log("Generation done! N: " + nodes.Count + ", E: " + edges.Count);

            return(new WaypointGraph(nodes.ToArray(), edges.ToArray()));
        }
        void CheckNeighbours(int x, int y)
        {
            for (int a = -range; a <= range; a++)
            {
                for (int b = -range; b <= range; b++)
                {
                    if (x + a >= 0 && y + b >= 0 && x + a < gridSizeX && y + b < gridSizeZ)
                    {
                        WaypointNode current   = grid[x, y];
                        WaypointNode target    = grid[x + a, y + b];
                        float        halfWidth = .4f;

                        Vector3 perp = Vector3.Cross(current.position - target.position, Vector3.up);
                        perp.Normalize();
                        if (!IsRaycastColliding(current.position + perp * halfWidth, target.position + perp * halfWidth) &&
                            !IsRaycastColliding(current.position, target.position) &&
                            !IsRaycastColliding(current.position - perp * halfWidth, target.position - perp * halfWidth))
                        {
                            edges.Add(new WaypointEdge(current, target));
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public WaypointEdge(WaypointNode f, WaypointNode s)
 {
     first  = f;
     second = s;
 }