GenericWeapon.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. Debug.Log($"GenericWeapon initialized as {behaviorType} for {item.itemName}");
  32. }
  33. private void DetermineBehaviorType(WeaponItem item)
  34. {
  35. // Determine behavior based on WeaponType enum or weapon name
  36. switch (item.weaponType)
  37. {
  38. case WeaponType.Fists:
  39. behaviorType = WeaponBehaviorType.Melee;
  40. break;
  41. case WeaponType.Bow:
  42. case WeaponType.Crossbow:
  43. behaviorType = WeaponBehaviorType.Ranged;
  44. if (item.arrowPrefab != null) arrowPrefab = item.arrowPrefab;
  45. break;
  46. case WeaponType.Sword:
  47. case WeaponType.Axe:
  48. case WeaponType.Mace:
  49. case WeaponType.Hammer:
  50. case WeaponType.Spear:
  51. behaviorType = WeaponBehaviorType.Melee;
  52. break;
  53. case WeaponType.Staff:
  54. behaviorType = WeaponBehaviorType.Magic;
  55. break;
  56. case WeaponType.Dagger:
  57. behaviorType = WeaponBehaviorType.FastMelee;
  58. break;
  59. default:
  60. behaviorType = WeaponBehaviorType.Melee;
  61. break;
  62. }
  63. }
  64. private void InitializeBehavior(WeaponItem item)
  65. {
  66. // Set weapon stats from item data
  67. // Assuming Weapon base class has these properties
  68. // You'll need to adjust based on your actual Weapon class structure
  69. // Example initialization:
  70. // damage = Random.Range(item.minDamage, item.maxDamage + 1);
  71. // range = item.range;
  72. // attackSpeed = item.attackSpeed;
  73. // Initialize specific behavior patterns
  74. switch (behaviorType)
  75. {
  76. case WeaponBehaviorType.Ranged:
  77. InitializeRangedWeapon(item);
  78. break;
  79. case WeaponBehaviorType.FastMelee:
  80. InitializeFastMeleeWeapon(item);
  81. break;
  82. case WeaponBehaviorType.Magic:
  83. InitializeMagicWeapon(item);
  84. break;
  85. default:
  86. InitializeMeleeWeapon(item);
  87. break;
  88. }
  89. }
  90. private void InitializeMeleeWeapon(WeaponItem item)
  91. {
  92. // Standard melee weapon setup
  93. Debug.Log($"Initialized melee weapon: {item.itemName}");
  94. }
  95. private void InitializeRangedWeapon(WeaponItem item)
  96. {
  97. // Ranged weapon setup
  98. if (arrowPrefab == null && item.arrowPrefab != null)
  99. {
  100. arrowPrefab = item.arrowPrefab;
  101. }
  102. Debug.Log($"Initialized ranged weapon: {item.itemName} with arrows: {arrowPrefab != null}");
  103. }
  104. private void InitializeFastMeleeWeapon(WeaponItem item)
  105. {
  106. // Fast attack weapon setup (daggers, etc.)
  107. Debug.Log($"Initialized fast melee weapon: {item.itemName}");
  108. }
  109. private void InitializeMagicWeapon(WeaponItem item)
  110. {
  111. // Magic weapon setup (staffs, etc.)
  112. Debug.Log($"Initialized magic weapon: {item.itemName}");
  113. }
  114. // Override attack method to add behavior-specific logic
  115. public override void Attack(GameObject target)
  116. {
  117. // Call the base attack method first (handles all the standard logic)
  118. base.Attack(target);
  119. // Add any behavior-specific effects here if needed
  120. switch (behaviorType)
  121. {
  122. case WeaponBehaviorType.Ranged:
  123. // Could add special ranged effects here
  124. Debug.Log($"{weaponData.itemName} fires with ranged behavior!");
  125. break;
  126. case WeaponBehaviorType.FastMelee:
  127. // Could add fast attack effects here
  128. Debug.Log($"{weaponData.itemName} strikes with fast melee behavior!");
  129. break;
  130. case WeaponBehaviorType.Magic:
  131. // Could add magic effects here
  132. Debug.Log($"{weaponData.itemName} attacks with magical energy!");
  133. break;
  134. default:
  135. // Standard melee behavior
  136. Debug.Log($"{weaponData.itemName} performs melee attack!");
  137. break;
  138. }
  139. }
  140. }
  141. [System.Serializable]
  142. public enum WeaponBehaviorType
  143. {
  144. Melee,
  145. Ranged,
  146. FastMelee,
  147. Magic
  148. }