WeaponItem.cs 2.8 KB

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