Ejemplo n.º 1
0
    private void ScanRoomPath(Vector2 position)
    {
        // This holds all graph data
        AstarData data = AstarPath.active.data;

        // This creates a Grid Graph
        GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;

        // Setup a grid graph with some values
        int   width    = 31;
        int   depth    = 31;
        float nodeSize = 1;

        gg.center = position;

        gg.rotation = new Vector3(90, 0, 0);

        gg.collision.heightCheck = false;

        gg.collision.mask = LayerMask.GetMask("Obstacle");
        // Updates internal size from the above values
        gg.SetDimensions(width, depth, nodeSize);
        gg.collision.use2D          = true;
        gg.collision.collisionCheck = true;
        gg.collision.type           = ColliderType.Ray;

        /*// Scans all graphs
         * AstarPath.active.Scan();*/
    }
    void Start()
    {
        var size = wallMap.size;
        var cent = wallMap.cellBounds.center;

        cent *= tileSize;

        // This creates a Grid Graph
        AstarData data = AstarPath.active.data;
        GridGraph gg   = data.AddGraph(typeof(GridGraph)) as GridGraph;
        var       gc   = new GraphCollision();

        gc.use2D = true;

        /*var layer1 = LayerMask.NameToLayer("Environment");
         * var layer2 = LayerMask.NameToLayer("Enemy");
         * var mask1 = 1 << layer1;
         * var mask2 = 1 << layer2;
         * var combinedMask = mask1 | mask2;*/
        gc.mask      = LayerMask.GetMask("Environment");
        gc.diameter  = tileSize / 2f;
        gg.collision = gc;
        gg.SetDimensions(size.x, size.y, tileSize);
        gg.center   = cent;
        gg.rotation = new Vector3(gg.rotation.y - 90, 270, 90);
        StartCoroutine(ScanMap());
    }
Ejemplo n.º 3
0
    public bool AiGridPath(int depth, int width, float nodeSize, float diameter, bool cutCorners)
    {
        // This holds all graph data
        AstarData data = AstarPath.active.data;

        data.cacheStartup = false;

        // This creates a Grid Graph
        GridGraph gridGraph = data.AddGraph(typeof(GridGraph)) as GridGraph;

        // Setting up the default parameters.
        //gridGraph.width = width;
        //gridGraph.depth = depth;
        //gridGraph.nodeSize = nodeSize;

        // Because it's 2d we are rotating to face the camera, which looks down the z - axis
        gridGraph.rotation.x = -90.0f;

        // Calculating the centre based on node size and number of nodes
        gridGraph.center.x = (width * nodeSize);
        gridGraph.center.y = (depth * nodeSize);
        gridGraph.center   = new Vector3(gridGraph.center.x, gridGraph.center.y, 0);

        // Enabled corner cutting, disable slop detection and change slop axis to Z
        gridGraph.cutCorners = cutCorners;
        //gridGraph.maxClimb = 0;
        //gridGraph.

        // Setting to use 2d grid collision detection
        gridGraph.collision.use2D    = true;
        gridGraph.collision.type     = ColliderType.Sphere;
        gridGraph.collision.diameter = diameter;
        gridGraph.collision.mask     = LayerMask.GetMask("PlayerObstacles");

        // Updates internal size from the above values
        gridGraph.SetDimensions(width * 2, depth * 2, nodeSize);


        // Scans all graphs, do not call gg.Scan(), that is an internal method
        AstarPath.active.Scan();

        return(true);

        // this is used to start the coroutine that only renders stuff close to the player
        // has to be called after the final scan so the grid knows where to draw the entire dungeon

        //Debug.Log("Scan is now complete");
    }
Ejemplo n.º 4
0
    private void CreatePathGraphic()
    {
        AstarData data = AstarPath.active.data;

        _GridGraph = data.AddGraph(typeof(GridGraph)) as GridGraph;

        _GridGraph.SetDimensions(_Width, _Depth, _NodeSize);

        _GridGraph.rotation = Vector3.up * 45;

        _GridGraph.center = RoundVector3(_GridGraph.CalculateTransform().Transform(new Vector3(_Width, 0, _Depth))) - Vector3.right * Mathf.Sin(45);

        _GridGraph.collision.heightMask = LayerMask.GetMask(_HeightMaskName);

        AstarPath.active.Scan();
    }
Ejemplo n.º 5
0
        public WWAIManager()
        {
            GameObject obj = new GameObject("AIScript");

            obj.transform.position = new Vector3(0.0f, 0.0f, 0.0f);
            obj.AddComponent <AstarPath>();

            //RVOSimulator rsim = obj.AddComponent<RVOSimulator>();

            ProceduralGridMover gridMover = obj.AddComponent <ProceduralGridMover>();

            gridMover.target         = Camera.main.transform;
            gridMover.updateDistance = 5;
            gridMover.floodFill      = true;

            // This holds all graph data
            AstarData data = AstarPath.active.data;

            // This creates a Grid Graph
            LGG = data.AddGraph(typeof(LayerGridGraph)) as LayerGridGraph;

            // Setup a grid graph with some values
            int   width    = 80;
            int   depth    = 80;
            float nodeSize = 0.5f;

            LGG.characterHeight = 2;
            LGG.maxClimb        = 1.5f;

            LGG.collision.collisionOffset = 1;
            LGG.collision.diameter        = 2.5f;
            LGG.collision.type            = 0;

            LGG.center = new Vector3(0, 0, 0);

            // Updates internal size from the above values
            LGG.SetDimensions(width, depth, nodeSize);

            LGG.collision.heightMask = LayerMask.GetMask("Terrain");
            LGG.collision.mask       = LayerMask.GetMask("Obstacles");

            //set erode iterations to givebetter edges for obstacles
            LGG.erodeIterations = 0;

            // Scans graph
            LGG.Scan();
        }
Ejemplo n.º 6
0
    /**
     * creates the navmesh for our grid
     */
    private void CreateGraph()
    {
        AstarData data   = AstarPath.active.data;
        var       graphs = data.graphs.ToList();

        graphs.ForEach(x => data.RemoveGraph(x));
        PointGraph graph = data.AddGraph(typeof(PointGraph)) as PointGraph;

        AstarPath.active.Scan(graph);
        // Make sure we only modify the graph when all pathfinding threads are paused
        AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
            //create the graph
            //first make node array
            PointNode[,] nodeArray = new PointNode[map.GetUpperBound(0) + 1, map.GetUpperBound(1) + 1];

            for (int y = 0; y <= map.GetUpperBound(0); y++)
            {
                for (int x = 0; x <= map.GetUpperBound(1); x++)
                {
                    nodeArray[x, y] = graph.AddNode((Int3) new Vector3(x, y, 0));
                }
            }
            int connections = 0;
            //now connect nodes
            for (int y = 0; y <= map.GetUpperBound(0); y++)
            {
                for (int x = 0; x <= map.GetUpperBound(1); x++)
                {
                    for (int i = x - 1; i <= x + 1; i++)
                    {
                        for (int j = y - 1; j <= y + 1; j++)
                        {
                            if (InBounds(nodeArray, i, j))
                            {
                                nodeArray[x, y].AddConnection(nodeArray[i, j], (uint)map[x, y].MoveCost);
                                connections++;
                            }
                        }
                    }
                }
            }
        }));

        // Run the above work item immediately
        AstarPath.active.FlushWorkItems();
    }
Ejemplo n.º 7
0
    // Generates a grid graph for the A* Pathfinding system.
    private void generateGridGraph(int width, int height)
    {
        AstarData data     = AstarPath.active.data;
        GridGraph gg       = data.AddGraph(typeof(GridGraph)) as GridGraph;
        float     nodeSize = 1f;

        gg.center = new Vector3((width / 2) - 0.5f, 0, (height / 2) - 0.5f);
        gg.SetDimensions(width, height, nodeSize);
        gg.neighbours = NumNeighbours.Four;
        AstarPath.active.Scan();
        AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    var node      = gg.GetNode(x, y);
                    node.Walkable = walkable[x, y];
                }
            }
            gg.GetNodes(node => gg.CalculateConnections((GridNodeBase)node));
        }));
    }
Ejemplo n.º 8
0
    // sets up pathfinding
    // To get walking/flying working with dynamic room generation, some complexity was required
    public void AddGraph()
    {
        AstarData data = AstarPath.active.astarData;
        // This creates a Grid Graph
        GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;

        // Setup a grid graph with some values
        gg.width  = 50;
        gg.depth  = 40;
        gg.center = transform.position + new Vector3(0, -5, 0);
        // Updates internal size from the above values
        gg.UpdateSizeFromWidthDepth();

        GraphCollision gc = new GraphCollision();

        gc.collisionCheck = true;
        gc.heightCheck    = true;
        gc.diameter       = 2F;
        gc.heightMask     = LayerMask.GetMask(new string[] { "Rock", "Room", "Floor" });
        gc.mask           = LayerMask.GetMask(new string[] { "Rock", "Room" });
        gg.maxClimb       = 8F;
        gg.collision      = gc;
    }
Ejemplo n.º 9
0
 /// <summary>
 /// 创建网格路面
 /// </summary>
 /// <param name="center"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="nodeSize"></param>
 public void CreateGridGraph(Vector3Int center, int width, int height)
 {
     if (dicGridGraph.TryGetValue(center, out GridGraph value))
     {
         value.Scan();
     }
     else
     {
         AstarData astarData = AstarPath.active.data;
         GridGraph gridGraph = astarData.AddGraph(typeof(GridGraph)) as GridGraph;
         gridGraph.center = center;
         gridGraph.SetDimensions(width, width, 0.5f);
         gridGraph.name          = center + "";
         gridGraph.maxStepHeight = 1.5f;
         gridGraph.maxSlope      = 90;
         GraphCollision graphCollision = gridGraph.collision;
         graphCollision.collisionCheck = false;
         graphCollision.fromHeight     = height;
         graphCollision.mask           = (1 << LayerInfo.Chunk);
         dicGridGraph.Add(center, gridGraph);
         gridGraph.Scan();
     }
 }
Ejemplo n.º 10
0
    void CreatePathGraphic()
    {
        // This holds all graph data
        AstarData data = AstarPath.active.data;

        // This creates a Grid Graph
        GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;

        // Setup a grid graph with some values
        int   width    = 1000;
        int   depth    = 1000;
        float nodeSize = 1;

        // Updates internal size from the above values
        gg.SetDimensions(width, depth, nodeSize);

        gg.center = new Vector3(0, 0, 0);
        //gg.collision.mask = LayerMask.GetMask("Obstacles");
        gg.collision.heightMask = LayerMask.GetMask("Ground");

        // Scans all graphs
        AstarPath.active.Scan();

        AstarPath.active.AddWorkItem(new AstarWorkItem(ctx =>
        {
            var ggd = AstarPath.active.data.gridGraph;
            for (int i = 995; i < 1000; i++)
            {
                for (int j = 995; j < 1000; j++)
                {
                    var node      = ggd.GetNode(j, i);
                    node.Walkable = false;
                    ggd.CalculateConnectionsForCellAndNeighbours(j, i);
                }
            }
        }));
    }
Ejemplo n.º 11
0
    void Spawn()
    {
        if (spawned == false)
        {
            spawned = true;
            if (openingDirection == 1)
            {
                // Need to spawn a room with a BOTTOM door.

                bool up     = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX && e.StatesY == roomParent.statesY + 2);
                bool right  = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX + 1 && e.StatesY == roomParent.statesY + 1);
                bool bottom = false;
                bool left   = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX - 1 && e.StatesY == roomParent.statesY + 1);

                AddRoom room = RoomTemplates.Instance.GetBottomRoomWithConstraint(up, right, bottom, left);
                room.SetNode(roomParent.statesX, roomParent.statesY + 1);

                Instantiate(room.gameObject, transform.position, room.transform.rotation);
            }
            else if (openingDirection == 2)
            {
                // Need to spawn a room with a TOP door.

                bool up     = false;
                bool right  = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX + 1 && e.StatesY == roomParent.statesY - 1);
                bool bottom = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX && e.StatesY == roomParent.statesY - 2);
                bool left   = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX - 1 && e.StatesY == roomParent.statesY - 1);

                AddRoom room = RoomTemplates.Instance.GetTopRoomWithConstraint(up, right, bottom, left);
                room.SetNode(roomParent.statesX, roomParent.statesY - 1);
                Instantiate(room.gameObject, transform.position, room.transform.rotation);
            }
            else if (openingDirection == 3)
            {
                // Need to spawn a room with a LEFT door.

                bool up     = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX + 1 && e.StatesY == roomParent.statesY + 1);
                bool right  = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX + 2 && e.StatesY == roomParent.statesY);
                bool bottom = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX + 1 && e.StatesY == roomParent.statesY - 1);
                bool left   = false;

                AddRoom room = RoomTemplates.Instance.GetLeftRoomWithConstraint(up, right, bottom, left);
                room.SetNode(roomParent.statesX + 1, roomParent.statesY);
                Instantiate(room.gameObject, transform.position, room.transform.rotation);
            }
            else if (openingDirection == 4)
            {
                // Need to spawn a room with a RIGHT door.

                bool up     = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX - 1 && e.StatesY == roomParent.statesY + 1);
                bool right  = false;
                bool bottom = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX - 1 && e.StatesY == roomParent.statesY - 1);
                bool left   = NodeRoomHelper.RoomNode.Any(e => e.StatesX == roomParent.statesX - 2 && e.StatesY == roomParent.statesY);

                AddRoom room = RoomTemplates.Instance.GetRightRoomWithConstraint(up, right, bottom, left);
                room.SetNode(roomParent.statesX - 1, roomParent.statesY);
                Instantiate(room.gameObject, transform.position, room.transform.rotation);
            }

            // This holds all graph data
            AstarData data = AstarPath.active.data;

            // This creates a Grid Graph
            GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;

            // Setup a grid graph with some values
            int   width    = 26;
            int   depth    = 18;
            float nodeSize = 1;

            gg.center = this.transform.position;

            gg.rotation = new Vector3(90, 0, 0);

            gg.collision.heightCheck = false;

            gg.collision.mask = layer;
            // Updates internal size from the above values
            gg.SetDimensions(width, depth, nodeSize);
            gg.collision.use2D          = true;
            gg.collision.collisionCheck = true;
            gg.collision.type           = ColliderType.Ray;

            // Scans all graphs
            AstarPath.active.Scan();
        }
    }