Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class SimpleEnemyAI : MonoBehaviour {
  7. public enum EnemyType
  8. {
  9. Knight,
  10. Archer
  11. }
  12. public GameObject arrow;
  13. public EnemyType enemy;
  14. public Transform nextPoint;
  15. public List<GameObject> targets = new List<GameObject>();
  16. public float speed;
  17. public float attackRange;
  18. public Animator anim;
  19. public float damage;
  20. public float attackCooldown;
  21. public Image background;
  22. public Image bar, secondBar;
  23. public Text showHP;
  24. public float maxHP;
  25. public float hp;
  26. public float HP
  27. {
  28. get
  29. {
  30. return hp;
  31. }
  32. set
  33. {
  34. hp = value;
  35. hp = Mathf.Clamp(hp, 0, maxHP);
  36. tempHP = hp;
  37. StartCoroutine(ReduceHealth());
  38. if(hp == 0)
  39. {
  40. if(OnEnemyDead != null)
  41. OnEnemyDead(this.gameObject);
  42. Destroy(this.gameObject);
  43. }
  44. }
  45. }
  46.  
  47. private float tempHP;
  48. private bool isAttacking;
  49. private float timer;
  50. private SimpleDefenderAI tempDefenderAIScript;
  51.  
  52. public delegate void OnEnemyAttacked(GameObject enemy);
  53. public static event OnEnemyAttacked OnEnemyDead;
  54.  
  55. // Update is called once per frame
  56. void Update () {
  57. if(targets.Count == 0)
  58. {
  59. if(nextPoint!= null)
  60. {
  61. MoveToNextWaypoint();
  62. }
  63. }
  64. else
  65. {
  66. if(this.enemy == EnemyType.Archer)
  67. {
  68. Shoot();
  69. }
  70. else if(this.enemy == EnemyType.Knight)
  71. {
  72. Attack();
  73. }
  74. }
  75. }
  76.  
  77. void MoveToNextWaypoint()
  78. {
  79. transform.position = Vector2.MoveTowards(transform.position, nextPoint.position, speed * Time.deltaTime);
  80. }
  81.  
  82. void Attack()
  83. {
  84. if(Vector2.Distance(transform.position, targets[0].transform.position) >= attackRange)
  85. {
  86. transform.position = Vector2.MoveTowards(transform.position, targets[0].transform.position, speed * Time.deltaTime);
  87. isAttacking = false;
  88. }
  89. else
  90. {
  91. isAttacking = true;
  92. if(tempDefenderAIScript == null)
  93. tempDefenderAIScript = targets[0].GetComponent<SimpleDefenderAI>();
  94.  
  95. if(Time.time > timer)
  96. {
  97. anim.SetTrigger("attack");
  98. tempDefenderAIScript.HP -= damage;
  99. timer = Time.time + attackCooldown;
  100. }
  101. }
  102. }
  103.  
  104. void Shoot()
  105. {
  106. if(Vector2.Distance(transform.position, targets[0].transform.position) <= attackRange)
  107. {
  108. if(tempDefenderAIScript == null)
  109. tempDefenderAIScript = targets[0].GetComponent<SimpleDefenderAI>();
  110. if(Time.time > timer)
  111. {
  112. Vector3 dir = targets[0].transform.position - transform.position;
  113. dir = targets[0].transform.InverseTransformDirection(dir);
  114. float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  115. anim.SetTrigger("attack");
  116. GameObject shotArrow = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0,0,angle)));
  117. timer = Time.time + attackCooldown;
  118. }
  119. }
  120. }
  121.  
  122. void OnTriggerEnter2D(Collider2D newColl)
  123. {
  124. if(newColl.CompareTag("Defender") && newColl.isTrigger == false)
  125. {
  126. targets.Add(newColl.gameObject);
  127. SimpleDefenderAI.OnDefenderDead += RemoveTarget;// как отписаться от события, когда объект уничтожается?
  128. }
  129. }
  130.  
  131. void RemoveTarget(GameObject target)
  132. {
  133. targets.Remove(target);
  134. //SimpleDefenderAI.OnDefenderDead -= RemoveTarget;
  135. }
  136.  
  137. IEnumerator ReduceHealth()
  138. {
  139. while(showHP.text != tempHP.ToString())
  140. {
  141. showHP.text = ((int) Mathf.Lerp(float.Parse(showHP.text), tempHP, Time.deltaTime * 2)).ToString();
  142. bar.fillAmount = float.Parse(showHP.text)/maxHP;
  143. yield return null;
  144. }
  145. while(secondBar.fillAmount != bar.fillAmount)
  146. {
  147. secondBar.fillAmount -= 0.01f;
  148. secondBar.fillAmount = Mathf.Clamp(secondBar.fillAmount, bar.fillAmount, 1);
  149. yield return new WaitForSeconds(0.025f);
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement