UIManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using GameAudio = DevilLink2.Presentation.AudioManager;
  2. using System;
  3. using DevilLink2.Gameplay;
  4. using DevilLink2.Persistence;
  5. using DevilLink2.Presentation;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace DevilLink2.UI
  9. {
  10. public enum UIPage { Start, Lobby, Gameplay }
  11. [ExecuteAlways]
  12. [DisallowMultipleComponent]
  13. public sealed class UIManager : MonoBehaviour
  14. {
  15. private SavedPlayer player;
  16. private PlayerEconomy subscribedEconomy;
  17. private BoardDemoBootstrap bootstrap;
  18. private ModeSelectionController modes;
  19. private bool ownsPlayer, navigating;
  20. private readonly int[] lastInventory = new int[3];
  21. private readonly System.Collections.Generic.HashSet<Guid> heardCoins = new System.Collections.Generic.HashSet<Guid>();
  22. private GameFlowController shopFlow;
  23. private bool shopReturnsToSettings,helpReturnsToSettings;
  24. public HelpScreen Help {get;private set;}
  25. public void AttachHelp(HelpScreen help){Help=help;}
  26. public void OpenHelp(){if(!IsInitialized || CurrentPage!=UIPage.Lobby || Shop.IsVisible || Daily.IsVisible || Wheel.IsVisible || Profile.IsVisible || Settings.IsVisible || Help.IsVisible)return;transform.SetAsLastSibling();Help.transform.SetAsLastSibling();Help.Open();SetUnderlyingInput(false);}
  27. public void CloseHelp(){bool goBack=helpReturnsToSettings;helpReturnsToSettings=false;if(Help)Help.Close();SetUnderlyingInput(true);if(goBack)OpenSettings();}
  28. private void OpenHelpFromSettings(){if(!Settings.IsVisible)return;CloseSettings();OpenHelp();helpReturnsToSettings=Help.IsVisible;}
  29. public SettingsScreen Settings {get;private set;}
  30. public void AttachSettings(SettingsScreen settings){Settings=settings;}
  31. public void OpenSettings(){if(!IsInitialized || CurrentPage!=UIPage.Lobby || Shop.IsVisible || Daily.IsVisible || Wheel.IsVisible || Profile.IsVisible || Settings.IsVisible || (Help && Help.IsVisible))return;transform.SetAsLastSibling();Settings.transform.SetAsLastSibling();Settings.Open();SetUnderlyingInput(false);}
  32. public void CloseSettings(){if(Settings)Settings.Close();SetUnderlyingInput(true);}
  33. private void OpenShopFromSettings(){if(!Settings.IsVisible)return;CloseSettings();OpenShop();shopReturnsToSettings=Shop.IsVisible;}
  34. private void OnResetCompleted(){RefreshLobby();Shop.Refresh();Daily.Refresh();Wheel.Bind(null);Wheel.Bind(player);Profile.Refresh();}
  35. public ProfileScreen Profile {get;private set;}
  36. public void AttachProfile(ProfileScreen profile){Profile=profile;}
  37. public void OpenProfile(){if(!IsInitialized || CurrentPage!=UIPage.Lobby || Shop.IsVisible || Daily.IsVisible || Wheel.IsVisible || Profile.IsVisible || (Settings && Settings.IsVisible) || (Help && Help.IsVisible))return;transform.SetAsLastSibling();Profile.transform.SetAsLastSibling();Profile.Open();SetUnderlyingInput(false);}
  38. public void CloseProfile(){if(Profile)Profile.Close();SetUnderlyingInput(true);}
  39. private void OnProfileChanged(){RefreshLobby();if(Profile)Profile.Refresh();}
  40. public LuckyWheelScreen Wheel {get;private set;}
  41. public void AttachWheel(LuckyWheelScreen wheel) {Wheel=wheel;}
  42. public void OpenWheel()
  43. {
  44. if(!IsInitialized || CurrentPage!=UIPage.Lobby || Shop.IsVisible || Daily.IsVisible || Wheel.IsVisible || (Profile && Profile.IsVisible) || (Settings && Settings.IsVisible) || (Help && Help.IsVisible))return;
  45. transform.SetAsLastSibling(); Wheel.transform.SetAsLastSibling(); Wheel.Open(); SetUnderlyingInput(false);
  46. }
  47. public void CloseWheel() {if(Wheel)Wheel.Close(); SetUnderlyingInput(true);}
  48. public DailyCheckInScreen Daily { get; private set; }
  49. public void AttachDaily(DailyCheckInScreen daily) { Daily=daily; }
  50. public void OpenDaily()
  51. {
  52. if(!IsInitialized || CurrentPage!=UIPage.Lobby || Shop.IsVisible || Daily.IsVisible || (Wheel && Wheel.IsVisible) || (Profile && Profile.IsVisible) || (Settings && Settings.IsVisible) || (Help && Help.IsVisible))return;
  53. transform.SetAsLastSibling(); Daily.transform.SetAsLastSibling(); Daily.Open(); SetUnderlyingInput(false);
  54. }
  55. public void CloseDaily() { if(Daily)Daily.SetVisible(false); SetUnderlyingInput(true); }
  56. public ShopScreen Shop { get; private set; }
  57. public Button GameShopButton { get; private set; }
  58. public void AttachShop(ShopScreen shop)
  59. {
  60. Shop=shop; GameShopButton=Gameplay.transform.Find("ShopEntry")?.GetComponent<Button>();
  61. if(!GameShopButton)GameShopButton=UIBuilder.Button("ShopEntry", Gameplay.transform,"SHOP",152,888,126,36,UIBuilder.Gold,UIBuilder.Hex("A56600"),13);
  62. }
  63. public void OpenShop()
  64. {
  65. if(!IsInitialized || (CurrentPage!=UIPage.Lobby && CurrentPage!=UIPage.Gameplay))return;
  66. if(CurrentPage==UIPage.Gameplay && bootstrap.Flow.State!=GameFlowState.Running)return;
  67. if(Shop.IsVisible || (Daily && Daily.IsVisible) || (Wheel && Wheel.IsVisible) || (Profile && Profile.IsVisible) || (Settings && Settings.IsVisible) || (Help && Help.IsVisible))return;
  68. transform.SetAsLastSibling(); Shop.transform.SetAsLastSibling(); Shop.Open(); SetUnderlyingInput(false);
  69. shopFlow=CurrentPage==UIPage.Gameplay ? bootstrap.Flow : null;
  70. if(shopFlow!=null)shopFlow.StateChanged+=OnShopFlowChanged;
  71. }
  72. public void CloseShop()
  73. {
  74. bool returnToSettings=shopReturnsToSettings;shopReturnsToSettings=false;
  75. if(shopFlow!=null)shopFlow.StateChanged-=OnShopFlowChanged; shopFlow=null;
  76. if(Shop)Shop.SetVisible(false); SetUnderlyingInput(true);if(returnToSettings)OpenSettings();
  77. }
  78. private void OnShopFlowChanged(GameFlowState previous,GameFlowState next) { if(next!=GameFlowState.Running)CloseShop(); }
  79. private void SetUnderlyingInput(bool enabled)
  80. {
  81. if(Lobby)UITransition.SetInput(Lobby.gameObject,enabled);
  82. if(Gameplay)UITransition.SetInput(Gameplay.gameObject,enabled);
  83. }
  84. public StartScreen StartScreen { get; private set; }
  85. public LobbyScreen Lobby { get; private set; }
  86. public UIScreen Gameplay { get; private set; }
  87. public Text GameTitle { get; private set; }
  88. public Button LobbyButton { get; private set; }
  89. public Text ErrorLabel { get; private set; }
  90. public GameHUD Hud { get; private set; }
  91. public void AttachHUD(GameHUD hud) { Hud = hud; }
  92. public UIPage CurrentPage { get; private set; }
  93. public bool IsInitialized => player != null;
  94. public event Action<UIPage> PageChanged;
  95. public void Configure(StartScreen start, LobbyScreen lobby, UIScreen gameplay, Text title, Button lobbyButton, Text error)
  96. { StartScreen = start; Lobby = lobby; Gameplay = gameplay; GameTitle = title; LobbyButton = lobbyButton; ErrorLabel = error; }
  97. public void Initialize(SavedPlayer savedPlayer, BoardDemoBootstrap boardBootstrap, bool ownPlayer = false)
  98. {
  99. if (savedPlayer == null || !boardBootstrap) throw new ArgumentNullException(nameof(savedPlayer));
  100. if (ReferenceEquals(player, savedPlayer) && bootstrap == boardBootstrap) return;
  101. Cleanup(); player = savedPlayer; bootstrap = boardBootstrap; ownsPlayer = ownPlayer;
  102. bootstrap.ConfigureAutomaticStart(false);
  103. modes = new ModeSelectionController(bootstrap, player); modes.LobbyRequested += OnLobbyRequested;
  104. StartScreen.StartRequested += ShowLobby; Lobby.ModeRequested += OnModeRequested;
  105. Lobby.HelpRequested+=OpenHelp;Help.CloseRequested+=CloseHelp;Settings.HelpRequested+=OpenHelpFromSettings;
  106. Lobby.SettingsRequested+=OpenSettings;Settings.CloseRequested+=CloseSettings;Settings.ShopRequested+=OpenShopFromSettings;Settings.ResetCompleted+=OnResetCompleted;Settings.Bind(player);Settings.BindAudio(GameAudio.Instance);
  107. Lobby.ProfileRequested+=OpenProfile;Profile.CloseRequested+=CloseProfile;Profile.Bind(player);player.ProfileChanged+=OnProfileChanged;
  108. Lobby.WheelRequested+=OpenWheel; Wheel.CloseRequested+=CloseWheel; Wheel.Bind(player);
  109. Lobby.DailyRequested += OpenDaily; Daily.CloseRequested += CloseDaily; Daily.Bind(player);
  110. Lobby.ShopRequested += OpenShop; Shop.CloseRequested += CloseShop; GameShopButton.onClick.AddListener(OpenShop); Shop.Bind(player);
  111. LobbyButton.onClick.AddListener(ShowLobby); RefreshLobby(); SetPage(UIPage.Start);
  112. }
  113. public void ShowLobby()
  114. {
  115. if (!IsInitialized || navigating) return;
  116. if (CurrentPage == UIPage.Gameplay && bootstrap.Flow != null && bootstrap.Flow.State != GameFlowState.Abandoned)
  117. { modes.ReturnToLobby(); return; }
  118. OnLobbyRequested();
  119. }
  120. private void OnLobbyRequested() { if (!this || !StartScreen || !Lobby || !Gameplay || !IsInitialized) return; RefreshLobby(); SetPage(UIPage.Lobby); }
  121. private void OnModeRequested(GameMode mode) { SelectMode(mode); }
  122. public bool SelectMode(GameMode mode)
  123. {
  124. if (!IsInitialized || navigating || (Shop && Shop.IsVisible) || (Daily && Daily.IsVisible) || (Wheel && Wheel.IsVisible) || (Profile && Profile.IsVisible) || (Settings && Settings.IsVisible) || (Help && Help.IsVisible) || CurrentPage != UIPage.Lobby) return false;
  125. navigating = true;
  126. try
  127. {
  128. SetPage(UIPage.Gameplay); Canvas.ForceUpdateCanvases();
  129. var flow = modes.StartMode(mode); var level = flow.Runtime.Session.Definition;
  130. Hud?.Bind(flow, bootstrap.GetComponent<BoardInteractionController>());
  131. ErrorLabel.text = ""; return true;
  132. }
  133. catch (Exception error)
  134. {
  135. ErrorLabel.text = "Unable to open this mode."; SetPage(UIPage.Lobby);
  136. Debug.LogError("DevilLink2 UI mode startup failed: " + error, this); return false;
  137. }
  138. finally { navigating = false; }
  139. }
  140. public static string ModeName(GameMode mode)
  141. { return mode == GameMode.Classic ? "CLASSIC" : mode == GameMode.TimeAttack ? "TIME ATTACK" : "ZEN MODE"; }
  142. public void RefreshLobby()
  143. {
  144. if (!this || !Lobby || player == null) return;
  145. if (!ReferenceEquals(subscribedEconomy, player.Economy))
  146. {
  147. if (subscribedEconomy != null) subscribedEconomy.PersistentStateChanged -= OnEconomyChanged;
  148. subscribedEconomy = player.Economy; subscribedEconomy.PersistentStateChanged += OnEconomyChanged;
  149. for(int i=0;i<3;i++)lastInventory[i]=subscribedEconomy.Inventory.GetCount((ItemType)i);
  150. heardCoins.Clear();foreach(var entry in subscribedEconomy.Ledger.Entries)heardCoins.Add(entry.Id);
  151. }
  152. Lobby.Refresh(player);
  153. }
  154. private void OnEconomyChanged(PlayerChangeKind kind)
  155. {
  156. var currentIds=new System.Collections.Generic.HashSet<Guid>();
  157. foreach(var entry in subscribedEconomy.Ledger.Entries) {
  158. currentIds.Add(entry.Id);
  159. if(heardCoins.Add(entry.Id) && entry.Delta>0)GameAudio.Sound(AudioCue.Coin_Reward,"coin:"+entry.Id);
  160. }
  161. heardCoins.IntersectWith(currentIds);
  162. for(int i=0;i<3;i++) {
  163. int count=subscribedEconomy.Inventory.GetCount((ItemType)i);
  164. if(kind==PlayerChangeKind.ItemUsed && count<lastInventory[i])GameAudio.Sound(i==0?AudioCue.Item_Hint:i==1?AudioCue.Item_Shuffle:AudioCue.Item_Time);
  165. lastInventory[i]=count;
  166. }
  167. RefreshLobby(); if(Shop)Shop.Refresh(); if(Daily)Daily.Refresh(); if(Wheel)Wheel.Refresh();if(Profile)Profile.Refresh();
  168. }
  169. private void SetPage(UIPage page)
  170. {
  171. if (!(Shop.IsVisible || Daily.IsVisible || Wheel.IsVisible || Profile.IsVisible || Settings.IsVisible || Help.IsVisible) && CurrentPage == page && StartScreen.IsVisible == (page == UIPage.Start) && Lobby.IsVisible == (page == UIPage.Lobby) && Gameplay.IsVisible == (page == UIPage.Gameplay)) return;
  172. helpReturnsToSettings=false;CloseHelp();shopReturnsToSettings=false;CloseSettings();CloseProfile(); CloseWheel(); CloseDaily(); CloseShop(); CurrentPage = page;
  173. StartScreen.SetVisible(page == UIPage.Start); Lobby.SetVisible(page == UIPage.Lobby); Gameplay.SetVisible(page == UIPage.Gameplay);
  174. if(GameAudio.Instance && page!=UIPage.Gameplay)GameAudio.Instance.SetMusic(MusicTrack.Lobby);
  175. PageChanged?.Invoke(page);
  176. }
  177. private void Cleanup()
  178. {
  179. if(Lobby)Lobby.HelpRequested-=OpenHelp;if(Help)Help.CloseRequested-=CloseHelp;if(Settings)Settings.HelpRequested-=OpenHelpFromSettings;
  180. helpReturnsToSettings=false;CloseHelp();shopReturnsToSettings=false;CloseSettings();if(Lobby)Lobby.SettingsRequested-=OpenSettings;
  181. if(Settings){Settings.BindAudio(null);Settings.CloseRequested-=CloseSettings;Settings.ShopRequested-=OpenShopFromSettings;Settings.ResetCompleted-=OnResetCompleted;Settings.Bind(null);}
  182. CloseProfile();if(player!=null)player.ProfileChanged-=OnProfileChanged;
  183. if(Lobby)Lobby.ProfileRequested-=OpenProfile;if(Profile){Profile.CloseRequested-=CloseProfile;Profile.Bind(null); }
  184. CloseWheel(); if(Lobby)Lobby.WheelRequested-=OpenWheel;
  185. if(Wheel) {Wheel.CloseRequested-=CloseWheel;Wheel.Bind(null); }
  186. CloseDaily();
  187. if(Lobby)Lobby.DailyRequested-=OpenDaily;
  188. if(Daily) { Daily.CloseRequested-=CloseDaily; Daily.Bind(null); }
  189. CloseShop();
  190. if(Lobby)Lobby.ShopRequested -= OpenShop;
  191. if(Shop) { Shop.CloseRequested -= CloseShop; Shop.Bind(null); }
  192. if(GameShopButton)GameShopButton.onClick.RemoveListener(OpenShop);
  193. if (Hud) Hud.Unbind();
  194. if (StartScreen) StartScreen.StartRequested -= ShowLobby;
  195. if (Lobby) Lobby.ModeRequested -= OnModeRequested;
  196. if (LobbyButton) LobbyButton.onClick.RemoveListener(ShowLobby);
  197. if (subscribedEconomy != null) subscribedEconomy.PersistentStateChanged -= OnEconomyChanged;
  198. subscribedEconomy = null;
  199. if (modes != null) { modes.LobbyRequested -= OnLobbyRequested; modes.Dispose(); modes = null; }
  200. if (bootstrap && bootstrap.Flow != null) bootstrap.Flow.ReturnToLobby();
  201. if (Gameplay) Gameplay.SetVisible(false);
  202. if (ownsPlayer) player?.Dispose();
  203. player = null; ownsPlayer = false;
  204. }
  205. private void OnDisable() { Cleanup(); }
  206. private void OnDestroy() { Cleanup(); }
  207. }
  208. }