Exemple #1
0
    public void PathFound(List <Node> nodePath, AgentPathfinder agent, Vector3 to)
    {
        if (nodePath == null)
        {
            PathfindResult result = new PathfindResult(agent, null);
            lock (results){ results.Enqueue(result); }
            return;
        }

        vector3Path.Clear();

        //invert path and convert to Vector3
        // i > 0 as we don't want to add last one, see below comment
        for (int i = nodePath.Count - 1; i > 0; i--)
        {
            vector3Path.Add(nodePath[i].pos);
        }

        //last one should be exact position, not node position.
        vector3Path.Add(to);

        PathfindResult successfulResult = new PathfindResult(agent, vector3Path);

        lock (results) { results.Enqueue(successfulResult); }
    }
Exemple #2
0
    /// <summary>
    /// A_Star bits
    /// </summary>


    public void requestPath(Vector3 from, Vector3 to, AgentPathfinder agent)
    {
        Node fromNode = ReturnNodeFromVector3(from);

        int X = mapX;
        int Y = mapY;
        int Z = mapZ;


        if (to.x > X || to.x < .0f ||
            to.y > Y || to.y < .0f ||
            to.z > Z || to.z < .0f)
        {
            Debug.Log("Demanded an out of range vector destination!");
            PathfindResult result;
            result.agent = agent;
            return;
        }

        Node toNode = ReturnNodeFromVector3ToNode(from, to);

        Thread t = new Thread(() => FindPath(fromNode, toNode, to, agent));

        t.Start();

        //ThreadStart threadStart = delegate
        //{
        //	FindPath(fromNode, toNode, to, agent);
        //};
    }
    void Awake()
    {
        //Find the agent movement controller attached
        _agentPathfinder = GetComponent<AgentPathfinder>();

        WorkWaypoint = GameObject.FindGameObjectWithTag ("WorkWaypoint");

        //When an agent spawns, he should start by wandering
        aState = agentState.Default;

        //When an agent spawns, start updating his hunger level
        updateHunger();
    }
 private void Awake()
 {
     agentPathfinding = gameObject.GetComponent <AgentPathfinder>();
     stats            = GetComponent <AgentStats>();
 }
Exemple #5
0
 public PathfindResult(AgentPathfinder agent, List <Vector3> path)
 {
     this.agent = agent;
     this.path  = path;
 }
Exemple #6
0
    //This function is broken up to allow for easier profiling with Unity tools.
    void FindPath(Node from, Node to, Vector3 finalTo, AgentPathfinder agent)
    {
        Node current = FindPathInit(from);

        if (CheckReached(current, to))
        {
            PathFound(returnNodes, agent, finalTo);
            return;
        }

        while (openNodes.Count > 0)
        {
            current.f = current.g = current.h = 0;

            current = openNodes[0];
            openNodes.RemoveAt(0);

            if (CheckReached(current, to))
            {
                PathFound(returnNodes, agent, finalTo);
                return;
            }

            if (current == null)
            {
                Debug.Log("How is current null?!");
            }


            current.open    = false;
            current.visited = true;



            // check neighbours
            for (int x = -1; x < 2; x++)
            {
                if (current.intVec3.x + x < 0 || current.intVec3.x + x >= mapX)                 // do these checks early in the loop to ensure we don't waste time checking an entire row of null neighbours
                {
                    continue;
                }
                for (int y = -1; y < 2; y++)
                {
                    if (current.intVec3.y + y < 0 || current.intVec3.y + y >= mapY)
                    {
                        continue;
                    }
                    for (int z = -1; z < 2; z++)
                    {
                        if (current.intVec3.z + z < 0 || current.intVec3.z + z >= mapZ)
                        {
                            continue;
                        }


                        Node neighbour = null;
                        // check its a valid neighbour


                        neighbour = CheckNeighbourValidity(x, y, z, current);

                        if (neighbour == null)
                        {
                            continue;
                        }

                        float g = neighbour.g + nodeSize;
                        float h = manhattanHeuristic(neighbour, to);
                        float f = g + h;

                        bool inOpen = neighbour.open;

                        AddToOpenSet(inOpen, neighbour, f, g, h, current);
                    }
                }
            }
        }


        Debug.Log("WARNING: Destination could not be found for path find!!");
        PathFound(null, agent, finalTo);
    }