ItemInfo.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Assets.HeroEditor4D.InventorySystem.Scripts.Data;
  4. using Assets.HeroEditor4D.InventorySystem.Scripts.Enums;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace Assets.HeroEditor4D.InventorySystem.Scripts.Elements
  8. {
  9. /// <summary>
  10. /// Represents item when it was selected. Displays icon, name, price and properties.
  11. /// </summary>
  12. public class ItemInfo : MonoBehaviour
  13. {
  14. public GameObject Selection;
  15. public GameObject Buttons;
  16. public Text Name;
  17. public Text Labels;
  18. public Text Values;
  19. public Text Price;
  20. public Image Icon;
  21. public Image Background;
  22. public Item Item { get; protected set; }
  23. protected static readonly List<PropertyId> Sorting = new List<PropertyId>
  24. {
  25. PropertyId.Damage,
  26. PropertyId.StaminaMax,
  27. PropertyId.Blocking,
  28. PropertyId.Resistance
  29. };
  30. public void OnEnable()
  31. {
  32. if (Item == null)
  33. {
  34. Reset();
  35. }
  36. }
  37. public void Reset()
  38. {
  39. Selection.SetActive(false);
  40. Buttons.SetActive(false);
  41. if (Name) Name.text = null;
  42. if (Labels) Labels.text = null;
  43. if (Values) Values.text = null;
  44. if (Price) Price.text = null;
  45. }
  46. public virtual void Initialize(Item item, int price, bool trader = false)
  47. {
  48. Item = item;
  49. if (item == null)
  50. {
  51. Reset();
  52. return;
  53. }
  54. Selection.SetActive(true);
  55. Buttons.SetActive(true);
  56. Name.text = item.Params.GetLocalizedName(Application.systemLanguage.ToString());
  57. Icon.sprite = ItemCollection.Active.FindIcon(item.Params.IconId);
  58. Background.sprite = ItemCollection.Active.GetBackground(item);
  59. UpdatePrice(item, price, trader);
  60. var main = new List<object> { item.Params.Type };
  61. if (item.Params.Class != ItemClass.Undefined) main.Add(item.Params.Class);
  62. foreach (var t in item.Params.Tags)
  63. {
  64. main.Add(t);
  65. }
  66. var dict = new Dictionary<string, object> { { "ItemInfo.Type", string.Join(" / ", main) } };
  67. if (item.Params.Level >= 0) dict.Add("ItemInfo.Level", item.Params.Level);
  68. if (item.Modifier != null)
  69. {
  70. dict.Add("ItemInfo.Modifier", $"{item.Modifier.Id} [{item.Modifier.Level}]");
  71. }
  72. var props = item.Params.Properties.ToList().OrderBy(i => { var index = Sorting.IndexOf(i.Id); return index == -1 ? 999 : index; }).ToList();
  73. foreach (var p in props)
  74. {
  75. switch (p.Id)
  76. {
  77. case PropertyId.Damage:
  78. dict.Add($"ItemInfo.{p.Id}", $"{p.Min}-{p.Max}");
  79. break;
  80. case PropertyId.CriticalChance:
  81. case PropertyId.CriticalDamage:
  82. dict.Add($"ItemInfo.{p.Id}", $"+{p.Value}%");
  83. break;
  84. case PropertyId.ChargeTimings:
  85. dict.Add($"ItemInfo.{p.Id}", $"{p.Value.Split(',').Length}");
  86. break;
  87. default:
  88. dict.Add($"ItemInfo.{p.Id}", $"{p.Value}");
  89. break;
  90. }
  91. }
  92. dict.Add("ItemInfo.Weight", $"{item.Params.Weight / 10f:0.##} kg");
  93. if (Price && item.Params.Type != ItemType.Currency)
  94. {
  95. dict.Add("ItemInfo.Price", $"{item.Params.Price} gold");
  96. }
  97. Labels.text = string.Join("\n", dict.Keys);
  98. Values.text = string.Join("\n", dict.Values);
  99. }
  100. public virtual void UpdatePrice(Item item, int price, bool trader)
  101. {
  102. if (!Price) return;
  103. if (item.Params.Type == ItemType.Currency)
  104. {
  105. Price.text = null;
  106. }
  107. else
  108. {
  109. Price.text = trader ? $"Buy price: {price}G" : $"Sell price: {price}G";
  110. }
  111. }
  112. }
  113. }