| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using GameAudio = DevilLink2.Presentation.AudioManager;
- using System;
- using DevilLink2.Gameplay;
- using DevilLink2.Persistence;
- using DevilLink2.Presentation;
- using UnityEngine;
- using UnityEngine.UI;
- namespace DevilLink2.UI
- {
- public enum UIPage { Start, Lobby, Gameplay }
- [ExecuteAlways]
- [DisallowMultipleComponent]
- public sealed class UIManager : MonoBehaviour
- {
- private SavedPlayer player;
- private PlayerEconomy subscribedEconomy;
- private BoardDemoBootstrap bootstrap;
- private ModeSelectionController modes;
- private bool ownsPlayer, navigating;
- private readonly int[] lastInventory = new int[3];
- private readonly System.Collections.Generic.HashSet<Guid> heardCoins = new System.Collections.Generic.HashSet<Guid>();
- private GameFlowController shopFlow;
- private bool shopReturnsToSettings,helpReturnsToSettings;
- public HelpScreen Help {get;private set;}
- public void AttachHelp(HelpScreen help){Help=help;}
- 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);}
- public void CloseHelp(){bool goBack=helpReturnsToSettings;helpReturnsToSettings=false;if(Help)Help.Close();SetUnderlyingInput(true);if(goBack)OpenSettings();}
- private void OpenHelpFromSettings(){if(!Settings.IsVisible)return;CloseSettings();OpenHelp();helpReturnsToSettings=Help.IsVisible;}
- public SettingsScreen Settings {get;private set;}
- public void AttachSettings(SettingsScreen settings){Settings=settings;}
- 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);}
- public void CloseSettings(){if(Settings)Settings.Close();SetUnderlyingInput(true);}
- private void OpenShopFromSettings(){if(!Settings.IsVisible)return;CloseSettings();OpenShop();shopReturnsToSettings=Shop.IsVisible;}
- private void OnResetCompleted(){RefreshLobby();Shop.Refresh();Daily.Refresh();Wheel.Bind(null);Wheel.Bind(player);Profile.Refresh();}
- public ProfileScreen Profile {get;private set;}
- public void AttachProfile(ProfileScreen profile){Profile=profile;}
- 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);}
- public void CloseProfile(){if(Profile)Profile.Close();SetUnderlyingInput(true);}
- private void OnProfileChanged(){RefreshLobby();if(Profile)Profile.Refresh();}
- public LuckyWheelScreen Wheel {get;private set;}
- public void AttachWheel(LuckyWheelScreen wheel) {Wheel=wheel;}
- public void OpenWheel()
- {
- if(!IsInitialized || CurrentPage!=UIPage.Lobby || Shop.IsVisible || Daily.IsVisible || Wheel.IsVisible || (Profile && Profile.IsVisible) || (Settings && Settings.IsVisible) || (Help && Help.IsVisible))return;
- transform.SetAsLastSibling(); Wheel.transform.SetAsLastSibling(); Wheel.Open(); SetUnderlyingInput(false);
- }
- public void CloseWheel() {if(Wheel)Wheel.Close(); SetUnderlyingInput(true);}
- public DailyCheckInScreen Daily { get; private set; }
- public void AttachDaily(DailyCheckInScreen daily) { Daily=daily; }
- public void OpenDaily()
- {
- if(!IsInitialized || CurrentPage!=UIPage.Lobby || Shop.IsVisible || Daily.IsVisible || (Wheel && Wheel.IsVisible) || (Profile && Profile.IsVisible) || (Settings && Settings.IsVisible) || (Help && Help.IsVisible))return;
- transform.SetAsLastSibling(); Daily.transform.SetAsLastSibling(); Daily.Open(); SetUnderlyingInput(false);
- }
- public void CloseDaily() { if(Daily)Daily.SetVisible(false); SetUnderlyingInput(true); }
- public ShopScreen Shop { get; private set; }
- public Button GameShopButton { get; private set; }
- public void AttachShop(ShopScreen shop)
- {
- Shop=shop; GameShopButton=Gameplay.transform.Find("ShopEntry")?.GetComponent<Button>();
- if(!GameShopButton)GameShopButton=UIBuilder.Button("ShopEntry", Gameplay.transform,"SHOP",152,888,126,36,UIBuilder.Gold,UIBuilder.Hex("A56600"),13);
- }
- public void OpenShop()
- {
- if(!IsInitialized || (CurrentPage!=UIPage.Lobby && CurrentPage!=UIPage.Gameplay))return;
- if(CurrentPage==UIPage.Gameplay && bootstrap.Flow.State!=GameFlowState.Running)return;
- if(Shop.IsVisible || (Daily && Daily.IsVisible) || (Wheel && Wheel.IsVisible) || (Profile && Profile.IsVisible) || (Settings && Settings.IsVisible) || (Help && Help.IsVisible))return;
- transform.SetAsLastSibling(); Shop.transform.SetAsLastSibling(); Shop.Open(); SetUnderlyingInput(false);
- shopFlow=CurrentPage==UIPage.Gameplay ? bootstrap.Flow : null;
- if(shopFlow!=null)shopFlow.StateChanged+=OnShopFlowChanged;
- }
- public void CloseShop()
- {
- bool returnToSettings=shopReturnsToSettings;shopReturnsToSettings=false;
- if(shopFlow!=null)shopFlow.StateChanged-=OnShopFlowChanged; shopFlow=null;
- if(Shop)Shop.SetVisible(false); SetUnderlyingInput(true);if(returnToSettings)OpenSettings();
- }
- private void OnShopFlowChanged(GameFlowState previous,GameFlowState next) { if(next!=GameFlowState.Running)CloseShop(); }
- private void SetUnderlyingInput(bool enabled)
- {
- if(Lobby)UITransition.SetInput(Lobby.gameObject,enabled);
- if(Gameplay)UITransition.SetInput(Gameplay.gameObject,enabled);
- }
- public StartScreen StartScreen { get; private set; }
- public LobbyScreen Lobby { get; private set; }
- public UIScreen Gameplay { get; private set; }
- public Text GameTitle { get; private set; }
- public Button LobbyButton { get; private set; }
- public Text ErrorLabel { get; private set; }
- public GameHUD Hud { get; private set; }
- public void AttachHUD(GameHUD hud) { Hud = hud; }
- public UIPage CurrentPage { get; private set; }
- public bool IsInitialized => player != null;
- public event Action<UIPage> PageChanged;
- public void Configure(StartScreen start, LobbyScreen lobby, UIScreen gameplay, Text title, Button lobbyButton, Text error)
- { StartScreen = start; Lobby = lobby; Gameplay = gameplay; GameTitle = title; LobbyButton = lobbyButton; ErrorLabel = error; }
- public void Initialize(SavedPlayer savedPlayer, BoardDemoBootstrap boardBootstrap, bool ownPlayer = false)
- {
- if (savedPlayer == null || !boardBootstrap) throw new ArgumentNullException(nameof(savedPlayer));
- if (ReferenceEquals(player, savedPlayer) && bootstrap == boardBootstrap) return;
- Cleanup(); player = savedPlayer; bootstrap = boardBootstrap; ownsPlayer = ownPlayer;
- bootstrap.ConfigureAutomaticStart(false);
- modes = new ModeSelectionController(bootstrap, player); modes.LobbyRequested += OnLobbyRequested;
- StartScreen.StartRequested += ShowLobby; Lobby.ModeRequested += OnModeRequested;
- Lobby.HelpRequested+=OpenHelp;Help.CloseRequested+=CloseHelp;Settings.HelpRequested+=OpenHelpFromSettings;
- Lobby.SettingsRequested+=OpenSettings;Settings.CloseRequested+=CloseSettings;Settings.ShopRequested+=OpenShopFromSettings;Settings.ResetCompleted+=OnResetCompleted;Settings.Bind(player);Settings.BindAudio(GameAudio.Instance);
- Lobby.ProfileRequested+=OpenProfile;Profile.CloseRequested+=CloseProfile;Profile.Bind(player);player.ProfileChanged+=OnProfileChanged;
- Lobby.WheelRequested+=OpenWheel; Wheel.CloseRequested+=CloseWheel; Wheel.Bind(player);
- Lobby.DailyRequested += OpenDaily; Daily.CloseRequested += CloseDaily; Daily.Bind(player);
- Lobby.ShopRequested += OpenShop; Shop.CloseRequested += CloseShop; GameShopButton.onClick.AddListener(OpenShop); Shop.Bind(player);
- LobbyButton.onClick.AddListener(ShowLobby); RefreshLobby(); SetPage(UIPage.Start);
- }
- public void ShowLobby()
- {
- if (!IsInitialized || navigating) return;
- if (CurrentPage == UIPage.Gameplay && bootstrap.Flow != null && bootstrap.Flow.State != GameFlowState.Abandoned)
- { modes.ReturnToLobby(); return; }
- OnLobbyRequested();
- }
- private void OnLobbyRequested() { if (!this || !StartScreen || !Lobby || !Gameplay || !IsInitialized) return; RefreshLobby(); SetPage(UIPage.Lobby); }
- private void OnModeRequested(GameMode mode) { SelectMode(mode); }
- public bool SelectMode(GameMode mode)
- {
- 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;
- navigating = true;
- try
- {
- SetPage(UIPage.Gameplay); Canvas.ForceUpdateCanvases();
- var flow = modes.StartMode(mode); var level = flow.Runtime.Session.Definition;
- Hud?.Bind(flow, bootstrap.GetComponent<BoardInteractionController>());
- ErrorLabel.text = ""; return true;
- }
- catch (Exception error)
- {
- ErrorLabel.text = "Unable to open this mode."; SetPage(UIPage.Lobby);
- Debug.LogError("DevilLink2 UI mode startup failed: " + error, this); return false;
- }
- finally { navigating = false; }
- }
- public static string ModeName(GameMode mode)
- { return mode == GameMode.Classic ? "CLASSIC" : mode == GameMode.TimeAttack ? "TIME ATTACK" : "ZEN MODE"; }
- public void RefreshLobby()
- {
- if (!this || !Lobby || player == null) return;
- if (!ReferenceEquals(subscribedEconomy, player.Economy))
- {
- if (subscribedEconomy != null) subscribedEconomy.PersistentStateChanged -= OnEconomyChanged;
- subscribedEconomy = player.Economy; subscribedEconomy.PersistentStateChanged += OnEconomyChanged;
- for(int i=0;i<3;i++)lastInventory[i]=subscribedEconomy.Inventory.GetCount((ItemType)i);
- heardCoins.Clear();foreach(var entry in subscribedEconomy.Ledger.Entries)heardCoins.Add(entry.Id);
- }
- Lobby.Refresh(player);
- }
- private void OnEconomyChanged(PlayerChangeKind kind)
- {
- var currentIds=new System.Collections.Generic.HashSet<Guid>();
- foreach(var entry in subscribedEconomy.Ledger.Entries) {
- currentIds.Add(entry.Id);
- if(heardCoins.Add(entry.Id) && entry.Delta>0)GameAudio.Sound(AudioCue.Coin_Reward,"coin:"+entry.Id);
- }
- heardCoins.IntersectWith(currentIds);
- for(int i=0;i<3;i++) {
- int count=subscribedEconomy.Inventory.GetCount((ItemType)i);
- if(kind==PlayerChangeKind.ItemUsed && count<lastInventory[i])GameAudio.Sound(i==0?AudioCue.Item_Hint:i==1?AudioCue.Item_Shuffle:AudioCue.Item_Time);
- lastInventory[i]=count;
- }
- RefreshLobby(); if(Shop)Shop.Refresh(); if(Daily)Daily.Refresh(); if(Wheel)Wheel.Refresh();if(Profile)Profile.Refresh();
- }
- private void SetPage(UIPage page)
- {
- 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;
- helpReturnsToSettings=false;CloseHelp();shopReturnsToSettings=false;CloseSettings();CloseProfile(); CloseWheel(); CloseDaily(); CloseShop(); CurrentPage = page;
- StartScreen.SetVisible(page == UIPage.Start); Lobby.SetVisible(page == UIPage.Lobby); Gameplay.SetVisible(page == UIPage.Gameplay);
- if(GameAudio.Instance && page!=UIPage.Gameplay)GameAudio.Instance.SetMusic(MusicTrack.Lobby);
- PageChanged?.Invoke(page);
- }
- private void Cleanup()
- {
- if(Lobby)Lobby.HelpRequested-=OpenHelp;if(Help)Help.CloseRequested-=CloseHelp;if(Settings)Settings.HelpRequested-=OpenHelpFromSettings;
- helpReturnsToSettings=false;CloseHelp();shopReturnsToSettings=false;CloseSettings();if(Lobby)Lobby.SettingsRequested-=OpenSettings;
- if(Settings){Settings.BindAudio(null);Settings.CloseRequested-=CloseSettings;Settings.ShopRequested-=OpenShopFromSettings;Settings.ResetCompleted-=OnResetCompleted;Settings.Bind(null);}
- CloseProfile();if(player!=null)player.ProfileChanged-=OnProfileChanged;
- if(Lobby)Lobby.ProfileRequested-=OpenProfile;if(Profile){Profile.CloseRequested-=CloseProfile;Profile.Bind(null); }
- CloseWheel(); if(Lobby)Lobby.WheelRequested-=OpenWheel;
- if(Wheel) {Wheel.CloseRequested-=CloseWheel;Wheel.Bind(null); }
- CloseDaily();
- if(Lobby)Lobby.DailyRequested-=OpenDaily;
- if(Daily) { Daily.CloseRequested-=CloseDaily; Daily.Bind(null); }
- CloseShop();
- if(Lobby)Lobby.ShopRequested -= OpenShop;
- if(Shop) { Shop.CloseRequested -= CloseShop; Shop.Bind(null); }
- if(GameShopButton)GameShopButton.onClick.RemoveListener(OpenShop);
- if (Hud) Hud.Unbind();
- if (StartScreen) StartScreen.StartRequested -= ShowLobby;
- if (Lobby) Lobby.ModeRequested -= OnModeRequested;
- if (LobbyButton) LobbyButton.onClick.RemoveListener(ShowLobby);
- if (subscribedEconomy != null) subscribedEconomy.PersistentStateChanged -= OnEconomyChanged;
- subscribedEconomy = null;
- if (modes != null) { modes.LobbyRequested -= OnLobbyRequested; modes.Dispose(); modes = null; }
- if (bootstrap && bootstrap.Flow != null) bootstrap.Flow.ReturnToLobby();
- if (Gameplay) Gameplay.SetVisible(false);
- if (ownsPlayer) player?.Dispose();
- player = null; ownsPlayer = false;
- }
- private void OnDisable() { Cleanup(); }
- private void OnDestroy() { Cleanup(); }
- }
- }
|