DrawScreenView.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using System;
  2. using System.Collections;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. [DisallowMultipleComponent]
  7. public class DrawScreenView : MonoBehaviour
  8. {
  9. public event Action CloseRequested;
  10. public event Action<string> MessageRequested;
  11. readonly Cell[] cells = new Cell[9];
  12. Button spinButton;
  13. TMP_Text spinLabel;
  14. TMP_Text subtitle;
  15. Coroutine spinRoutine;
  16. int pendingPrizeIndex = -1;
  17. bool wired;
  18. sealed class Cell
  19. {
  20. public Transform root;
  21. public Transform glow;
  22. public Transform highlight;
  23. public TMP_Text label;
  24. }
  25. void Awake()
  26. {
  27. CacheReferences();
  28. WireButtons();
  29. }
  30. void OnEnable()
  31. {
  32. SaveService.Changed += HandleSaveChanged;
  33. LocaleService.Changed += HandleSaveChanged;
  34. Refresh();
  35. }
  36. void OnDisable()
  37. {
  38. SaveService.Changed -= HandleSaveChanged;
  39. LocaleService.Changed -= HandleSaveChanged;
  40. if (spinRoutine != null)
  41. {
  42. StopCoroutine(spinRoutine);
  43. spinRoutine = null;
  44. ResolvePendingPrize();
  45. }
  46. ClearHighlights();
  47. }
  48. public void Show()
  49. {
  50. gameObject.SetActive(true);
  51. Refresh();
  52. }
  53. public void Hide()
  54. {
  55. gameObject.SetActive(false);
  56. }
  57. void HandleSaveChanged()
  58. {
  59. if (isActiveAndEnabled && spinRoutine == null)
  60. Refresh();
  61. }
  62. void CacheReferences()
  63. {
  64. subtitle = UIViewUtility.Text(transform, "IMG_BG/IMG_PauseModeBadge/TMP_PauseModeInfo");
  65. spinButton = UIViewUtility.Button(transform, "IMG_BG/BTN_Spin");
  66. spinLabel = UIViewUtility.Text(transform, "IMG_BG/BTN_Spin/TMP_SpinLabel");
  67. for (int i = 0; i < cells.Length; i++)
  68. {
  69. Transform root = UIViewUtility.Find(transform, "IMG_BG/GameObject/item" + (i + 1));
  70. if (root == null)
  71. continue;
  72. cells[i] = new Cell
  73. {
  74. root = root,
  75. glow = UIViewUtility.Find(root, "IMG_RewardHighlightGlow"),
  76. highlight = UIViewUtility.Find(root, "IMG_RewardCardActive"),
  77. label = UIViewUtility.Text(root, "TMP_RewardLabel")
  78. };
  79. }
  80. }
  81. void WireButtons()
  82. {
  83. if (wired)
  84. return;
  85. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BG/BTN_ClosePause"), () => CloseRequested?.Invoke());
  86. UIViewUtility.Bind(spinButton, BeginSpin);
  87. wired = true;
  88. }
  89. public void Refresh()
  90. {
  91. if (!Application.isPlaying)
  92. return;
  93. UILocaleScreens.ApplyDraw(transform);
  94. for (int i = 0; i < cells.Length; i++)
  95. {
  96. Cell cell = cells[i];
  97. if (cell == null || cell.label == null)
  98. continue;
  99. int prizeIndex = GameDefinitions.DrawGridPrizeMap[i];
  100. if (prizeIndex < 0 || prizeIndex >= GameDefinitions.DrawPrizes.Length)
  101. continue;
  102. DrawPrize prize = GameDefinitions.DrawPrizes[prizeIndex];
  103. cell.label.text = prize.coins
  104. ? UIViewUtility.FormatNumber(prize.amount)
  105. : GameLocale.PowerUpShortName(prize.itemKey);
  106. }
  107. bool free = SaveService.FreeDrawAvailable();
  108. int tickets = SaveService.Data.tickets;
  109. bool usable = free || tickets > 0;
  110. if (subtitle != null)
  111. subtitle.text = L.Get("draw.subtitle");
  112. if (spinLabel != null)
  113. {
  114. if (free)
  115. spinLabel.text = L.Get("draw.free_today");
  116. else if (tickets > 0)
  117. spinLabel.text = L.F("draw.ticket", tickets);
  118. else
  119. spinLabel.text = L.Get("draw.no_ticket");
  120. }
  121. if (spinButton != null)
  122. {
  123. spinButton.interactable = spinRoutine == null;
  124. ColorBlock colors = spinButton.colors;
  125. Color tint = usable ? Color.white : new Color(0.62f, 0.62f, 0.62f, 1f);
  126. colors.normalColor = tint;
  127. colors.highlightedColor = usable ? new Color(0.96f, 0.96f, 0.96f, 1f) : new Color(0.70f, 0.70f, 0.70f, 1f);
  128. colors.pressedColor = usable ? new Color(0.86f, 0.86f, 0.86f, 1f) : new Color(0.56f, 0.56f, 0.56f, 1f);
  129. colors.selectedColor = tint;
  130. colors.disabledColor = new Color(0.72f, 0.72f, 0.72f, 0.72f);
  131. spinButton.colors = colors;
  132. Color c = spinLabel.color;
  133. c.a = usable ? 1f : 128f / 255f;
  134. spinLabel.color = c;
  135. }
  136. }
  137. void BeginSpin()
  138. {
  139. if (spinRoutine != null)
  140. return;
  141. if (!SaveService.CanDraw())
  142. {
  143. GameAudio.PlaySfx(GameSfx.Error);
  144. MessageRequested?.Invoke(L.Get("toast.draw_tickets"));
  145. return;
  146. }
  147. spinRoutine = StartCoroutine(SpinRoutine());
  148. }
  149. IEnumerator SpinRoutine()
  150. {
  151. if (spinButton != null)
  152. spinButton.interactable = false;
  153. int prizeIndex = PickPrize();
  154. pendingPrizeIndex = prizeIndex;
  155. int targetGrid = -1;
  156. for (int i = 0; i < GameDefinitions.DrawGridPrizeMap.Length; i++)
  157. {
  158. if (GameDefinitions.DrawGridPrizeMap[i] == prizeIndex)
  159. {
  160. targetGrid = i;
  161. break;
  162. }
  163. }
  164. int ringPosition = 0;
  165. for (int i = 0; i < GameDefinitions.DrawRingGridOrder.Length; i++)
  166. {
  167. if (GameDefinitions.DrawRingGridOrder[i] == targetGrid)
  168. {
  169. ringPosition = i;
  170. break;
  171. }
  172. }
  173. int totalSteps = 16 + Mathf.Max(0, ringPosition);
  174. int currentGrid = -1;
  175. for (int step = 0; step <= totalSteps; step++)
  176. {
  177. int grid = GameDefinitions.DrawRingGridOrder[step % GameDefinitions.DrawRingGridOrder.Length];
  178. if (grid != currentGrid)
  179. {
  180. ClearHighlights();
  181. SetHighlight(grid, true);
  182. currentGrid = grid;
  183. GameAudio.PlaySfx(GameSfx.Spin);
  184. }
  185. int remaining = totalSteps - step;
  186. float delay = remaining <= 6
  187. ? 0.09f + (6 - remaining) * 0.055f
  188. : 0.055f;
  189. yield return new WaitForSecondsRealtime(delay);
  190. }
  191. ResolvePendingPrize();
  192. yield return new WaitForSecondsRealtime(0.65f);
  193. ClearHighlights();
  194. spinRoutine = null;
  195. Refresh();
  196. }
  197. void ResolvePendingPrize()
  198. {
  199. if (pendingPrizeIndex < 0)
  200. return;
  201. int index = pendingPrizeIndex;
  202. pendingPrizeIndex = -1;
  203. if (!SaveService.ApplyDrawPrize(index, out DrawPrize prize))
  204. return;
  205. GameAudio.PlaySfx(prize.coins ? GameSfx.Coin : GameSfx.Win);
  206. string message = prize.coins
  207. ? L.F("toast.draw_coins", UIViewUtility.FormatNumber(prize.amount))
  208. : L.F("toast.draw_item", GameLocale.PowerUpName(prize.itemKey));
  209. MessageRequested?.Invoke(message);
  210. }
  211. int PickPrize()
  212. {
  213. int total = 0;
  214. for (int i = 0; i < GameDefinitions.DrawPrizes.Length; i++)
  215. total += GameDefinitions.DrawPrizes[i].weight;
  216. float roll = UnityEngine.Random.value * total;
  217. for (int i = 0; i < GameDefinitions.DrawPrizes.Length; i++)
  218. {
  219. roll -= GameDefinitions.DrawPrizes[i].weight;
  220. if (roll <= 0f)
  221. return i;
  222. }
  223. return GameDefinitions.DrawPrizes.Length - 1;
  224. }
  225. void SetHighlight(int gridIndex, bool active)
  226. {
  227. if (gridIndex < 0 || gridIndex >= cells.Length)
  228. return;
  229. Cell cell = cells[gridIndex];
  230. if (cell == null)
  231. return;
  232. if (cell.glow != null)
  233. cell.glow.gameObject.SetActive(active);
  234. if (cell.highlight != null)
  235. cell.highlight.gameObject.SetActive(active);
  236. }
  237. void ClearHighlights()
  238. {
  239. for (int i = 0; i < cells.Length; i++)
  240. {
  241. Cell cell = cells[i];
  242. if (cell == null)
  243. continue;
  244. if (cell.glow != null)
  245. cell.glow.gameObject.SetActive(false);
  246. if (cell.highlight != null)
  247. cell.highlight.gameObject.SetActive(false);
  248. }
  249. }
  250. }