Example #1
0
    // Update is called once per frame
    void Update()
    {
        //Simple state machine
        switch (state)
        {
        case STATES.WAITING:
            currentWaitTime += Time.deltaTime;
            if (currentWaitTime > nextWaitTime)
            {
                goToNextPatrol();
            }
            break;

        //The guard is patrolling
        case STATES.PATROL:
            //if the guard is at the next patrol point, go to the next one, or if they're stuck
            if (move.isWithinThreshold() || isStuck)
            {
                state           = STATES.WAITING;
                nextWaitTime    = Random.Range(minWaitTime, MaxWaitTime);
                currentWaitTime = 0;
            }
            break;

        //The guard has seen a player partially
        case STATES.INVES:
            //The guard is at the last known location, return to patrol
            if (move.isWithinThreshold())
            {
                //TODO: MAKE GUARD ACTUALLY INVESTIGATE
                state           = STATES.WAITING;
                currentWaitTime = 0;
            }
            break;

        //The guard has caught a player
        case STATES.ALERT:

            //if the target has already been caught
            if (!target.isActivated)
            {
                //go back to normal
                GameManager.instance.evadeGuard();
                state  = STATES.PATROL;
                target = null;
                break;
            }

            //Go to position
            move.goToPosition(target.gameObject.transform.position);

            //if guard can't see the target
            if (!sight.canSeeTarget(target.gameObject))
            {
                //increment the timer
                currentAlertTime += Time.deltaTime;
                //if it's been too long since they last saw the player
                if (currentAlertTime > totalAlertTime)
                {
                    //the player has escaped
                    GameManager.instance.evadeGuard();
                    currentAlertTime = 0;
                    state            = STATES.PATROL;
                    target           = null;
                    break;
                }
            }
            else
            {
                currentAlertTime = 0;
            }

            //If the player has been caught
            if (move.hasCaughtPlayer(target.gameObject))
            {
                //Immobilize the character and update the game manager
                GameManager.instance.catchPlayer();
                GameManager.instance.evadeGuard();
                target.immobilize();
                state  = STATES.PATROL;
                target = null;
            }
            break;
        }
    }