WeaponItem.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. [CreateAssetMenu(fileName = "New Weapon", menuName = "RPG/Items/Weapon")]
  3. [System.Serializable]
  4. public class WeaponItem : Item
  5. {
  6. [Header("Weapon Stats")]
  7. public int minDamage;
  8. public int maxDamage;
  9. public int range;
  10. public int weaponModifier;
  11. public float attackSpeed;
  12. public WeaponType weaponType;
  13. [Header("Weapon Behavior")]
  14. public string weaponClassName; // The class name to instantiate (e.g., "SimpleSword", "SimpleBow")
  15. public GameObject arrowPrefab; // For ranged weapons
  16. public WeaponItem()
  17. {
  18. itemType = ItemType.Weapon;
  19. }
  20. /// <summary>
  21. /// Creates an actual Weapon component instance from this WeaponItem data
  22. /// This is the updated method that works with the enhanced weapon classes
  23. /// </summary>
  24. /// <param name="parent">Parent transform to attach the weapon to</param>
  25. /// <returns>The instantiated Weapon component</returns>
  26. public Weapon CreateWeaponInstance(Transform parent)
  27. {
  28. GameObject weaponObject = new GameObject(itemName);
  29. weaponObject.transform.SetParent(parent, false);
  30. Weapon weaponComponent = null;
  31. // Create the appropriate weapon component based on weaponClassName
  32. switch (weaponClassName)
  33. {
  34. case "SimpleSword":
  35. var swordComponent = weaponObject.AddComponent<SimpleSword>();
  36. swordComponent.InitializeFromItem(this);
  37. weaponComponent = swordComponent;
  38. break;
  39. case "SimpleBow":
  40. var bowComponent = weaponObject.AddComponent<SimpleBow>();
  41. bowComponent.arrowPrefab = arrowPrefab;
  42. bowComponent.InitializeFromItem(this);
  43. weaponComponent = bowComponent;
  44. break;
  45. // Add more weapon types here as you create them
  46. default:
  47. Debug.LogError($"Unknown weapon class: {weaponClassName}");
  48. DestroyImmediate(weaponObject);
  49. return null;
  50. }
  51. if (weaponComponent != null)
  52. {
  53. Debug.Log($"Created weapon instance: {itemName}");
  54. }
  55. return weaponComponent;
  56. }
  57. public override bool MatchesSearch(string searchTerm)
  58. {
  59. if (base.MatchesSearch(searchTerm)) return true;
  60. // Additional weapon-specific search terms
  61. if (string.IsNullOrEmpty(searchTerm)) return true;
  62. searchTerm = searchTerm.ToLower();
  63. // Check weapon type
  64. if (weaponType.ToString().ToLower().Contains(searchTerm)) return true;
  65. return false;
  66. }
  67. }
  68. [System.Serializable]
  69. public enum WeaponType
  70. {
  71. Sword,
  72. Bow,
  73. Crossbow,
  74. Dagger,
  75. Mace,
  76. Staff,
  77. Spear,
  78. Axe,
  79. Hammer
  80. }