CapersProject/Assets/02.Scripts/Ui/Title/TycoonTitle.cs

242 lines
6.8 KiB
C#
Raw Normal View History

2024-10-08 06:12:27 +00:00
using BlueWater.Audios;
2024-11-18 03:51:28 +00:00
using BlueWater.Uis;
2024-10-08 06:12:27 +00:00
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
2024-11-18 03:51:28 +00:00
using UnityEngine.SceneManagement;
2024-10-08 06:12:27 +00:00
using UnityEngine.UI;
namespace BlueWater.Titles
{
2024-11-18 03:51:28 +00:00
public class TycoonTitle : PopupUi
2024-10-08 06:12:27 +00:00
{
2024-11-18 03:51:28 +00:00
[SerializeField]
private GameObject _panel;
2024-11-11 12:23:27 +00:00
[SerializeField]
private GameObject _titleMenuUiPanel;
[SerializeField]
2024-11-18 03:51:28 +00:00
private TitleOptions titleOptions;
2024-11-11 12:23:27 +00:00
2024-10-08 06:12:27 +00:00
[SerializeField]
private Button _startGameButton;
2024-11-11 12:23:27 +00:00
2024-10-31 12:17:10 +00:00
[SerializeField]
2024-11-19 07:57:05 +00:00
private Button _resumeGameButton;
2024-11-11 12:23:27 +00:00
2024-10-31 12:17:10 +00:00
[SerializeField]
2024-11-19 07:57:05 +00:00
private Button _optionsButton;
[SerializeField]
private Button _lobbyButton;
[SerializeField]
private Button _quitGameButton;
2024-11-18 03:51:28 +00:00
[SerializeField]
private bool _isTitleScene = true;
2024-10-08 06:12:27 +00:00
[SerializeField]
private TMP_Text _versionText;
[SerializeField]
2024-10-31 12:17:10 +00:00
private string _dailyBgm = "DailyBgm1";
2024-11-19 07:57:05 +00:00
2024-11-18 03:51:28 +00:00
private SceneController _sceneController;
2024-11-17 04:29:57 +00:00
private InputAction _interactionEAction;
2024-11-18 03:51:28 +00:00
private InputAction _openAction;
private InputAction _closeAction;
2024-11-17 04:29:57 +00:00
private bool _isQuitting;
2024-11-18 03:51:28 +00:00
private bool _onButtonClicked;
2024-10-08 06:12:27 +00:00
private void Start()
{
2024-11-19 07:57:05 +00:00
_interactionEAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.InteractionE);
_openAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.Tycoon, TycoonActions.Options);
_closeAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.Cancel);
2024-10-31 12:17:10 +00:00
2024-11-18 03:51:28 +00:00
if (_isTitleScene)
{
_sceneController = SceneController.Instance;
_startGameButton.onClick.AddListener(_sceneController.FadeIn);
AudioManager.Instance.PlayBgm(_dailyBgm);
Open();
}
else
{
_panel.SetActive(false);
_openAction.performed += OnOpen;
}
2024-10-31 12:17:10 +00:00
2024-11-19 07:57:05 +00:00
_startGameButton?.onClick.AddListener(() => { _onButtonClicked = true; });
_resumeGameButton?.onClick.AddListener(() => { _onButtonClicked = true; });
_optionsButton?.onClick.AddListener(() => { _onButtonClicked = true; });
_lobbyButton?.onClick.AddListener(() => { _onButtonClicked = true; });
_quitGameButton?.onClick.AddListener(() => { _onButtonClicked = true; });
2024-10-31 12:17:10 +00:00
2024-11-19 07:57:05 +00:00
PlayerInputKeyManager.Instance.AddOnActionKeyboard(OnKeyboard);
PlayerInputKeyManager.Instance.AddOnActionMouse(OnMouse);
titleOptions.CloseOptions = HideSettingUi;
2024-11-18 03:51:28 +00:00
_versionText.text = GetVersion();
2024-10-31 12:17:10 +00:00
}
2024-11-18 03:51:28 +00:00
private void OnDestroy()
2024-11-17 04:29:57 +00:00
{
2024-11-18 03:51:28 +00:00
if (_isTitleScene)
{
2024-11-19 07:57:05 +00:00
if (_startGameButton != null && _sceneController != null)
{
_startGameButton.onClick.RemoveListener(_sceneController.FadeIn);
}
2024-11-18 03:51:28 +00:00
}
2024-11-19 07:57:05 +00:00
PlayerInputKeyManager.Instance.RemoveOnActionKeyboard(OnKeyboard);
PlayerInputKeyManager.Instance.RemoveOnActionMouse(OnMouse);
if (_interactionEAction != null)
{
_interactionEAction.performed -= OnInteractionE;
}
if (_openAction != null)
{
_openAction.performed -= OnOpen;
}
if (_closeAction != null)
{
_closeAction.performed -= OnClose;
}
2024-10-31 12:17:10 +00:00
}
2024-11-18 03:51:28 +00:00
public void OnOpen(InputAction.CallbackContext context)
2024-10-31 12:17:10 +00:00
{
2024-11-18 03:51:28 +00:00
Open();
}
public override void Open()
{
2024-11-19 07:57:05 +00:00
if (!_isTitleScene)
2024-11-18 03:51:28 +00:00
{
VisualFeedbackManager.Instance.SetBaseTimeScale(0.0f);
}
2024-11-19 07:57:05 +00:00
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.TycoonUi);
PopupUiController.RegisterPopup(this);
_panel.SetActive(true);
IsOpened = true;
2024-11-18 03:51:28 +00:00
}
public void OnClose(InputAction.CallbackContext context)
{
Close();
2024-10-08 06:12:27 +00:00
}
2024-11-18 03:51:28 +00:00
public override void Close()
2024-11-19 07:57:05 +00:00
{
if (!_isTitleScene)
{
_panel.SetActive(false);
PopupUiController.UnregisterPopup(this);
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.Tycoon);
IsOpened = false;
VisualFeedbackManager.Instance.ResetTimeScale();
}
}
public override void EnableInput()
{
if (_isTitleScene)
{
}
else
{
_closeAction.performed += OnClose;
}
_interactionEAction.performed += OnInteractionE;
}
public override void DisableInput()
2024-10-08 06:12:27 +00:00
{
2024-11-18 03:51:28 +00:00
if (_isTitleScene)
{
}
else
{
_closeAction.performed -= OnClose;
}
2024-11-17 04:29:57 +00:00
_interactionEAction.performed -= OnInteractionE;
2024-10-08 06:12:27 +00:00
}
2024-11-19 07:57:05 +00:00
2024-11-18 03:51:28 +00:00
private void OnKeyboard()
2024-10-08 06:12:27 +00:00
{
2024-11-19 07:57:05 +00:00
if (_isTitleScene)
{
EventSystem.current.SetSelectedGameObject(_startGameButton.gameObject);
}
else
{
EventSystem.current.SetSelectedGameObject(_resumeGameButton.gameObject);
}
2024-11-18 03:51:28 +00:00
}
private void OnMouse()
{
if (!_onButtonClicked)
{
EventSystem.current.SetSelectedGameObject(null);
}
2024-10-08 06:12:27 +00:00
}
private string GetVersion()
{
#if UNITY_EDITOR
return PlayerSettings.bundleVersion;
#else
return Application.version;
#endif
}
2024-11-17 04:29:57 +00:00
public void OnInteractionE(InputAction.CallbackContext context)
2024-10-08 06:12:27 +00:00
{
2024-11-17 04:29:57 +00:00
var current = EventSystem.current.currentSelectedGameObject;
if (!current) return;
2024-10-08 06:12:27 +00:00
2024-11-17 04:29:57 +00:00
var currenButton = current.GetComponent<Button>();
currenButton.onClick.Invoke();
2024-10-08 06:12:27 +00:00
}
public void QuitGame()
{
#if UNITY_EDITOR
2024-11-18 03:51:28 +00:00
EditorApplication.isPlaying = false;
2024-10-08 06:12:27 +00:00
#else
Application.Quit();
#endif
}
2024-11-17 04:29:57 +00:00
public void ShowSettingUi()
{
_titleMenuUiPanel.SetActive(false);
2024-11-19 07:57:05 +00:00
titleOptions.Open();
2024-11-17 04:29:57 +00:00
}
public void HideSettingUi()
{
2024-11-19 07:57:05 +00:00
titleOptions.Close();
2024-11-17 04:29:57 +00:00
_titleMenuUiPanel.SetActive(true);
2024-11-18 03:51:28 +00:00
}
public void MoveLobbyScene()
{
SceneManager.LoadScene("00.TycoonTitle");
2024-11-17 04:29:57 +00:00
}
2024-10-08 06:12:27 +00:00
}
}