Daniel_Adi_Setiawan

Structure

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