//patruyaje
 IEnumerator patrol(float time)
 {
     isPatrolling = true;
     while (EnemyState == botStates.waiting)
     {
         if (gameManager.instance.IsPaused)
         {
             isPatrolling = false;
             yield break;
         }
         if (Axis == Vector2.zero)
         {
             Axis = (new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f))).normalized;
             if (!Aud.isPlaying)
             {
                 Aud.Play();
             }
         }
         else
         {
             Aud.Stop();
             Axis = Vector2.zero;
         }
         Anim.SetFloat("speed", Axis.magnitude);
         yield return(new WaitForSeconds(time));
     }
     isPatrolling = false;
 }
예제 #2
0
    private void Update()
    {
        //Controles dentro del juego
        if (!gameManager.instance.IsPaused)
        {
            changeAxis();
            if (Rigid.velocity.magnitude > 0f)
            {
                if (!Aud.isPlaying)
                {
                    Aud.Play();
                }
            }
            else
            {
                Aud.Stop();
            }

            //interactua con objeto
            if (Input.GetButtonDown("Fire1") && interactionTarget != null)
            {
                gameManager.instance.IsPaused = true;
                gameManager.instance.InScene  = true;
                interactionTarget.nextDialogue();
            }
            else if (Input.GetButtonDown("Fire1") && !isAttacking)
            {
                StartCoroutine(attack(1));
            }

            if (Input.GetButtonDown("Cancel"))
            {
                if (!gameManager.instance.InScene)
                {
                    pauseGame();
                }
            }
        }
        //controles de escena
        else if (gameManager.instance.InScene)
        {
            if (Input.GetButtonDown("Fire1"))
            {
                interactionTarget.nextDialogue();
            }
            if (Input.GetButtonDown("Cancel"))
            {
                interactionTarget.endConversation();
            }
        }
        //Controles de pausa
        else if (Input.GetButtonDown("Cancel"))
        {
            if (!gameManager.instance.InScene)
            {
                pauseGame();
            }
        }

        if (Health <= 0)
        {
            SceneManager.LoadScene(0);
        }
    }
    private void Update()
    {
        //pausa
        if (gameManager.instance.IsPaused)
        {
            return;
        }

        //IA
        switch (EnemyState)
        {
        case botStates.waiting:
            //inicia patrullaje en caso de no haberse activado ya
            if (!isPatrolling)
            {
                StartCoroutine(patrol(patrolTime));
            }
            //revisa constamente en busqueda del objetivo a atacar
            if (Vector3.Distance(gameManager.instance.player.position, transform.position) <= visionDistance)
            {
                if (Vector3.Angle(gameManager.instance.player.position, transform.position) <= visionRange)
                {
                    EnemyState = botStates.chase;
                }
            }
            break;

        case botStates.chase:
            if (!Aud.isPlaying)
            {
                Aud.Play();
            }
            if (Axis != Vector2.zero)
            {
                Axis = Vector2.zero;
            }
            if (Vector3.Distance(gameManager.instance.player.position, transform.position) <= chaseDistance)
            {
                if (Vector3.Distance(gameManager.instance.player.position, transform.position) <= attackDistance)
                {
                    Anim.SetFloat("speed", 0f);
                    EnemyState = botStates.attack;
                }
                else
                {
                    Nav.SetDestination(gameManager.instance.player.position);
                    Anim.SetFloat("speed", 1f);
                }
            }
            else
            {
                EnemyState = botStates.waiting;
            }
            break;

        case botStates.attack:
            Aud.Stop();
            if (!isAttacking)
            {
                transform.LookAt(gameManager.instance.player);
                StartCoroutine(attack(attackTime));
            }
            break;

        case botStates.dying:
            //Deja de realizar acciones
            break;

        default:
            Debug.Log("Error en update del objeto" + transform.name);
            break;
        }
    }