Weapon.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using UnityEngine;
  2. using System.Collections;
  3. public abstract class Weapon : MonoBehaviour
  4. {
  5. public string weaponName;
  6. public string description;
  7. public int damage;
  8. public float attackSpeed;
  9. public GameObject weaponModel; // Assign the 3D model for the weapon
  10. // Reference to the character wielding this weapon
  11. public Character Wielder { get; private set; }
  12. // Track if an attack is currently in progress
  13. protected bool isAttacking = false;
  14. public float lastAttackTime { get; private set; }
  15. // Abstract properties that must be implemented by derived classes
  16. public abstract int MinDamage { get; }
  17. public abstract int MaxDamage { get; }
  18. public abstract int Range { get; }
  19. public abstract int WeaponModifier { get; set; }
  20. protected int weaponModifier = 0; // Base modifier for the weapon, can be overridden by subclasses
  21. protected int baseWeaponModifier = 0; // Base modifier for the weapon, used for calculations
  22. public float AttackStarted { get; private set; }
  23. public void SetWielder(Character character)
  24. {
  25. Wielder = character;
  26. if (Wielder == null)
  27. {
  28. Debug.LogWarning($"Weapon {weaponName} - SetWielder called with null character!");
  29. }
  30. }
  31. public virtual int GetDamage()
  32. {
  33. return UnityEngine.Random.Range(MinDamage, MaxDamage + 1);
  34. }
  35. public virtual bool IsInRange(Vector3 attackerPosition, Vector3 targetPosition)
  36. {
  37. float distance = Vector3.Distance(attackerPosition, targetPosition);
  38. return distance <= Range;
  39. }
  40. public bool IsAttacking()
  41. {
  42. return isAttacking;
  43. }
  44. // Method to perform attack - can be called by the character
  45. public virtual void Attack(GameObject target)
  46. {
  47. if (Wielder == null)
  48. {
  49. Debug.LogError($"Cannot attack - {weaponName} has no wielder!");
  50. return;
  51. }
  52. if (isAttacking)
  53. {
  54. Debug.Log($"attack started {AttackStarted} time now {Time.time}"); ;
  55. Debug.Log($"{Wielder.CharacterName} is already attacking!");
  56. return;
  57. }
  58. Character targetCharacter = target.GetComponent<Character>();
  59. if (targetCharacter == null)
  60. {
  61. Debug.LogError($"Target {target.name} is not a character!");
  62. return;
  63. }
  64. if (!targetCharacter.IsTargetable)
  65. {
  66. Debug.Log($"Cannot attack {targetCharacter.CharacterName} - target is not targetable!");
  67. return;
  68. }
  69. if (!IsInRange(Wielder.transform.position, target.transform.position))
  70. {
  71. Debug.Log($"{Wielder.CharacterName} is not in range to attack {targetCharacter.CharacterName} with {weaponName}");
  72. return;
  73. }
  74. AttackStarted = Time.time;
  75. // Start the attack coroutine
  76. StartCoroutine(PerformAttackSequence(targetCharacter));
  77. }
  78. protected virtual IEnumerator PerformAttackSequence(Character target)
  79. {
  80. isAttacking = true;
  81. lastAttackTime = Time.time;
  82. Debug.Log($"{Wielder.CharacterName} begins attacking {target.CharacterName} with {weaponName} (attack speed: {attackSpeed}s)");
  83. // Wait for the attack speed duration
  84. yield return new WaitForSeconds(attackSpeed);
  85. // Check if target is still in range after attack speed delay
  86. if (!IsInRange(Wielder.transform.position, target.transform.position))
  87. {
  88. Debug.Log($"{target.CharacterName} moved out of range before {Wielder.CharacterName}'s attack completed!");
  89. isAttacking = false;
  90. yield break;
  91. }
  92. // Check if target is still targetable (not dead)
  93. if (!target.IsTargetable)
  94. {
  95. Debug.Log($"{target.CharacterName} is no longer targetable!");
  96. isAttacking = false;
  97. yield break;
  98. }
  99. // Perform hit calculation (d20 + weapon modifier vs target armor class)
  100. int d20Roll = Random.Range(1, 21);
  101. int attackRoll = d20Roll + WeaponModifier;
  102. bool hits = attackRoll >= target.ArmorClass;
  103. int healthBefore = target.CurrentHealth;
  104. Debug.Log($"=== ATTACK DEBUG ===");
  105. Debug.Log($"Attacker: {Wielder.CharacterName} with {weaponName}");
  106. Debug.Log($"Target: {target.CharacterName}");
  107. Debug.Log($"D20 Roll: {d20Roll} + Weapon Modifier: {WeaponModifier} = Attack Roll: {attackRoll}");
  108. Debug.Log($"Target AC: {target.ArmorClass}");
  109. Debug.Log($"Target Health Before: {healthBefore}/{target.MaxHealth}");
  110. if (hits)
  111. {
  112. int damageDealt = GetDamage();
  113. Debug.Log($"HIT! Damage Roll: {damageDealt}");
  114. target.TakeDamage(damageDealt);
  115. Debug.Log($"Target Health After: {target.CurrentHealth}/{target.MaxHealth}");
  116. Debug.Log($"{Wielder.CharacterName} hits {target.CharacterName} with {weaponName} for {damageDealt} damage!");
  117. }
  118. else
  119. {
  120. Debug.Log($"MISS! {attackRoll} < {target.ArmorClass}");
  121. Debug.Log($"Target Health After: {healthBefore}/{target.MaxHealth} (no change)");
  122. Debug.Log($"{Wielder.CharacterName} misses {target.CharacterName} with {weaponName}!");
  123. }
  124. Debug.Log($"=== END ATTACK ===");
  125. isAttacking = false;
  126. }
  127. }