/Assets/Mine/Scripts/Enemie1.cs

https://bitbucket.org/catalina1411/the-dot-hunt · C# · 169 lines · 160 code · 8 blank · 1 comment · 21 complexity · 0e6df203c08cbe163a7835925a68ab98 MD5 · raw file

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using AkamaLife;
  6. public class Enemie1 : MonoBehaviour {
  7. enum HAbilitie { axe, sword, bow, magic}
  8. [SerializeField] HAbilitie abilitie;
  9. Animator anim;
  10. [SerializeField] Damager d_Sword, d_Axe;
  11. [SerializeField] Bow b_Bow;
  12. [SerializeField]
  13. float timeToAttackAgain;
  14. float speed = 2f, runSpeed = 5f, groundDistance = 0.5f;
  15. MineLifer lifer;
  16. Rigidbody rb;
  17. NavMeshAgent agent;
  18. bool running, attaking, grounded, jump, alive;
  19. Transform tarect;
  20. bool axe, bow, sword, magic;
  21. int currentAbilitie;
  22. void Start () {
  23. anim = GetComponent<Animator>();
  24. rb = GetComponent<Rigidbody>();
  25. agent = GetComponent<NavMeshAgent>();
  26. lifer = GetComponent<MineLifer>();
  27. agent.enabled = false;
  28. Habilitie();
  29. alive = true;
  30. }
  31. // Update is called once per frame
  32. void Update () {
  33. if (alive)
  34. {
  35. if (lifer.currentLife <= 0f)
  36. {
  37. anim.SetLayerWeight(1, 0);
  38. anim.SetTrigger("Death");
  39. CapsuleCollider[] collid = GetComponentsInChildren<CapsuleCollider>();
  40. for (int a = 1; a < collid.Length; a++)
  41. {
  42. collid[a].enabled = false;
  43. }
  44. StartCoroutine(WaitToDestroy());
  45. alive = false;
  46. }
  47. anim.SetBool("Ground", grounded);
  48. Grounder();
  49. CheckForAttack();
  50. if(agent.enabled == true)
  51. {
  52. agent.SetDestination(tarect.position);
  53. anim.SetFloat("FordWard", 1);
  54. }
  55. if ((lifer.currentLife > 0) && (!attaking) && (sword || axe || bow))
  56. anim.SetLayerWeight(1, 1);
  57. }
  58. }
  59. public void AnimateThis(string trigger)
  60. {
  61. anim.SetTrigger(trigger);
  62. }
  63. public void Agent(Transform tarject)
  64. {
  65. agent.enabled = true;
  66. tarect = tarject;
  67. }
  68. public void DisAgent()
  69. {
  70. tarect = null;
  71. agent.enabled = false;
  72. }
  73. private void Grounder()
  74. {
  75. RaycastHit hitInfo;
  76. if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, groundDistance))
  77. {
  78. Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * groundDistance), Color.blue);
  79. grounded = true;
  80. }
  81. else
  82. {
  83. Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * groundDistance), Color.red);
  84. grounded = false;
  85. attaking = false;
  86. }
  87. }
  88. void Habilitie()
  89. {
  90. switch (abilitie)
  91. {
  92. case HAbilitie.axe:
  93. axe = true;
  94. bow = false;
  95. sword = false;
  96. magic = false;
  97. break;
  98. case HAbilitie.bow:
  99. axe = false;
  100. bow = true;
  101. sword = false;
  102. magic = false;
  103. break;
  104. case HAbilitie.magic:
  105. axe = false;
  106. bow = false;
  107. sword = false;
  108. magic = true;
  109. break;
  110. case HAbilitie.sword:
  111. axe = false;
  112. bow = false;
  113. sword = true;
  114. magic = false;
  115. break;
  116. }
  117. anim.SetBool("Axe", axe);
  118. anim.SetBool("Bow", bow);
  119. anim.SetBool("Sword", sword);
  120. anim.SetBool("Magic", magic);
  121. }
  122. void CheckForAttack()
  123. {
  124. if(agent.enabled == true)
  125. {
  126. if (Vector3.Distance(transform.position,tarect.position) < (agent.stoppingDistance * 1.1f) && !attaking)
  127. {
  128. anim.SetLayerWeight(1, 0);
  129. anim.SetTrigger("Fire1");
  130. attaking = true;
  131. }
  132. }
  133. }
  134. public void AbleToAttack()
  135. {
  136. if (sword)
  137. d_Sword.GetComponent<BoxCollider>().enabled = false;
  138. if (axe)
  139. d_Axe.GetComponent<BoxCollider>().enabled = false;
  140. StartCoroutine(WaitToAttackAgain());
  141. }
  142. void ActiveWeapon()
  143. {
  144. if (sword)
  145. d_Sword.GetComponent<BoxCollider>().enabled = true;
  146. if (axe)
  147. d_Axe.GetComponent<BoxCollider>().enabled = true;
  148. if (bow)
  149. b_Bow.FireBow();
  150. }
  151. IEnumerator WaitToAttackAgain()
  152. {
  153. yield return new WaitForSeconds(timeToAttackAgain);
  154. attaking = false;
  155. }
  156. IEnumerator WaitToDestroy()
  157. {
  158. yield return new WaitForSeconds(2f);
  159. Destroy(gameObject);
  160. }
  161. }