WeaponItem.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. case "Fists":
  48. var fistsComponent = weaponObject.AddComponent<Fists>();
  49. weaponComponent = fistsComponent;
  50. break;
  51. // Add more weapon types here as you create them
  52. default:
  53. Debug.LogError($"Unknown weapon class: {weaponClassName}");
  54. DestroyImmediate(weaponObject);
  55. return null;
  56. }
  57. if (weaponComponent != null)
  58. {
  59. Debug.Log($"Created weapon instance: {itemName}");
  60. }
  61. return weaponComponent;
  62. }
  63. public override bool MatchesSearch(string searchTerm)
  64. {
  65. if (base.MatchesSearch(searchTerm)) return true;
  66. // Additional weapon-specific search terms
  67. if (string.IsNullOrEmpty(searchTerm)) return true;
  68. searchTerm = searchTerm.ToLower();
  69. // Check weapon type
  70. if (weaponType.ToString().ToLower().Contains(searchTerm)) return true;
  71. return false;
  72. }
  73. }
  74. [System.Serializable]
  75. public enum WeaponType
  76. {
  77. Fists,
  78. Sword,
  79. Bow,
  80. Crossbow,
  81. Dagger,
  82. Mace,
  83. Staff,
  84. Spear,
  85. Axe,
  86. Hammer
  87. }