Пример #1
0
    public bool hasCreatedHealthPath = false; //to stop the path from being created every time the update ticks
    //works identically to the State_Search_Ammo state
    public override void Execute(Agent agent)
    {
        agent.SB.PursuitOff();
        if (hasCreatedHealthPath == false)
        {
            agent.canMove = true;

            agent.SB.PathOff();
            Astarpathfinding pathFind     = new Astarpathfinding();
            CreateNodes      graphBuilder = new CreateNodes();
            NavGraph         tempGraph    = graphBuilder.createGraph();
            PathFollow       followCode   = new PathFollow();
            FindNearestNode  nodeFinder   = new FindNearestNode();
            pathFind.Initialize(tempGraph);
            pathFind.Check(tempGraph, nodeFinder.FindNearestIndex(agent.transform.position), nodeFinder.FindNearestIndex(agent.closestHp.transform.position));
            followCode.CreatePath(pathFind.Path, tempGraph);
            if (Vector3.Distance(tempGraph.Nodes[followCode.nodePath[0]].position, tempGraph.Nodes[followCode.nodePath[1]].position) > Vector3.Distance(agent.transform.position, tempGraph.Nodes[followCode.nodePath[1]].position) && followCode.nodePath.Count > 1)
            {
                followCode.nodePath.RemoveAt(0);
            }
            agent.SB.path = followCode;
            agent.SB.PathOn();
            hasCreatedHealthPath = true;
        }
        if (Vector3.Distance(agent.transform.position, agent.SB.path.nodeList[agent.SB.path.nodePath[agent.SB.path.nodePath.Count - 1]].position) < 8)
        {
            agent.SB.PathOff();
            agent.SB.SeekOn(agent.closestHp.transform.position, 5);
        }
    }
Пример #2
0
    public bool hasCreatedPath = false; //to stop the path from being created every time the update ticks

    //on execute the state will create a new A* graph that will find the shortest route to the position of the "closestAmmo" object in the world.
    public override void Execute(Agent agent)
    {
        agent.SB.PursuitOff();
        if (hasCreatedPath == false)
        {
            agent.canMove = true;
            agent.SB.PathOff();
            Astarpathfinding pathFind     = new Astarpathfinding();
            CreateNodes      graphBuilder = new CreateNodes();
            NavGraph         tempGraph    = graphBuilder.createGraph();
            PathFollow       followCode   = new PathFollow();
            FindNearestNode  nodeFinder   = new FindNearestNode();
            pathFind.Initialize(tempGraph);
            pathFind.Check(tempGraph, nodeFinder.FindNearestIndex(agent.transform.position), nodeFinder.FindNearestIndex(agent.closestAmmo.transform.position));
            followCode.CreatePath(pathFind.Path, tempGraph);
            if (followCode.nodePath.Count > 1)
            {
                if (Vector3.Distance(tempGraph.Nodes[followCode.nodePath[0]].position, tempGraph.Nodes[followCode.nodePath[1]].position) > Vector3.Distance(agent.transform.position, tempGraph.Nodes[followCode.nodePath[1]].position))
                {
                    //if the AI is closer to the second node in the path than the first node is it means that the AI has passed the first node, and as such it is no longer needed and can be removed.
                    followCode.nodePath.RemoveAt(0);
                    Debug.Log("new first node is " + followCode.nodePath[0]);
                }
            }

            agent.SB.path = followCode;
            agent.SB.PathOn();
            hasCreatedPath = true;
            Debug.Log("searching for ammo");
        }
        //while there are still nodes in the path to the ammo
        if (agent.SB.path.nodePath.Count > 1)
        {
            //if it is within 8 units of the last node in the path the agent will begin to seek towards the actual ammo position instead
            if (Vector3.Distance(agent.transform.position, agent.SB.path.nodeList[agent.SB.path.nodePath[agent.SB.path.nodePath.Count - 1]].position) < 8)
            {
                agent.SB.PathOff();
                agent.SB.SeekOn(agent.closestAmmo.transform.position, 2);
            }
        }
        else
        {
            //if the AI is already next to the ammo when the state is executed it will simply seek to the ammo immediately
            agent.SB.PathOff();
            agent.SB.SeekOn(agent.closestAmmo.transform.position, 2);
        }


        //agent.patrolCounter++;
    }
Пример #3
0
    public int FindNearestIndex(Vector3 position)
    {
        CreateNodes nodeMaker       = new CreateNodes();
        int         nearestNode     = 0;              //the indext of the current nearest node
        float       closestDistance = float.MaxValue; //the distance to the current closest node
        NavGraph    graph           = nodeMaker.createGraph();

        for (int i = 0; i < graph.Nodes.Count; i++)
        {
            float tempDistance = Vector3.Distance(position, graph.Nodes[i].position);
            if (tempDistance < closestDistance)
            {
                closestDistance = tempDistance;
                nearestNode     = graph.Nodes[i].index;
            }
        }
        Debug.Log("nearest index is " + nearestNode);
        return(nearestNode);
    }