MonsterController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using UnityEngine;
  2. using System.Collections;
  3. using DamageNumbersPro;
  4. using UnityEditor.Embree;
  5. public class MonsterController : MonoBehaviour
  6. {
  7. public MonsterData data;
  8. public MonsterFormationGroup formationGroup;
  9. private MonsterAnimatorController animator;
  10. private Vector3 currentTarget;
  11. public int currentHP;
  12. private float attackTimer = 0f;
  13. public float rotationSpeed = 40f;
  14. public DamageNumber damagePopupPrefab;
  15. public GameObject goldBagPrefab;
  16. public Transform goldSpawnPoint;
  17. public float goldDropYOffset = 0.15f;
  18. private void Start()
  19. {
  20. animator = GetComponent<MonsterAnimatorController>();
  21. currentHP = data.maxHP;
  22. if (data != null)
  23. {
  24. attackTimer = data.attackCooldown;
  25. }
  26. FindFirstObjectByType<CombatManager>()?.RegisterMonster(this);
  27. }
  28. public void ShowDamageNumber(float amount)
  29. {
  30. if (damagePopupPrefab == null) return;
  31. Vector3 popupPosition = transform.position + Vector3.up * 2f;
  32. DamageNumber newPopup = damagePopupPrefab.Spawn(popupPosition, amount);
  33. if (newPopup != null)
  34. {
  35. Transform cameraTransform = Camera.main?.transform;
  36. if (cameraTransform != null)
  37. {
  38. Vector3 cameraEuler = cameraTransform.eulerAngles;
  39. newPopup.transform.rotation = Quaternion.Euler(0f, cameraEuler.y, 0f);
  40. }
  41. }
  42. }
  43. public void MoveTo(Vector3 newPosition)
  44. {
  45. currentTarget = newPosition;
  46. StopAllCoroutines();
  47. StartCoroutine(MoveStep());
  48. }
  49. IEnumerator MoveStep()
  50. {
  51. animator?.PlayMove();
  52. while (Vector3.Distance(transform.position, currentTarget) > 0.05f)
  53. {
  54. transform.position = Vector3.MoveTowards(transform.position, currentTarget, data.moveSpeed * Time.deltaTime);
  55. yield return null;
  56. }
  57. animator?.StopMove();
  58. }
  59. public void FaceTarget(Vector3 target, bool instant = false)
  60. {
  61. Vector3 direction = (target - transform.position).normalized;
  62. direction.y = 0;
  63. if (direction != Vector3.zero)
  64. {
  65. Quaternion targetRotation = Quaternion.LookRotation(direction);
  66. if (instant)
  67. {
  68. transform.rotation = targetRotation;
  69. }
  70. else
  71. {
  72. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
  73. }
  74. }
  75. }
  76. public bool IsFacingTarget(Vector3 target, float angleThreshold = 30f)
  77. {
  78. Vector3 directionToTarget = (target - transform.position).normalized;
  79. directionToTarget.y = 0;
  80. if (directionToTarget == Vector3.zero) return true;
  81. float angle = Vector3.Angle(transform.forward, directionToTarget);
  82. return angle <= angleThreshold;
  83. }
  84. public void StopMove()
  85. {
  86. animator?.StopMove();
  87. }
  88. public void Attack()
  89. {
  90. animator?.PlayAttack();
  91. }
  92. public void UpdateAttackTimer(float deltaTime)
  93. {
  94. attackTimer += deltaTime;
  95. }
  96. public bool CanAttack()
  97. {
  98. return attackTimer >= data.attackCooldown;
  99. }
  100. public void ResetAttackTimer()
  101. {
  102. attackTimer = 0f;
  103. }
  104. public int GetDamage()
  105. {
  106. if (Random.value < data.missChance)
  107. {
  108. return 0;
  109. }
  110. float min = data.attackDamage / 2f;
  111. float max = data.attackDamage * 1.5f;
  112. int damage = Mathf.RoundToInt(Random.Range(min, max));
  113. return damage;
  114. }
  115. public void TakeDamage(int amount)
  116. {
  117. currentHP -= amount;
  118. ShowDamageNumber(amount);
  119. if (currentHP <= 0)
  120. {
  121. Die();
  122. }
  123. else
  124. {
  125. animator?.PlayHurt();
  126. }
  127. }
  128. public void Die()
  129. {
  130. animator?.PlayDeath();
  131. Collider collider = GetComponent<Collider>();
  132. if (collider != null)
  133. {
  134. collider.enabled = false;
  135. }
  136. GrantExperienceToParty();
  137. StartCoroutine(CoroutineDropGoldBag());
  138. formationGroup?.NotifyMonsterDeath(this);
  139. Destroy(gameObject, 2f);
  140. }
  141. private IEnumerator CoroutineDropGoldBag()
  142. {
  143. if (Random.value < 0.5f)
  144. {
  145. if (goldBagPrefab != null)
  146. {
  147. Vector3 dropPosition;
  148. if (goldSpawnPoint != null)
  149. {
  150. dropPosition = goldSpawnPoint.position + Vector3.up * goldDropYOffset;
  151. }
  152. else
  153. {
  154. dropPosition = transform.position + Vector3.up * goldDropYOffset;
  155. }
  156. GameObject goldBag = Instantiate(goldBagPrefab, dropPosition, Quaternion.Euler(-90f, 0f, 0f));
  157. }
  158. }
  159. yield return null;
  160. }
  161. private void GrantExperienceToParty()
  162. {
  163. TeamCohesionManager cohesionManager = FindFirstObjectByType<TeamCohesionManager>();
  164. if (cohesionManager == null || data == null) return;
  165. int expPerMember = Mathf.RoundToInt(data.exp);
  166. foreach (var character in cohesionManager.groupMembers)
  167. {
  168. character.experience += expPerMember;
  169. // Update the experience bar
  170. UIUpdater.Instance?.UpdateCharacterExperience(character);
  171. // Check for level up
  172. CheckLevelUp(character);
  173. }
  174. }
  175. private void CheckLevelUp(CharacterInGroup character)
  176. {
  177. while (character.experience >= 100)
  178. {
  179. character.experience -= 100;
  180. character.level++;
  181. int hpIncrease = 5 + character.constitution;
  182. int staminaIncrease = 5 + (character.constitution / 2);
  183. character.maxHP += hpIncrease;
  184. character.currentHP += hpIncrease;
  185. character.maxFatigue += staminaIncrease;
  186. character.currentFatigue += staminaIncrease;
  187. UIUpdater.Instance?.UpdateCharacterHP(character);
  188. UIUpdater.Instance?.UpdateCharacterFatigue(character);
  189. UIUpdater.Instance?.UpdateCharacterExperience(character);
  190. }
  191. }
  192. public void Setup(MonsterGroupManager mgr) { }
  193. }