Advertisement
Guest User

Untitled

a guest
May 24th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyAI : MonoBehaviour
  5. {
  6. public enum FSMState { None, arvoreChase, Chase, Attack, Dead, AttackArvore }
  7.  
  8.  
  9. public float maxHealth = 100;
  10. public float health;
  11. public float speed = 5;
  12. Rigidbody2D EnemyRgdb;
  13. Vector3 facingDir;
  14. public Player opponent;
  15. Animator anim;
  16. public bool meeleAttacking;
  17. public float monsterPhysicalDmg = 30;
  18. public FSMState curState;
  19. public Vector3 facinDir;
  20. public Transform arvore;
  21.  
  22. void Start()
  23. {
  24.  
  25. monsterPhysicalDmg = 30;
  26. curState = FSMState.arvoreChase;
  27. anim = GetComponent<Animator>();
  28. EnemyRgdb = GetComponent<Rigidbody2D>();
  29. arvore = GameObject.FindGameObjectWithTag("arvore").transform;
  30. opponent = GameObject.FindGameObjectWithTag("opponent").GetComponent<Player>();
  31.  
  32.  
  33.  
  34. }
  35.  
  36. // Update is called once per frame
  37. void Update()
  38. {
  39.  
  40. //if (Vector3.Distance(transform.position, opponent.transform.position) < 0.5)
  41. // target = GameObject.FindGameObjectWithTag("opponent").transform;
  42. //else
  43. // target = GameObject.FindGameObjectWithTag("arvore").transform;
  44. switch (curState)
  45. {
  46. case FSMState.arvoreChase: UpdatearvoreChaseState(); break;
  47. case FSMState.Chase: UpdateChaseState(); break;
  48. case FSMState.Attack: UpdateAttackState(); break;
  49. case FSMState.Dead: UpdateDeadState(); break;
  50. case FSMState.AttackArvore: UpdateAttackArvoreState(); break;
  51. }
  52. if (health <= 0)
  53. {
  54. curState = FSMState.Dead;
  55. }
  56.  
  57.  
  58.  
  59.  
  60. }
  61.  
  62. private void UpdateAttackArvoreState()
  63. {
  64.  
  65. facingDir = (arvore.position - transform.position).normalized;
  66. if (playerRange() < 0.7)
  67. {
  68. curState = FSMState.Chase;
  69. }
  70. if (!meeleAttacking)
  71. {
  72. StartCoroutine(monsterMeeleAttackArvore());
  73.  
  74. }
  75. }
  76.  
  77. private void UpdatearvoreChaseState()
  78. {
  79. transform.position = Vector3.MoveTowards(transform.position, arvore.position, Time.deltaTime * speed);
  80. facingDir = (arvore.position - transform.position).normalized;
  81. if (playerRange() < 0.7)
  82. {
  83. curState = FSMState.Chase;
  84. }
  85. if (Vector3.Distance(transform.position, arvore.position) < 0.5)
  86. curState = FSMState.AttackArvore;
  87. }
  88.  
  89. private void UpdateDeadState()
  90. {
  91. Destroy(gameObject);
  92. }
  93.  
  94. private void UpdateAttackState()
  95. {
  96. facingDir = (arvore.position - transform.position).normalized;
  97. if (Vector3.Distance(transform.position, opponent.transform.position) > 0.8)
  98. curState = FSMState.Chase;
  99.  
  100. if (!meeleAttacking)
  101. StartCoroutine("monsterMeeleAttack");
  102. }
  103.  
  104. private void UpdateChaseState()
  105. {
  106. facingDir = (arvore.position - transform.position).normalized;
  107. transform.position = Vector3.MoveTowards(transform.position, opponent.transform.position, Time.deltaTime * speed);
  108. if (Vector3.Distance(transform.position, arvore.position) < 0.5)
  109. curState = FSMState.AttackArvore;
  110. if (Vector3.Distance(transform.position, opponent.transform.position) > 4)
  111. curState = FSMState.arvoreChase;
  112. if (playerRange() > 0.5)
  113. curState = FSMState.Attack;
  114.  
  115. }
  116.  
  117.  
  118. float playerRange(){
  119. return Vector2.Distance(transform.position, opponent.transform.position);
  120.  
  121. }
  122.  
  123.  
  124. public void takeDamage(float damage)
  125. {
  126. health -= damage;
  127.  
  128. }
  129. IEnumerator monsterMeeleAttack()
  130. {
  131. opponent.takeDmg(monsterPhysicalDmg);
  132. meeleAttacking = true;
  133. yield return new WaitForSeconds(3f);
  134. meeleAttacking = false;
  135.  
  136.  
  137. }
  138.  
  139. IEnumerator monsterMeeleAttackArvore()
  140. {
  141.  
  142. arvore.GetComponent<arvoreHP>().takeDmg(monsterPhysicalDmg);
  143. meeleAttacking = true;
  144. yield return new WaitForSeconds(3f);
  145. meeleAttacking = false;
  146.  
  147.  
  148. }
  149.  
  150.  
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement