コード例 #1
0
    private void Recalculate(bool changeTarget)
    {
        List <GameObject> agents = new List <GameObject>();

        agents.Add(gameObject);


        // Source to Target
        if (changeTarget || Target != null)
        {
            Destroy(Target);
            Target  = SpawnAgentsScript.Spawn(false, agents, color);
            replans = 0; // can't forget this
        }
        var edge = DrawVisibilityGraphScript.DrawVisibilityEdge(
            transform.position, Target.transform.position,
            Color.green, false, true, false, true
            );

        Path = new List <Vector2>();
        if (edge != null)
        {
            Path.Add(edge[1]);
        }

        if (Path.Count > 0)   // if there's a direct path between two points, no need to even compute A*, just walk the path!
        {
            return;
        }

        // Clone a fresh new graph
        Graph          = new VisibilityGraph();
        Graph.Vertices = OriginalGraph.ClonedVertices();
        Graph.AdjList  = OriginalGraph.ClonedAdjList();
        Graph.SetSource(transform.position);
        Graph.SetTarget(Target.transform.position);

        // Debug.DrawLine(transform.position, Target, color, 100);

        // Graph.PrintNumEdges();

        // Edges from source/target to boundaries/obstacles
        DrawVisibilityGraphScript.DrawFromBoundaries(
            transform.position, Target.transform.position, color, Graph
            );

        DrawVisibilityGraphScript.DrawFromObstacles(
            transform.position, Target.transform.position, color, Graph
            );

        // Graph.PrintNumEdges();

        /*
         * if (Graph.AdjList.ContainsKey(transform.position)) {
         *  Debug.Log(Graph.AdjList[transform.position].Count);
         * } else {
         *  Debug.Log("Nada");
         * }
         */

        if (
            Graph.AdjList.ContainsKey(transform.position) &&
            Graph.AdjList[transform.position].Count > 0 &&
            Graph.AdjList.ContainsKey(Target.transform.position) &&
            Graph.AdjList[Target.transform.position].Count > 0
            )
        {
            // Debug.Log("A Star time.");
            // Debug.Log(Time.realtimeSinceStartup);
            float planningTime = Time.realtimeSinceStartup;
            Path         = Graph.AStar();
            planningTime = Time.realtimeSinceStartup - planningTime;
            StatsScript.TotalPlanningTime += planningTime;
            // Debug.Log(Time.realtimeSinceStartup);
        }
    }