Example #1
0
    void Update()
    {
        playerDetectionTimer += Time.deltaTime;
        //We could have the below check only occurwhen player is close enough to reduce operations
        if (playerDetectionTimer > playerDetectionWaitTime)                                             //Only do this check every chaseWaitTime seconds
        {
            playerDetectionTimer = 0;

            sight.CheckForPlayer();                                             //Have NPC check if player is seen

            if (sight.PlayerSeen())                                             //If the player is seen
            {
                search.Stop(true);                                              //Stop if currentely searching
                searchNewGem = false;                                           //Do not search for a gem any longer
                playerPos    = sight.GetLastPlayerSeenPos();                    //Get the position of the player

                if (!attack.OutOfAmmo())                                        //We only want to chase if we are NOT out of ammo
                {
                    chasing = true;                                             //We are chaing now

                    goalPos = new Vector3(playerPos.x, transform.position.y, playerPos.z);
                    search.SetGoalPos(goalPos);                     //New searching goal is the player's position
                    inASearch = true;                               //Allow to start searching!
                }
                else                                                //If we NPC is out of ammo then run away from it
                {
                    chasing      = false;                           //Make sure we are not chasing
                    searchNewGem = false;                           //Make sure we are not looking for Gems
                    fleeing      = true;                            //We need to flee

                    escapePos = search.GetFurthestPoint(playerPos); //Get the furtherst point based on straight line distance form where the player is
                    goalPos   = new Vector3(escapePos.x, transform.position.y, escapePos.z);
                    search.SetGoalPos(goalPos);                     //The new search goal is as far as the NPC thinks it can get from player
                    inASearch = true;                               //We make sure to have it true for ins a search
                }
            }
            else                                                             // I the player is not seen
            {
                if (!attack.OutOfAmmo() && gemTracker.GetAmountOfGems() > 0) //If the player is NOT seen but we are out of ammo, is a good time to find new gems
                {
                    searchNewGem = true;
                }
                chasing = false;                                        //But not a good time to chase (this check may not be needed)
            }
        }


        //only set a new goal if searchNewGem is true and if not already in the middle of search and if we not chasing the player
        if (!chasing && searchNewGem && health.GetHealth() < health.GetCriticalLevel() && !inASearch && gemTracker.GetAmountOfGems() > 0)
        {
            closestGem = gemTracker.FindClosestGem(playerPos);                          //hold closest gem position here
            goalPos    = new Vector3(closestGem.x, transform.position.y, closestGem.z); //constraing the y pos to this object y pos

            search.SetGoalPos(goalPos);                                                 //set the goal by calling SetGoalPos method in SearchingState script
            inASearch    = true;
            searchNewGem = false;                                                       //set searchNewGem to false until we find this gem

            if (debugMode)
            {
                Debug.Log("Closest Gem Pos: " + closestGem);                  //display value of closestGemPos in console
                Debug.Log("Goal Pos: " + goalPos);                            //display value of goalPos in console
            }
        }

        //keep searching until no longer in an active search
        if (inASearch)
        {
            if (chasing)
            {
                attack.AttackPlayer();                  //If we are chasing the player we want to attack it to while chasing
            }

            Searching();
        }
    }