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

310 lines
8.7 KiB
C#
Raw Normal View History

2024-11-19 12:53:00 +00:00
using System.Collections;
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-11-19 12:53:00 +00:00
[SerializeField]
private TitleQuit titleQuit;
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-19 12:53:00 +00:00
[SerializeField]
public Material _backgroundInkMaterial;
2024-11-19 13:32:43 +00:00
private PlayerInputKeyManager _playerInputKeyManager;
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-11-19 13:33:54 +00:00
private Coroutine _InkCoroutine;
2024-11-19 12:53:00 +00:00
2024-10-08 06:12:27 +00:00
private void Start()
{
2024-11-19 13:32:43 +00:00
_playerInputKeyManager = PlayerInputKeyManager.Instance;
_interactionEAction = _playerInputKeyManager.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.InteractionE);
_openAction = _playerInputKeyManager.GetAction(InputActionMaps.Tycoon, TycoonActions.Options);
_closeAction = _playerInputKeyManager.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);
2024-11-19 13:33:54 +00:00
//수정
2024-11-18 03:51:28 +00:00
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 13:32:43 +00:00
_playerInputKeyManager.AddOnActionKeyboard(OnKeyboard);
_playerInputKeyManager.AddOnActionMouse(OnMouse);
2024-11-19 07:57:05 +00:00
titleOptions.CloseOptions = HideSettingUi;
2024-11-19 12:53:00 +00:00
titleQuit.CloseQuit = HideQuitUi;
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-19 13:33:54 +00:00
//수정
2024-11-19 07:57:05 +00:00
}
2024-11-18 03:51:28 +00:00
}
2024-11-19 13:32:43 +00:00
if (_playerInputKeyManager != null)
{
_playerInputKeyManager.RemoveOnActionKeyboard(OnKeyboard);
_playerInputKeyManager.RemoveOnActionMouse(OnMouse);
}
2024-11-19 07:57:05 +00:00
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 13:32:43 +00:00
_playerInputKeyManager.SwitchCurrentActionMap(InputActionMaps.TycoonUi);
2024-11-19 07:57:05 +00:00
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);
2024-11-19 13:32:43 +00:00
_playerInputKeyManager.SwitchCurrentActionMap(InputActionMaps.Tycoon);
2024-11-19 07:57:05 +00:00
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
}
2024-11-19 12:53:00 +00:00
public void ShowQuitUi()
{
_titleMenuUiPanel.SetActive(false);
titleQuit.Open();
2024-11-19 13:33:54 +00:00
if (_InkCoroutine != null)
{
StopCoroutine(_InkCoroutine);
}
_InkCoroutine = StartCoroutine(MoveInkBackground(1.0f));
2024-11-19 12:53:00 +00:00
}
public void HideQuitUi()
{
titleQuit.Close();
_titleMenuUiPanel.SetActive(true);
2024-11-19 13:33:54 +00:00
if (_InkCoroutine != null)
2024-11-19 12:53:00 +00:00
{
2024-11-19 13:33:54 +00:00
StopCoroutine(_InkCoroutine);
2024-11-19 12:53:00 +00:00
}
2024-11-19 13:33:54 +00:00
_InkCoroutine = StartCoroutine(MoveInkBackground(0.4f));
2024-11-19 12:53:00 +00:00
}
2024-11-19 13:33:54 +00:00
private IEnumerator MoveInkBackground(float pos)
{
2024-11-19 12:53:00 +00:00
float timer = 0f;
var orgPos = _backgroundInkMaterial.GetFloat("_Position"); // 기존 _Position 값 가져오기.
2024-11-19 13:33:54 +00:00
while (timer < 0.3f)
2024-11-19 12:53:00 +00:00
{
timer += Time.unscaledDeltaTime;
2024-11-19 13:33:54 +00:00
float t = timer / 0.8f;
2024-11-19 12:53:00 +00:00
float easedT = EaseEffect.ExpoOut(t);
// Lerp로 계산한 값을 SetFloat으로 설정.
2024-11-19 13:33:54 +00:00
_backgroundInkMaterial.SetFloat("_Position", Mathf.Lerp(orgPos, pos, easedT));
2024-11-19 12:53:00 +00:00
yield return null;
}
// 마지막에 정확히 목표값 설정.
2024-11-19 13:33:54 +00:00
_backgroundInkMaterial.SetFloat("_Position", pos);
2024-11-19 12:53:00 +00:00
}
2024-11-19 13:33:54 +00:00
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
}
}