using UnityEngine; [CreateAssetMenu(fileName = "New Weapon", menuName = "RPG/Items/Weapon")] [System.Serializable] public class WeaponItem : Item { [Header("Weapon Stats")] public int minDamage; public int maxDamage; public int range; public int weaponModifier; public float attackSpeed; public WeaponType weaponType; [Header("Weapon Behavior")] public string weaponClassName; // The class name to instantiate (e.g., "SimpleSword", "SimpleBow") [Header("Weapon Prefabs")] public GameObject weaponPrefab; // 3D model prefab for the weapon public GameObject arrowPrefab; // For ranged weapons public WeaponItem() { itemType = ItemType.Weapon; } /// /// Creates an actual Weapon component instance from this WeaponItem data /// This is the updated method that works with the enhanced weapon classes /// /// Parent transform to attach the weapon to /// The instantiated Weapon component public Weapon CreateWeaponInstance(Transform parent) { GameObject weaponObject = new GameObject(itemName); weaponObject.transform.SetParent(parent, false); Weapon weaponComponent = null; // Create the appropriate weapon component based on weaponClassName switch (weaponClassName) { case "SimpleSword": var swordComponent = weaponObject.AddComponent(); swordComponent.InitializeFromItem(this); weaponComponent = swordComponent; break; case "SimpleBow": var bowComponent = weaponObject.AddComponent(); bowComponent.arrowPrefab = arrowPrefab; bowComponent.InitializeFromItem(this); weaponComponent = bowComponent; break; case "Fists": var fistsComponent = weaponObject.AddComponent(); weaponComponent = fistsComponent; break; // Add more weapon types here as you create them default: Debug.LogError($"Unknown weapon class: {weaponClassName}"); DestroyImmediate(weaponObject); return null; } if (weaponComponent != null) { Debug.Log($"Created weapon instance: {itemName}"); } return weaponComponent; } public override bool MatchesSearch(string searchTerm) { if (base.MatchesSearch(searchTerm)) return true; // Additional weapon-specific search terms if (string.IsNullOrEmpty(searchTerm)) return true; searchTerm = searchTerm.ToLower(); // Check weapon type if (weaponType.ToString().ToLower().Contains(searchTerm)) return true; return false; } } [System.Serializable] public enum WeaponType { Fists, Sword, Bow, Crossbow, Dagger, Mace, Staff, Spear, Axe, Hammer }