Example #1
0
    /// <summary>
    /// Handles enemy behavior according to alert state and if the player is in view
    /// </summary>
    private void EnemyBehavior()
    {
        if (fow.FindTarget())
        {
            playerSpotted = true;
            lastSpotted   = fow.viewTarget;
            agent.SetDestination(lastSpotted.position);
            transform.LookAt(lastSpotted.position);
            agent.stoppingDistance = 5;
            arrived = false;
            patrol.OffPath();
            //StartCoroutine(GetPath()); //Debug
            switch (alertState)
            {
            case 0:
                if (Vector3.Distance(transform.position, lastSpotted.position) > engageDistance)
                {
                    //pause, then walk towards
                    if (cautionCoroutine == null)
                    {
                        Invoke(nameof(WhosThere), .4f);
                        cautionCoroutine = StartCoroutine(CautionCoroutine());
                    }
                }
                else
                {
                    Invoke(nameof(Intruder), .7f);
                    if (discoverCoroutine == null)
                    {
                        discoverCoroutine = StartCoroutine(DiscoverCoroutine(lastSpotted.position));
                    }
                }
                break;

            case 1:
                if (Vector3.Distance(transform.position, lastSpotted.position) > engageDistance)
                {
                    //continute to destination
                }
                else
                {
                    agent.isStopped = true;
                    if (discoverCoroutine == null)
                    {
                        Invoke(nameof(Intruder), .5f);
                        discoverCoroutine = StartCoroutine(DiscoverCoroutine(lastSpotted.position));
                    }
                }
                break;

            case 2:
                alertState = 3;
                NotifyOthers(lastSpotted.position);
                break;

            default:
                lastSpotted.SendMessage("Alerted");
                if (Vector3.Distance(transform.position, lastSpotted.position) < engageDistance)
                {
                    if (Physics.Raycast(transform.position + gunHeight, transform.forward, out RaycastHit hit, 100, targetLayersMask))
                    {
                        if (hit.transform.tag == "Player")
                        {
                            if (shootingCoroutine == null && discoverCoroutine == null)
                            {
                                shootingCoroutine = StartCoroutine(ShootingCoroutine());
                            }
                        }
                        else
                        {
                            StopShooting();
                        }
                    }
                    else
                    {
                        StopShooting();
                    }
                }
                else
                {
                    StopShooting();
                }
                //engage
                NotifyOthers(fow.viewTarget.position);
                break;
            }