Item.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using UnityEngine;
  2. [System.Serializable]
  3. public abstract class Item : ScriptableObject
  4. {
  5. [Header("Item Basic Info")]
  6. public string itemName;
  7. public string description;
  8. public Sprite icon;
  9. public GameObject model3D; // For 3D representation in-game
  10. public ItemType itemType;
  11. public ItemRarity rarity = ItemRarity.Common;
  12. [Header("Cost")]
  13. public int goldCost;
  14. public int silverCost;
  15. public int copperCost;
  16. [Header("Tags")]
  17. public string[] searchTags; // For search functionality
  18. public virtual bool CanAfford(Bank bank)
  19. {
  20. int totalCopperCost = goldCost * 100 + silverCost * 10 + copperCost;
  21. int totalCopperAvailable = bank.gold * 100 + bank.silver * 10 + bank.copper;
  22. return totalCopperAvailable >= totalCopperCost;
  23. }
  24. public virtual void Purchase(Bank bank)
  25. {
  26. if (!CanAfford(bank))
  27. {
  28. Debug.LogWarning($"Cannot afford {itemName}");
  29. return;
  30. }
  31. // Convert everything to copper for easier calculation
  32. int totalCopperCost = goldCost * 100 + silverCost * 10 + copperCost;
  33. int totalCopperAvailable = bank.gold * 100 + bank.silver * 10 + bank.copper;
  34. int remainingCopper = totalCopperAvailable - totalCopperCost;
  35. // Convert back to gold, silver, copper
  36. bank.gold = remainingCopper / 100;
  37. remainingCopper %= 100;
  38. bank.silver = remainingCopper / 10;
  39. bank.copper = remainingCopper % 10;
  40. Debug.Log($"Purchased {itemName} for {goldCost}g {silverCost}s {copperCost}c");
  41. }
  42. public virtual string GetCostString()
  43. {
  44. string cost = "";
  45. if (goldCost > 0) cost += $"{goldCost}g ";
  46. if (silverCost > 0) cost += $"{silverCost}s ";
  47. if (copperCost > 0) cost += $"{copperCost}c";
  48. return cost.Trim();
  49. }
  50. public virtual bool MatchesSearch(string searchTerm)
  51. {
  52. if (string.IsNullOrEmpty(searchTerm)) return true;
  53. searchTerm = searchTerm.ToLower();
  54. // Check name
  55. if (itemName.ToLower().Contains(searchTerm)) return true;
  56. // Check description
  57. if (description.ToLower().Contains(searchTerm)) return true;
  58. // Check tags
  59. if (searchTags != null)
  60. {
  61. foreach (string tag in searchTags)
  62. {
  63. if (tag.ToLower().Contains(searchTerm)) return true;
  64. }
  65. }
  66. return false;
  67. }
  68. }
  69. [System.Serializable]
  70. public enum ItemType
  71. {
  72. Weapon,
  73. Armor,
  74. Miscellaneous,
  75. Consumable,
  76. Tool,
  77. Accessory
  78. }
  79. [System.Serializable]
  80. public enum ItemRarity
  81. {
  82. Common,
  83. Uncommon,
  84. Rare,
  85. Epic,
  86. Legendary
  87. }