ShopUI.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using cfg;
  2. using Sirenix.OdinInspector;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. public class ShopUI : MonoBehaviour
  7. {
  8. [FoldoutGroup("组件")] public TextMeshProUGUI moneyText;
  9. [FoldoutGroup("组件")] public TreasuresSaleUI[] treasuresSaleUI;
  10. [FoldoutGroup("组件")] public Sprite[] tagsUI;
  11. [FoldoutGroup("组件")] public TextMeshProUGUI refreshPriceText;
  12. [FoldoutGroup("组件")] public RadarChart radarChart;
  13. [FoldoutGroup("组件")] public GameObject treasureInBag;
  14. [FoldoutGroup("组件")] public GameObject treasureDetails_inBag;
  15. [FoldoutGroup("组件")] public Transform bagContent;
  16. [FoldoutGroup("组件")] public Transform detailsParent;
  17. [FoldoutGroup("组件")] public Sprite[] qualityBox;
  18. [FoldoutGroup("组件")] public Color[] qualitysText;
  19. [FoldoutGroup("组件")] public TextMeshProUGUI[] polygonText;
  20. [FoldoutGroup("组件")] public List<Sprite> treasureBackgrounds;
  21. [LabelText("标签间隔")] public float interval;
  22. [LabelText("雷达图初始最大值")] public int polygonInitMax;
  23. [LabelText("雷达图最大值间隔")] public int polygonMaxInterval;
  24. public int refreshPrice;
  25. public int refreshNum;
  26. public void Init()
  27. {
  28. GameManager gameManager = GameManager.instance;
  29. refreshPrice = refreshNum + 1;
  30. moneyText.text = $"{gameManager.money}";
  31. refreshPriceText.text = $"-{refreshPrice}";
  32. //List<int> randomTreasure = new List<int>();
  33. //for (int i = 0; i < 5; i++)
  34. //{
  35. // if (treasuresSaleUI[i].islock)
  36. // {
  37. // randomTreasure.Add(treasuresSaleUI[i].treasure.id);
  38. // }
  39. //}
  40. for (int i = 0; i < 5; i++)
  41. {
  42. if (treasuresSaleUI[i].islock)
  43. {
  44. continue;
  45. }
  46. int targetQualityID = 0;
  47. float randomQuality = Random.Range(0f, 100f);
  48. float refreshChance = 0;
  49. for(int j = 0; j < 4; j++)
  50. {
  51. refreshChance += gameManager.trueRefreshChance[j];
  52. if(randomQuality <= refreshChance)
  53. {
  54. targetQualityID = j;
  55. break;
  56. }
  57. }
  58. int targetTag = 0;
  59. if(targetQualityID != 0 && gameManager.maxTreasuresTag!=-1)
  60. {
  61. int targetTagWeight = -1;
  62. float randomTagWeight = Random.Range(0f, 100f);
  63. refreshChance = 0;
  64. for (int j = 0; j < 3; j++)
  65. {
  66. refreshChance += gameManager.tagWeight[j];
  67. if (randomTagWeight <= refreshChance)
  68. {
  69. targetTagWeight = j;
  70. break;
  71. }
  72. }
  73. switch (targetTagWeight)
  74. {
  75. case 0:
  76. targetTag = gameManager.maxTreasuresTag + 1;
  77. break;
  78. case 1:
  79. List<int> targetTags = new List<int>();
  80. for(int j = 0; j < 8; j++)
  81. {
  82. if(gameManager.myTreasuresTag[j] > 0)
  83. {
  84. targetTags.Add(j + 1);
  85. }
  86. }
  87. targetTag = targetTags[Random.Range(0, targetTags.Count)];
  88. break;
  89. }
  90. }
  91. int randId = Random.Range(0, gameManager.treasuresList[targetQualityID][targetTag].Count);
  92. SingleTreasureConfig singleTreasureConfig = gameManager.treasuresList[targetQualityID][targetTag][randId];
  93. //while (randomTreasure.Exists(x => x == singleTreasureConfig.ID))
  94. //{
  95. // randId = Random.Range(0, gameManager.treasuresList[targetQualityID][targetTag].Count);
  96. // singleTreasureConfig = gameManager.treasuresList[targetQualityID][targetTag][randId];
  97. // string aa = "";
  98. // for (int a = 0; a < randomTreasure.Count; a++)
  99. // {
  100. // aa += $"{randomTreasure[a]}";
  101. // }
  102. // Debug.Log(aa);
  103. // Debug.Log($"{singleTreasureConfig.ID}");
  104. //}
  105. Sprite sprite = Resources.Load<Sprite>($"Textures/UI/Treasure/{singleTreasureConfig.SpriteName}");
  106. List<Sprite> tags = new List<Sprite>();
  107. for (int j = 0; j < singleTreasureConfig.Tag.Count; j++)
  108. {
  109. tags.Add(tagsUI[singleTreasureConfig.Tag[j] - 1]);
  110. }
  111. Treasure treasure = new Treasure(
  112. singleTreasureConfig.ID,
  113. singleTreasureConfig.Name,
  114. sprite,
  115. treasureBackgrounds[singleTreasureConfig.Quality],
  116. qualityBox[singleTreasureConfig.Quality],
  117. qualitysText[singleTreasureConfig.Quality],
  118. singleTreasureConfig.Tag,
  119. tags,
  120. singleTreasureConfig.Attribute,
  121. singleTreasureConfig.Price,
  122. singleTreasureConfig.Type,
  123. singleTreasureConfig.Data
  124. );
  125. treasuresSaleUI[i].treasure = treasure;
  126. treasuresSaleUI[i].Init();
  127. //randomTreasure.Add(singleTreasureConfig.ID);
  128. }
  129. DrawUIPolygon();
  130. DrawBag();
  131. gameObject.SetActive(true);
  132. }
  133. public void Refresh()
  134. {
  135. if(GameManager.instance.money - refreshPrice < 0)
  136. {
  137. return;
  138. }
  139. GameManager.instance.money -= refreshPrice;
  140. refreshNum += 1;
  141. Init();
  142. }
  143. public void DrawUIPolygon()
  144. {
  145. GameManager gameManager = GameManager.instance;
  146. int range = polygonInitMax;
  147. while (range <= gameManager.maxTreasuresTag)
  148. {
  149. range += polygonMaxInterval;
  150. }
  151. List<float> polygon = new List<float>();
  152. for(int i = 0; i < 8; i++)
  153. {
  154. float rate = gameManager.myTreasuresTag[i] * 1f / range;
  155. polygon.Add(rate);
  156. }
  157. radarChart.SetRatioList(polygon);
  158. }
  159. public void DrawBag()
  160. {
  161. List<Treasure> treasures = GameManager.instance.myTreasures;
  162. for(int i = 0; i < treasures.Count; i++)
  163. {
  164. TreasureInBag treasureInBag;
  165. TreasuresSaleUI treasuresSaleUI;
  166. if (i >= bagContent.childCount)
  167. {
  168. GameObject obj = Instantiate(this.treasureInBag,bagContent);
  169. GameObject objDetails = Instantiate(treasureDetails_inBag, detailsParent);
  170. objDetails.SetActive(false);
  171. treasureInBag = obj.GetComponent<TreasureInBag>();
  172. treasuresSaleUI = objDetails.GetComponent<TreasuresSaleUI>();
  173. }
  174. else
  175. {
  176. treasureInBag = bagContent.GetChild(i).GetComponent<TreasureInBag>();
  177. treasuresSaleUI = detailsParent.GetChild(i).GetComponent<TreasuresSaleUI>();
  178. }
  179. treasureInBag.treasuresSaleUI = treasuresSaleUI;
  180. treasureInBag.sprite = treasures[i].sprite;
  181. treasureInBag.box = treasures[i].qualityBox;
  182. treasureInBag.shopUI = this;
  183. treasuresSaleUI.owner = treasureInBag;
  184. treasuresSaleUI.treasure = treasures[i];
  185. treasuresSaleUI.transform.position = treasureInBag.transform.position;
  186. treasureInBag.Refresh();
  187. }
  188. for(int i = treasures.Count; i < bagContent.childCount; i++)
  189. {
  190. Destroy(bagContent.GetChild(i));
  191. }
  192. }
  193. }