WeaponRegistry.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. /// <summary>
  5. /// Registry system for looking up WeaponItem assets by name or weapon type
  6. /// Used to convert string-based weapon references to actual WeaponItem objects
  7. /// </summary>
  8. [CreateAssetMenu(fileName = "WeaponRegistry", menuName = "RPG/Systems/Weapon Registry")]
  9. public class WeaponRegistry : ScriptableObject
  10. {
  11. [Header("Available Weapons")]
  12. [SerializeField] private List<WeaponItem> availableWeapons = new List<WeaponItem>();
  13. private static WeaponRegistry _instance;
  14. public static WeaponRegistry Instance
  15. {
  16. get
  17. {
  18. if (_instance == null)
  19. {
  20. _instance = Resources.Load<WeaponRegistry>("WeaponRegistry");
  21. if (_instance == null)
  22. {
  23. Debug.LogWarning("WeaponRegistry not found in Resources folder. Creating default registry.");
  24. _instance = CreateInstance<WeaponRegistry>();
  25. }
  26. }
  27. return _instance;
  28. }
  29. }
  30. /// <summary>
  31. /// Find a WeaponItem by exact name match
  32. /// </summary>
  33. public WeaponItem GetWeaponByName(string weaponName)
  34. {
  35. if (string.IsNullOrEmpty(weaponName)) return null;
  36. return availableWeapons.FirstOrDefault(w =>
  37. w != null && string.Equals(w.itemName, weaponName, System.StringComparison.OrdinalIgnoreCase));
  38. }
  39. /// <summary>
  40. /// Find a WeaponItem by weapon type (finds first match of that type)
  41. /// </summary>
  42. public WeaponItem GetWeaponByType(WeaponType weaponType)
  43. {
  44. return availableWeapons.FirstOrDefault(w => w != null && w.weaponType == weaponType);
  45. }
  46. /// <summary>
  47. /// Find a WeaponItem by weapon type string (e.g., "Sword", "Bow")
  48. /// </summary>
  49. public WeaponItem GetWeaponByTypeString(string weaponTypeString)
  50. {
  51. if (string.IsNullOrEmpty(weaponTypeString)) return null;
  52. // Try to parse as WeaponType enum
  53. if (System.Enum.TryParse<WeaponType>(weaponTypeString, true, out WeaponType weaponType))
  54. {
  55. return GetWeaponByType(weaponType);
  56. }
  57. return null;
  58. }
  59. /// <summary>
  60. /// Get all weapons of a specific type
  61. /// </summary>
  62. public List<WeaponItem> GetWeaponsByType(WeaponType weaponType)
  63. {
  64. return availableWeapons.Where(w => w != null && w.weaponType == weaponType).ToList();
  65. }
  66. /// <summary>
  67. /// Get a random weapon of the specified type
  68. /// </summary>
  69. public WeaponItem GetRandomWeaponByType(WeaponType weaponType)
  70. {
  71. var weaponsOfType = GetWeaponsByType(weaponType);
  72. if (weaponsOfType.Count == 0) return null;
  73. int randomIndex = Random.Range(0, weaponsOfType.Count);
  74. return weaponsOfType[randomIndex];
  75. }
  76. /// <summary>
  77. /// Get the default/basic weapon for a weapon type
  78. /// Prioritizes weapons with "Simple" or "Basic" in the name
  79. /// </summary>
  80. public WeaponItem GetDefaultWeaponByType(WeaponType weaponType)
  81. {
  82. var weaponsOfType = GetWeaponsByType(weaponType);
  83. if (weaponsOfType.Count == 0) return null;
  84. // Try to find a "simple" or "basic" weapon first
  85. var simpleWeapon = weaponsOfType.FirstOrDefault(w =>
  86. w.itemName.ToLower().Contains("simple") ||
  87. w.itemName.ToLower().Contains("basic"));
  88. return simpleWeapon ?? weaponsOfType[0]; // Return first if no simple weapon found
  89. }
  90. /// <summary>
  91. /// Register a weapon in the registry (for runtime additions)
  92. /// </summary>
  93. public void RegisterWeapon(WeaponItem weapon)
  94. {
  95. if (weapon != null && !availableWeapons.Contains(weapon))
  96. {
  97. availableWeapons.Add(weapon);
  98. }
  99. }
  100. /// <summary>
  101. /// Get all available weapons
  102. /// </summary>
  103. public List<WeaponItem> GetAllWeapons()
  104. {
  105. return availableWeapons.Where(w => w != null).ToList();
  106. }
  107. /// <summary>
  108. /// Debug method to list all available weapons
  109. /// </summary>
  110. [ContextMenu("Debug List All Weapons")]
  111. public void DebugListAllWeapons()
  112. {
  113. Debug.Log($"=== WeaponRegistry: {availableWeapons.Count} weapons ===");
  114. foreach (var weapon in availableWeapons)
  115. {
  116. if (weapon != null)
  117. {
  118. Debug.Log($"• {weapon.itemName} ({weapon.weaponType}) - Damage: {weapon.minDamage}-{weapon.maxDamage}");
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// Initialize the registry with common weapons if empty
  124. /// </summary>
  125. [ContextMenu("Initialize Default Weapons")]
  126. public void InitializeDefaults()
  127. {
  128. // This would be called to auto-populate with default weapons
  129. // In practice, you'd manually assign weapons in the inspector
  130. Debug.Log("WeaponRegistry: Use the inspector to assign available weapons");
  131. }
  132. }