Пример #1
0
    public override BehaviourStatus OnBehave(BehaviourState state)
    {
        BehaviourContext enemyContext = (BehaviourContext)state;

        if (enemyContext.enemyInSight())
        {
            playerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
        }

        if ((!enemyContext.enemyInRange(0.5f) && !enemyContext.enemyTooClose()))
        {
            enemyContext.enemyWeaponController.isFiring = false;
            return(BehaviourStatus.FAILURE);
        }

        if ((!enemyContext.enemyTooClose() && enemyContext.enemyInSight()))
        {
            enemyContext.enemyWeaponController.isFiring = false;
            return(BehaviourStatus.SUCCESS);
        }

        /////////////////////////////////////////////////////////////
        if (closestNode == null)
        {
            closestNode = PathFinder.getRetreatNode(enemyContext.pathNodes, enemyContext.enemyPhysics.transform, playerPosition);
        }

        if (PathFinder.atDestination(enemyContext.enemy.transform, closestNode.transform.position))
        {
            closestNode = PathFinder.getRetreatNeighbor(closestNode, enemyContext.enemy.transform, playerPosition);
            if (closestNode == null)
            {
                closestNode = PathFinder.getRetreatNode(enemyContext.pathNodes, enemyContext.enemyPhysics.transform, playerPosition);
            }
        }

        enemyContext.enemyPhysics.enemyRotation(playerPosition);
        if (enemyContext.enemyAnimation.GetCurrentAnimatorStateInfo(0).IsName("idle") ||
            enemyContext.enemyAnimation.GetCurrentAnimatorStateInfo(0).IsName("attack"))
        {
            enemyContext.enemyAnimation.Play("walk");
        }
        enemyContext.enemyPhysics.enemyMovement(closestNode.transform.position, angle);

        if (!enemyContext.enemyWeaponController.isFiring)
        {
            enemyContext.enemyWeaponController.isFiring = true;
        }
        ////////////////////////////////////////////////////////////////

        return(BehaviourStatus.RUNNING);
    }
Пример #2
0
    //Leaf node that checks whether or not the target is not seen or heard so it can patrol.

    public override BehaviourStatus OnBehave(BehaviourState state)
    {
        BehaviourContext enemyContext = (BehaviourContext)state;

        if (!enemyContext.enemyInSight() && !enemyContext.enemyInRange(0.5f) && !enemyContext.playerHeard)
        {
            if (enemyContext.enemySight.enableDebug)
            {
                Debug.Log("Player Not Found");
            }

            return(BehaviourStatus.SUCCESS);
        }

        return(BehaviourStatus.FAILURE);
    }
    //Leaf node responsible for getting the start node closest to the enemy and a random end node
    //responsible for the enemy patrolling.

    public override BehaviourStatus OnBehave(BehaviourState state)
    {
        BehaviourContext enemyContext = (BehaviourContext)state;

        if (enemyContext.enemyInSight() || enemyContext.enemyInRange(0.5f))
        {
            return(BehaviourStatus.FAILURE);
        }

        if (enemyContext.startNode == null)
        {
            enemyContext.startNode = PathFinder.getInitialNode(enemyContext.pathNodes,
                                                               enemyContext.enemyPhysics.transform.position);
            enemyContext.startInfo = enemyContext.startNode.GetComponent <NodeController> ();
        }
        enemyContext.endNode = PathFinder.getEndNode(enemyContext.pathNodes, enemyContext.startNode);
        return(BehaviourStatus.SUCCESS);
    }
Пример #4
0
    public override BehaviourStatus OnBehave(BehaviourState state)
    {
        BehaviourContext enemyContext = (BehaviourContext)state;


        if (!enemyContext.enemyInRange(0.5f) || !enemyContext.enemyInSight() || enemyContext.enemyTooClose())
        {
            if (enemyContext.enemyAnimation.GetCurrentAnimatorStateInfo(0).IsName("attack"))
            {
                enemyContext.enemyAnimation.Play("idle");
            }
            enemyContext.enemyWeaponController.isFiring = false;
            return(BehaviourStatus.FAILURE);
        }

        if (GameObject.FindGameObjectWithTag("Player") != null)
        {
            playerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
        }
        else
        {
            enemyContext.enemyWeaponController.isFiring = false;
            return(BehaviourStatus.SUCCESS);
        }

        enemyContext.enemyPhysics.enemyRotation(playerPosition);
        if (enemyContext.enemyAnimation.GetCurrentAnimatorStateInfo(0).IsName("idle") ||
            enemyContext.enemyAnimation.GetCurrentAnimatorStateInfo(0).IsName("walk"))
        {
            enemyContext.enemyAnimation.Play("attack");
        }

        if (!enemyContext.enemyWeaponController.isFiring)
        {
            enemyContext.enemyWeaponController.isFiring = true;
        }

        return(BehaviourStatus.RUNNING);
    }
Пример #5
0
    public override BehaviourStatus OnBehave(BehaviourState state)
    {
        BehaviourContext enemyContext = (BehaviourContext)state;

        //Checks if the player is in sight, or the player has fired a shot that is has heard recently to chase.
        if (enemyContext.enemyInSight())
        {
            playerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
        }
        else if (enemyContext.playerHeard && enemyContext.playerRecentShot)
        {
            enemyContext.playerRecentShot = false;
            RaycastHit hit;
            playerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
            Vector3 rayDirection = playerPosition - enemyContext.enemy.transform.position;
            if (Physics.Raycast(enemyContext.enemy.transform.position, rayDirection, out hit, enemyContext.enemySight.viewDistance))
            {
                if (hit.collider.tag == "Player")
                {
                    chaseHearing = true;
                }
                else
                {
                    chaseHearing = false;
                    hearPath     = true;
                }
            }
        }

        //Fails if the player is not in sight, and it has not been heard after it has finished its path.
        if (!enemyContext.enemyInSight() && !enemyContext.playerHeard && playerPath.Count == 0)
        {
            enemyContext.playerHeard = false;
            return(BehaviourStatus.FAILURE);
        }

        //If it has reached the players position and the player is in sight it successful, else it fails if it reaches the last known
        //destination with no player.
        if (atPlayer(enemyContext.enemy.transform, playerPosition, enemyContext.enemyRange) && enemyContext.enemyInSight())
        {
            enemyContext.enemyAnimation.Play("idle");
            enemyContext.playerHeard = false;
            return(BehaviourStatus.SUCCESS);
        }
        else if (PathFinder.atDestination(enemyContext.enemy.transform, playerPosition) && !enemyContext.enemyInSight())
        {
            chaseHearing             = false;
            enemyContext.playerHeard = false;
            return(BehaviourStatus.FAILURE);
        }

        if (Physics.Raycast(playerPosition, Vector3.down, out playerHit, 30.0f) && Physics.Raycast(enemyContext.enemy.transform.position, Vector3.down, out enemyHit, 10.0f))
        {
            //Checks if the player and enemy are on the same plane making sure they are heard or insight.
            //If so it moves to the player position.
            if (playerHit.collider.name == enemyHit.collider.name && (enemyContext.enemyInSight() || chaseHearing))
            {
                if (playerPath.Count > 0)
                {
                    playerPath.Clear();
                }

                targetPosition = playerPosition;

                //If not it calculates a path to the player so it avoids walls and running into obstacles.
            }
            else if (playerHit.collider.name != currentPlaneName && (enemyContext.enemyInSight() || (!chaseHearing && hearPath)))
            {
                startNode        = PathFinder.getPlaneNode(enemyContext.planeNodes, enemyContext.enemy.transform.position, enemyHit);
                endNode          = PathFinder.getPlaneNode(enemyContext.planeNodes, playerPosition, playerHit);
                currentPlaneName = playerHit.collider.name;
                hearPath         = false;

                if (startNode != null && endNode != null)
                {
                    if (startNode.transform == endNode.transform)
                    {
                        if (playerPath.Count > 0)
                        {
                            playerPath.Clear();
                        }
                        targetPosition = playerPosition;
                    }
                    else
                    {
                        playerPath  = PathFinder.getPath(startNode, endNode);
                        offsetMod   = false;
                        pathCounter = 0;

                        //Makes sure it does not turn around to go to a node behind it.
                        NodeController firstNode = (NodeController)playerPath [pathCounter];
                        if (PathFinder.isNodeBehind(firstNode.transform.position, enemyContext.enemy.transform))
                        {
                            pathCounter++;
                        }
                    }
                }
                else
                {
                    targetPosition = playerPosition;
                }
            }

            //Part of the behaviour for moving through the path nodes and processing the queue.
            if (playerPath.Count > 0)
            {
                currentNode = (NodeController)playerPath [pathCounter];
                modPositionOffset();
                targetPosition = currPos;


                if (PathFinder.atDestination(enemyContext.enemy.transform, targetPosition))
                {
                    if (pathCounter == playerPath.Count - 1)
                    {
                        playerPath.Clear();
                        if (enemyContext.enemyInSight())
                        {
                            targetPosition = playerPosition;
                        }
                        else
                        {
                            enemyContext.playerHeard = false;
                            return(BehaviourStatus.FAILURE);
                        }
                    }
                    else
                    {
                        pathCounter++;
                        currentNode = (NodeController)playerPath [pathCounter];
                        offsetMod   = false;
                        modPositionOffset();
                        targetPosition = currPos;
                    }
                }
            }

            rotateAndMove(enemyContext, targetPosition);
        }

        return(BehaviourStatus.RUNNING);
    }