Exemple #1
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();
    }
Exemple #2
0
    void scanSelf()
    {
        if (this.Visible)
        {
            PointGraph navg = (PointGraph)AstarPath.active.graphs[0];
            AstarPath.active.AddWorkItem(new AstarPath.AstarWorkItem(delegate() {
                //float f = Time.realtimeSinceStartup;
                PointNode p = navg.AddNode((Int3)pathLocation);

                if (p != null)
                {
                    Dictionary <GraphNode, float> cons = new Dictionary <GraphNode, float>();
                    navg.GetNodes((delegate(GraphNode node) {
                        float distance;
                        if (navg.IsValidConnection(p, node, out distance))
                        {
                            //Debug.Log(distance);
                            cons[node] = distance;
                        }
                        return(true);
                    }));


                    foreach (GraphNode g in cons.Keys)
                    {
                        p.Area = 0;
                        g.Area = 0;
                        p.AddConnection(g, (uint)cons[g]);
                        g.AddConnection(p, (uint)cons[g]);
                    }

                    //Debug.Log("Found " + cons.Count + " valid connections");
                    //Debug.Log("Scan time: " + (Time.realtimeSinceStartup - f));
                }
            }, null));
        }
    }