GenericWeapon.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using UnityEngine;
  2. /// <summary>
  3. /// A single weapon class that can handle all weapon types through configuration
  4. /// </summary>
  5. public class GenericWeapon : Weapon
  6. {
  7. [Header("Weapon Configuration")]
  8. public WeaponBehaviorType behaviorType;
  9. public GameObject arrowPrefab; // For ranged weapons
  10. private WeaponItem weaponData;
  11. // Implement abstract properties from Weapon base class
  12. public override int MinDamage => weaponData?.minDamage ?? 1;
  13. public override int MaxDamage => weaponData?.maxDamage ?? 1;
  14. public override int Range => weaponData?.range ?? 1;
  15. public override int WeaponModifier
  16. {
  17. get => weaponData?.weaponModifier ?? 0;
  18. set => weaponModifier = value; // Set the base class field
  19. }
  20. public void InitializeFromItem(WeaponItem item)
  21. {
  22. weaponData = item;
  23. // Set basic weapon properties
  24. weaponName = item.itemName;
  25. description = item.description;
  26. attackSpeed = item.attackSpeed;
  27. // Determine behavior type from weapon data
  28. DetermineBehaviorType(item);
  29. // Initialize based on behavior
  30. InitializeBehavior(item);
  31. }
  32. private void DetermineBehaviorType(WeaponItem item)
  33. {
  34. // Determine behavior based on WeaponType enum or weapon name
  35. switch (item.weaponType)
  36. {
  37. case WeaponType.Fists:
  38. behaviorType = WeaponBehaviorType.Melee;
  39. break;
  40. case WeaponType.Bow:
  41. case WeaponType.Crossbow:
  42. behaviorType = WeaponBehaviorType.Ranged;
  43. if (item.arrowPrefab != null) arrowPrefab = item.arrowPrefab;
  44. break;
  45. case WeaponType.Sword:
  46. case WeaponType.Axe:
  47. case WeaponType.Mace:
  48. case WeaponType.Hammer:
  49. case WeaponType.Spear:
  50. behaviorType = WeaponBehaviorType.Melee;
  51. break;
  52. case WeaponType.Staff:
  53. behaviorType = WeaponBehaviorType.Magic;
  54. break;
  55. case WeaponType.Dagger:
  56. behaviorType = WeaponBehaviorType.FastMelee;
  57. break;
  58. default:
  59. behaviorType = WeaponBehaviorType.Melee;
  60. break;
  61. }
  62. }
  63. private void InitializeBehavior(WeaponItem item)
  64. {
  65. // Set weapon stats from item data
  66. // Assuming Weapon base class has these properties
  67. // You'll need to adjust based on your actual Weapon class structure
  68. // Example initialization:
  69. // damage = Random.Range(item.minDamage, item.maxDamage + 1);
  70. // range = item.range;
  71. // attackSpeed = item.attackSpeed;
  72. // Initialize specific behavior patterns
  73. switch (behaviorType)
  74. {
  75. case WeaponBehaviorType.Ranged:
  76. InitializeRangedWeapon(item);
  77. break;
  78. case WeaponBehaviorType.FastMelee:
  79. InitializeFastMeleeWeapon(item);
  80. break;
  81. case WeaponBehaviorType.Magic:
  82. InitializeMagicWeapon(item);
  83. break;
  84. default:
  85. InitializeMeleeWeapon(item);
  86. break;
  87. }
  88. }
  89. private void InitializeMeleeWeapon(WeaponItem item)
  90. {
  91. // Standard melee weapon setup
  92. }
  93. private void InitializeRangedWeapon(WeaponItem item)
  94. {
  95. // Ranged weapon setup
  96. if (arrowPrefab == null && item.arrowPrefab != null)
  97. {
  98. arrowPrefab = item.arrowPrefab;
  99. }
  100. }
  101. private void InitializeFastMeleeWeapon(WeaponItem item)
  102. {
  103. // Fast attack weapon setup (daggers, etc.)
  104. }
  105. private void InitializeMagicWeapon(WeaponItem item)
  106. {
  107. // Magic weapon setup (staffs, etc.)
  108. }
  109. // Override attack method to add behavior-specific logic
  110. public override void Attack(GameObject target)
  111. {
  112. // Call the base attack method first (handles all the standard logic)
  113. base.Attack(target);
  114. // Add any behavior-specific effects here if needed
  115. switch (behaviorType)
  116. {
  117. case WeaponBehaviorType.Ranged:
  118. // Could add special ranged effects here
  119. break;
  120. case WeaponBehaviorType.FastMelee:
  121. // Could add fast attack effects here
  122. break;
  123. case WeaponBehaviorType.Magic:
  124. // Could add magic effects here
  125. break;
  126. default:
  127. // Standard melee behavior
  128. break;
  129. }
  130. }
  131. }
  132. [System.Serializable]
  133. public enum WeaponBehaviorType
  134. {
  135. Melee,
  136. Ranged,
  137. FastMelee,
  138. Magic
  139. }