Advertisement
Daniel_Adi_Setiawan

Unit

Feb 23rd, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Unit : MonoBehaviour, IDamageable
  7. {
  8. [SerializeField]
  9. private Actor3D agent;
  10. [SerializeField]
  11. private Actor2D unitSprite;
  12. [SerializeField]
  13. private GameObject target;
  14. [SerializeField]
  15. private BaseStats stats;
  16. [SerializeField]
  17. private List<GameObject> hitTargets;
  18.  
  19. public BaseStats Stats
  20. {
  21. get { return stats; }
  22. }
  23. public Actor3D Agent
  24. {
  25. get { return agent; }
  26. }
  27. public Actor2D UnitSprite
  28. {
  29. get { return unitSprite; }
  30. }
  31. public GameObject Target
  32. {
  33. get { return target; }
  34. set { target = value; }
  35. }
  36. public List<GameObject> HitTargets
  37. {
  38. get { return hitTargets; }
  39. }
  40. private void Update()
  41. {
  42. if(stats.CurrHealth > 0)
  43. {
  44. agent.Agent.speed = stats.MoveSpeed;
  45. stats.UpdateStats();
  46. Attack();
  47. if (target != null)
  48. agent.Agent.SetDestination(target.transform.position);
  49. }
  50.  
  51.  
  52. }
  53.  
  54. void Attack()
  55. {
  56. if(target != null)
  57. {
  58. if(stats.CurrAttackDelay >= stats.AttackDelay)
  59. {
  60. Component damageable = target.GetComponent(typeof(IDamageable));
  61.  
  62. if(damageable)
  63. {
  64. if(hitTargets.Contains(target))
  65. {
  66. if(GameFunctions.CanAttack(gameObject.tag,target.tag,damageable,stats))
  67. {
  68. GameFunctions.Attack(damageable, stats.BaseDamage);
  69. stats.CurrAttackDelay = 0;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76.  
  77. public void OnTriggerEnter(Collider other)
  78. {
  79. if(!other.transform.parent.parent.CompareTag(gameObject.tag))
  80. {
  81. Component damageable = other.transform.parent.parent.gameObject.GetComponent(typeof(IDamageable));
  82. if(damageable)
  83. {
  84. if(!hitTargets.Contains(damageable.gameObject))
  85. {
  86. hitTargets.Add(damageable.gameObject);
  87. }
  88. }
  89. }
  90. }
  91.  
  92. public void OnTriggerStay(Collider other)
  93. {
  94. if(!other.gameObject.CompareTag(gameObject.tag))
  95. {
  96. if(hitTargets.Count > 0)
  97. {
  98. GameObject go = GameFunctions.GetNearestTarget(hitTargets, stats.DetectionObject, gameObject.tag, stats.Range);
  99.  
  100. if(go != null)
  101. {
  102. target = go;
  103. }
  104. }
  105. }
  106. }
  107.  
  108. void IDamageable.TakeDamage(float amount)
  109. {
  110. stats.CurrHealth -= amount;
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement