// Start is called before the first frame update
 void Awake()
 {
     state                = zombieState.idle;
     currAnim             = zombieState.idle;
     mainCamera           = Camera.main;
     agent                = GetComponent <NavMeshAgent>();
     voiceSource          = GetComponent <AudioSource>();
     agent.updatePosition = false;
 }
 // Update is called once per frame
 void Update()
 {
     if (data.isActive)
     {
         NPCUpdate();
     }
     else
     {
         state = zombieState.wakeup;
         Timer = 8;
     }
     voiceSource.volume = data.isActive ? 1 : 0;
 }
    void NPCUpdate()
    {
        //agent.velocity=animator.velocity;
        Vector3 worldDeltaPosition = agent.nextPosition - transform.position;

        if (worldDeltaPosition.magnitude > agent.radius && !agent.isOnOffMeshLink)
        {
            agent.nextPosition = transform.position + 0.9f * worldDeltaPosition;
        }

        seePlayer = false;
        if (debugIsTargeting && Time.frameCount % framerate == 0)
        {
            seePlayer = CanSee();
            if (!seePlayer)
            {
                distanceFromPlayer = Mathf.Infinity;
            }
            else
            {
                currTarget = GameController.instance.playercache.transform.position;
            }

            if (state != zombieState.chase && state != zombieState.attack && state != zombieState.attackCool && state != zombieState.wakeup && seePlayer)
            {
                state = zombieState.chase;
            }
        }

        if (Time.frameCount % framerate == 0 && !foundSound)
        {
            CheckSounds();
        }


        //Current State
        switch (state)
        {
        case zombieState.wakeup:
        case zombieState.attackCool:
        case zombieState.attack:
        {
            onPath = false;
            agent.ResetPath();
            break;
        }

        case zombieState.idle:
        {
            onPath = false;
            agent.ResetPath();
            if (foundSound)
            {
                if (currSoundLevel > 0)
                {
                    state  = zombieState.soundChase;
                    onPath = false;
                }
                foundSound = false;
            }
            break;
        }

        case zombieState.patrol:
        {
            onPath = false;
            if (!agent.hasPath || agent.pathStatus == NavMeshPathStatus.PathInvalid)
            {
                agent.SetDestination(getRandomPoint());
            }

            if (foundSound)
            {
                if (currSoundLevel > 0)
                {
                    state  = zombieState.soundChase;
                    onPath = false;
                }
                foundSound = false;
            }

            break;
        }

        case zombieState.soundChase:
        {
            if (!onPath)
            {
                agent.SetDestination(currTarget);
                onPath = true;
            }


            if (agent.hasPath && agent.remainingDistance < 0.5f)
            {
                state          = zombieState.idle;
                onPath         = false;
                currSoundLevel = 0;
                Timer          = Random.Range(5, 10);
            }
            break;
        }

        case zombieState.chase:
        {
            currSoundLevel = 0;
            if (Time.frameCount % framerate == 0)
            {
                if (seePlayer)
                {
                    agent.SetDestination(currTarget);
                }
            }

            if (agent.hasPath && agent.remainingDistance < 0.5f && distanceFromPlayer > 1f)
            {
                state = zombieState.idle;
                Timer = Random.Range(5, 10);
            }

            if (Physics.OverlapSphere(transform.position + transform.forward, 0.75f, playerMask).Length > 0)
            {
                //GameController.instance.playercache.Death(4);
                Debug.Log("Kill");
                state = zombieState.attack;
                Timer = timeToHit;
            }

            break;
        }
        }

        Timer -= Time.deltaTime;

        //Next State
        if (Timer < 0)
        {
            switch (state)
            {
            case zombieState.attack:
            {
                state = zombieState.attackCool;
                Timer = timeToIdle;
                voiceSource.PlayOneShot(Hit);

                if (GameController.instance.isAlive && Physics.OverlapSphere(transform.position + transform.forward, 0.8f, playerMask).Length > 0)
                {
                    GameController.instance.playercache.Health -= 25;
                    if (GameController.instance.playercache.Health <= 0)
                    {
                        GameController.instance.deathmsg = Localization.GetString("deathStrings", "death_049_1");
                    }
                }

                break;
            }

            case zombieState.attackCool:
            case zombieState.wakeup:
            case zombieState.idle:
            {
                state = zombieState.patrol;
                Timer = Random.Range(10, 15);

                break;
            }

            case zombieState.patrol:
            {
                state = zombieState.idle;
                Timer = Random.Range(5, 10);
                break;
            }
            }
        }
    }