Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class EnemyAI : MonoBehaviour
- {
- public enum FSMState { None, arvoreChase, Chase, Attack, Dead, AttackArvore }
- public float maxHealth = 100;
- public float health;
- public float speed = 5;
- Rigidbody2D EnemyRgdb;
- Vector3 facingDir;
- public Player opponent;
- Animator anim;
- public bool meeleAttacking;
- public float monsterPhysicalDmg = 30;
- public FSMState curState;
- public Vector3 facinDir;
- public Transform arvore;
- void Start()
- {
- monsterPhysicalDmg = 30;
- curState = FSMState.arvoreChase;
- anim = GetComponent<Animator>();
- EnemyRgdb = GetComponent<Rigidbody2D>();
- arvore = GameObject.FindGameObjectWithTag("arvore").transform;
- opponent = GameObject.FindGameObjectWithTag("opponent").GetComponent<Player>();
- }
- // Update is called once per frame
- void Update()
- {
- //if (Vector3.Distance(transform.position, opponent.transform.position) < 0.5)
- // target = GameObject.FindGameObjectWithTag("opponent").transform;
- //else
- // target = GameObject.FindGameObjectWithTag("arvore").transform;
- switch (curState)
- {
- case FSMState.arvoreChase: UpdatearvoreChaseState(); break;
- case FSMState.Chase: UpdateChaseState(); break;
- case FSMState.Attack: UpdateAttackState(); break;
- case FSMState.Dead: UpdateDeadState(); break;
- case FSMState.AttackArvore: UpdateAttackArvoreState(); break;
- }
- if (health <= 0)
- {
- curState = FSMState.Dead;
- }
- }
- private void UpdateAttackArvoreState()
- {
- facingDir = (arvore.position - transform.position).normalized;
- if (playerRange() < 0.7)
- {
- curState = FSMState.Chase;
- }
- if (!meeleAttacking)
- {
- StartCoroutine(monsterMeeleAttackArvore());
- }
- }
- private void UpdatearvoreChaseState()
- {
- transform.position = Vector3.MoveTowards(transform.position, arvore.position, Time.deltaTime * speed);
- facingDir = (arvore.position - transform.position).normalized;
- if (playerRange() < 0.7)
- {
- curState = FSMState.Chase;
- }
- if (Vector3.Distance(transform.position, arvore.position) < 0.5)
- curState = FSMState.AttackArvore;
- }
- private void UpdateDeadState()
- {
- Destroy(gameObject);
- }
- private void UpdateAttackState()
- {
- facingDir = (arvore.position - transform.position).normalized;
- if (Vector3.Distance(transform.position, opponent.transform.position) > 0.8)
- curState = FSMState.Chase;
- if (!meeleAttacking)
- StartCoroutine("monsterMeeleAttack");
- }
- private void UpdateChaseState()
- {
- facingDir = (arvore.position - transform.position).normalized;
- transform.position = Vector3.MoveTowards(transform.position, opponent.transform.position, Time.deltaTime * speed);
- if (Vector3.Distance(transform.position, arvore.position) < 0.5)
- curState = FSMState.AttackArvore;
- if (Vector3.Distance(transform.position, opponent.transform.position) > 4)
- curState = FSMState.arvoreChase;
- if (playerRange() > 0.5)
- curState = FSMState.Attack;
- }
- float playerRange(){
- return Vector2.Distance(transform.position, opponent.transform.position);
- }
- public void takeDamage(float damage)
- {
- health -= damage;
- }
- IEnumerator monsterMeeleAttack()
- {
- opponent.takeDmg(monsterPhysicalDmg);
- meeleAttacking = true;
- yield return new WaitForSeconds(3f);
- meeleAttacking = false;
- }
- IEnumerator monsterMeeleAttackArvore()
- {
- arvore.GetComponent<arvoreHP>().takeDmg(monsterPhysicalDmg);
- meeleAttacking = true;
- yield return new WaitForSeconds(3f);
- meeleAttacking = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement