StatsUi.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. /// <summary>
  8. /// This module displays the total attributes of an InventoryHolder.
  9. /// Attach this script to a UI panel, assign the necessary UI elements,
  10. /// and call Initialize (InventoryHolder _equipHolder, Attribute[] _baseAttributes) to display the stats.
  11. /// _baseAttributes represents the player's attributes without any equipment.
  12. /// </summary>
  13. public class StatsUi : MonoBehaviour
  14. {
  15. #region Variables
  16. public GameObject StatPrefab;
  17. private InventoryHolder Holder;
  18. private Dictionary<string, Attribute> BaseDic = new Dictionary<string, Attribute>();
  19. private Dictionary<string, ListItem> StatList=new Dictionary<string, ListItem>();
  20. private Color StatColor;
  21. private bool Inited = false;
  22. #endregion
  23. #region MonoBehaviour
  24. private void Update()
  25. {
  26. if (Holder == null || !Inited) return;
  27. for (int i = 0; i < ItemManager.instance.itemAttributes.Count; i++)
  28. {
  29. if (ItemManager.instance.itemAttributes[i].visibleInStatsPanel && ItemManager.instance.itemAttributes[i].isNumber())
  30. {
  31. float _currentValue = GetValue(ItemManager.instance.itemAttributes[i].key);
  32. if (StatList[ItemManager.instance.itemAttributes[i].key].mValue != _currentValue)
  33. {
  34. if (StatList[ItemManager.instance.itemAttributes[i].key].mValue < _currentValue)
  35. {
  36. StartCoroutine(PopStat(StatList[ItemManager.instance.itemAttributes[i].key].mTexts[1],Color.green));
  37. }
  38. else
  39. {
  40. StartCoroutine(PopStat(StatList[ItemManager.instance.itemAttributes[i].key].mTexts[1], Color.red));
  41. }
  42. StatList[ItemManager.instance.itemAttributes[i].key].mValue = _currentValue;
  43. StatList[ItemManager.instance.itemAttributes[i].key].mTexts[1].text = _currentValue.ToString()+ ItemManager.instance.itemAttributes[i].suffixes;
  44. }
  45. }
  46. }
  47. }
  48. #endregion
  49. #region Internal Methods
  50. IEnumerator PopStat(Text _stat, Color _popColor)
  51. {
  52. float t = 0F;
  53. while (t<1F) {
  54. t += Time.deltaTime * 10F;
  55. _stat.color = Color.Lerp(StatColor, _popColor,t);
  56. _stat.transform.localScale = Vector3.one * (1F + t * 0.3F);
  57. yield return 1;
  58. }
  59. yield return new WaitForSeconds(0.5F);
  60. while (t > 0F)
  61. {
  62. t -= Time.deltaTime*3F;
  63. _stat.color = Color.Lerp(StatColor, _popColor, t);
  64. _stat.transform.localScale = Vector3.one * (1F + t * 0.3F);
  65. yield return 1;
  66. }
  67. _stat.color = StatColor;
  68. _stat.transform.localScale = Vector3.one;
  69. }
  70. private float GetValue(string _key)
  71. {
  72. float _base = 0F;
  73. if (BaseDic.ContainsKey(_key)) _base = BaseDic[_key].GetFloat();
  74. return _base + Holder.GetAttributeValue(_key);
  75. }
  76. #endregion
  77. /// <summary>
  78. /// Initialize to display the stats._baseAttributes represents the player's attributes without any equipment.
  79. /// </summary>
  80. /// <param name="_equipHolder"></param>
  81. /// <param name="_baseAttributes"></param>
  82. public void Init(InventoryHolder _equipHolder, Attribute[] _baseAttributes)
  83. {
  84. StatColor = StatPrefab.GetComponent<ListItem>().mTexts[1].color;
  85. Holder = _equipHolder;
  86. BaseDic.Clear();
  87. if (_baseAttributes != null)
  88. {
  89. foreach (Attribute att in _baseAttributes)
  90. {
  91. if (ItemManager.instance.GetAtttibute(att.key) != null)
  92. {
  93. if (!ItemManager.instance.GetAtttibute(att.key).stringValue)
  94. {
  95. float _value = att.GetFloat();
  96. Attribute _newAtt = ItemManager.instance.GetAtttibute(att.key).Copy();
  97. _newAtt.UpdateValue(_value);
  98. BaseDic.Add(att.key, _newAtt);
  99. }
  100. }
  101. }
  102. }
  103. for(int i=0;i<ItemManager.instance.itemAttributes.Count;i++) {
  104. if (ItemManager.instance.itemAttributes[i].visibleInStatsPanel && ItemManager.instance.itemAttributes[i].isNumber() && !StatList.ContainsKey(ItemManager.instance.itemAttributes[i].key))
  105. {
  106. GameObject _newStat = Instantiate(StatPrefab, StatPrefab.transform.parent);
  107. _newStat.transform.localScale = Vector3.one;
  108. _newStat.GetComponent<ListItem>().mTexts[0].text = ItemManager.instance.itemAttributes[i].name+" : ";
  109. _newStat.SetActive(true);
  110. _newStat.GetComponent<ListItem>().mValue = GetValue(ItemManager.instance.itemAttributes[i].key);
  111. _newStat.GetComponent<ListItem>().mTexts[1].text = GetValue(ItemManager.instance.itemAttributes[i].key).ToString()+ ItemManager.instance.itemAttributes[i].suffixes;
  112. StatList.Add(ItemManager.instance.itemAttributes[i].key,_newStat.GetComponent<ListItem>());
  113. }
  114. }
  115. Inited = true;
  116. }
  117. }
  118. }