CapersProject/Assets/02.Scripts/Ui/Tycoon/TycoonResultUi.cs
2024-11-17 13:29:57 +09:00

468 lines
15 KiB
C#

using System.Collections;
using System.Collections.Generic;
using BlueWater.Npcs.Customers;
using BlueWater.Tycoons;
using BlueWater.Utility;
using DG.Tweening;
using UnityEngine;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
namespace BlueWater.Uis
{
public class TycoonResultUi : PopupUi
{
[Title("결과 카드")]
[SerializeField]
private TycoonResultCard _cardObject;
[SerializeField]
private Transform _cardLocation;
[Title("컴포넌트")]
[SerializeField]
private GameObject _panel;
[Title("타이틀")]
[SerializeField]
private GameObject _titlePanel;
[Title("카드")]
[SerializeField]
private GameObject _cardTitlePanel;
[SerializeField]
private GameObject _resultCardPanel;
[SerializeField]
private GameObject _resultCardContents;
[Title("라운드")]
[SerializeField]
private GameObject _roundPanel;
[SerializeField]
private TMP_Text _roundText;
[Title("플레이 타임")]
[SerializeField]
private GameObject _playTimePanel;
[SerializeField]
private TMP_Text _playTimeText;
[Title("서비스")]
[SerializeField]
private GameObject _serviceTitlePanel;
[SerializeField]
private GameObject _serviceContents;
[Title("Good 서빙")]
[SerializeField]
private GameObject _goodServingPanel;
[SerializeField]
private TMP_Text _goodServingText;
private int _goodServingCount;
[Title("Fail 서빙")]
[SerializeField]
private GameObject _failServingPanel;
[SerializeField]
private TMP_Text _failedServingText;
private int _failedServingCount;
[Title("Miss 서빙")]
[SerializeField]
private GameObject _missServingPanel;
[SerializeField]
private TMP_Text _missServingText;
private int _missServingCount;
[Title("Good 청소")]
[SerializeField]
private GameObject _goodCleaningPanel;
[SerializeField]
private TMP_Text _goodCleaningText;
private int _goodCleaningCount;
[Title("Fail 청소")]
[SerializeField]
private GameObject _failedCleaningPanel;
[SerializeField]
private TMP_Text _failedCleaningText;
private int _failedCleaningCount;
[Title("골드")]
[SerializeField]
private GameObject _goldTitlePanel;
[SerializeField]
private GameObject _goldContents;
[Title("획득한 골드")]
[SerializeField]
private GameObject _goldGainedPanel;
[SerializeField]
private TMP_Text _goldGainedText;
private int _goldGained;
[SerializeField]
private GameObject _plusObject;
[Title("획득한 팁")]
[SerializeField]
private GameObject _tipGainedPanel;
[SerializeField]
private TMP_Text _tipGainedText;
private int _tipGained;
[SerializeField]
private GameObject _minusObject;
[Title("사용한 골드")]
[SerializeField]
private GameObject _goldSpentPanel;
[SerializeField]
private TMP_Text _goldSpentText;
private int _goldSpent;
[Title("총 획득 골드")]
[SerializeField]
private GameObject _totalGoldPanel;
[SerializeField]
private TMP_Text _totalGoldText;
[SerializeField]
private TMP_Text _minusPercentText;
[Title("Press Any Key")]
[SerializeField]
private GameObject _pressAnyKeyPanel;
[SerializeField]
private TMP_Text _pressAnyKeyText;
[Title("연출 효과")]
[SerializeField]
private float _panelWaitingTime = 0.5f;
[SerializeField]
private float _elementWaitingTime = 0.3f;
[SerializeField]
private float _totalGoldDuration = 1f;
private Coroutine _showResultInstance;
private Tween _pressAnyKeyTween;
private InputAction _cancelAction;
private InputAction _pressAnyKeyAction;
private float _playTime;
private void Awake()
{
EventManager.OnShowResult += Open;
EventManager.OnOrderResult += AddServingCount;
EventManager.OnMissedServing += AddMissedServingCount;
EventManager.OnCleaningResult += AddCleaningCount;
EventManager.OnAddedGold += AddGoldCount;
_pressAnyKeyTween = _pressAnyKeyText
.DOFade(0f, 1f)
.From(1f)
.SetLoops(-1, LoopType.Yoyo)
.SetEase(Ease.InOutSine)
.SetAutoKill(false)
.SetUpdate(true)
.Pause();
}
public void Start()
{
_cancelAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, "Cancel");
_pressAnyKeyAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, "PressAnyKey");
foreach (Transform element in _resultCardContents.transform)
{
Destroy(element.gameObject);
}
SetActiveUi(false);
}
public void Update()
{
_playTime += Time.deltaTime; //플레이타임 측정 (일시정지나 메뉴얼보는 시간은 제외)
}
private void OnDestroy()
{
EventManager.OnShowResult -= Open;
EventManager.OnOrderResult -= AddServingCount;
EventManager.OnMissedServing -= AddMissedServingCount;
EventManager.OnCleaningResult -= AddCleaningCount;
EventManager.OnAddedGold -= AddGoldCount;
_cancelAction.performed -= OnCancel;
_pressAnyKeyAction.performed -= OnPressAnyKey;
_pressAnyKeyTween.Kill();
}
[Button("결과 연출 테스트")]
public override void Open()
{
VisualFeedbackManager.Instance.SetBaseTimeScale(0.0f);
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.TycoonUi);
PopupUiController.RegisterPopup(this);
IsOpened = true;
Utils.StartUniqueCoroutine(this, ref _showResultInstance, ShowResultCoroutine());
}
public override void Close()
{
}
public override void EnableInput()
{
_pressAnyKeyAction.Disable();
_cancelAction.performed += OnCancel;
_pressAnyKeyAction.performed += OnPressAnyKey;
}
public override void DisableInput()
{
}
private void OnCancel(InputAction.CallbackContext context)
{
_cancelAction.performed -= OnCancel;
ShowImmediately();
}
private void OnPressAnyKey(InputAction.CallbackContext context)
{
SceneManager.LoadScene("00.TycoonTitle");
}
private IEnumerator ShowResultCoroutine()
{
SetResultData();
WaitForSecondsRealtime panelwaitingTime = new WaitForSecondsRealtime(_panelWaitingTime);
WaitForSecondsRealtime elementWaitingTime = new WaitForSecondsRealtime(_elementWaitingTime);
_panel.SetActive(true);
_titlePanel.SetActive(true);
yield return panelwaitingTime;
_cardTitlePanel.SetActive(true);
_resultCardPanel.SetActive(true);
yield return panelwaitingTime;
_resultCardContents.SetActive(true);
yield return panelwaitingTime;
_roundPanel.SetActive(true);
_playTimePanel.SetActive(true);
yield return panelwaitingTime;
_serviceTitlePanel.SetActive(true);
_serviceContents.SetActive(true);
yield return panelwaitingTime;
_goodServingPanel.SetActive(true);
yield return elementWaitingTime;
_failServingPanel.SetActive(true);
yield return elementWaitingTime;
_missServingPanel.SetActive(true);
yield return panelwaitingTime;
_goodCleaningPanel.SetActive(true);
yield return elementWaitingTime;
_failedCleaningPanel.SetActive(true);
yield return panelwaitingTime;
_goldTitlePanel.SetActive(true);
_goldContents.SetActive(true);
yield return panelwaitingTime;
_goldGainedPanel.SetActive(true);
yield return elementWaitingTime;
_plusObject.SetActive(true);
yield return elementWaitingTime;
_tipGainedPanel.SetActive(true);
yield return elementWaitingTime;
_minusObject.SetActive(true);
yield return elementWaitingTime;
_goldSpentPanel.SetActive(true);
yield return panelwaitingTime;
float elapsedTime = 0f;
int currentGold = TycoonManager.Instance.TycoonStatus.CurrentGold;
int targetGold = ES3.Load("EndGold", 0);
string totalGoldLocalized = Utils.GetLocalizedString("TotalGold");
_totalGoldText.text = $"{totalGoldLocalized} : {currentGold:N0}";
_totalGoldPanel.SetActive(true);
yield return panelwaitingTime;
_minusPercentText.enabled = true;
yield return panelwaitingTime;
while (elapsedTime <= _totalGoldDuration)
{
var newGold = (int)Mathf.Lerp(currentGold, targetGold, elapsedTime / _totalGoldDuration);
_totalGoldText.text = $"{totalGoldLocalized} : {newGold:N0}";
elapsedTime += Time.unscaledDeltaTime;
yield return null;
}
_totalGoldText.text = $"{totalGoldLocalized} : {targetGold:N0}";
yield return panelwaitingTime;
_pressAnyKeyPanel.SetActive(true);
_pressAnyKeyTween.Restart();
_cancelAction.performed -= OnCancel;
_pressAnyKeyAction.Enable();
yield return null;
}
private void SetActiveUi(bool isActive)
{
_panel.SetActive(isActive);
_titlePanel.SetActive(isActive);
_cardTitlePanel.SetActive(isActive);
_resultCardPanel.SetActive(isActive);
_resultCardContents.SetActive(isActive);
_roundPanel.SetActive(isActive);
_playTimePanel.SetActive(isActive);
_serviceTitlePanel.SetActive(isActive);
_serviceContents.SetActive(isActive);
_goodServingPanel.SetActive(isActive);
_failServingPanel.SetActive(isActive);
_missServingPanel.SetActive(isActive);
_goodCleaningPanel.SetActive(isActive);
_failedCleaningPanel.SetActive(isActive);
_goldTitlePanel.SetActive(isActive);
_goldContents.SetActive(isActive);
_goldGainedPanel.SetActive(isActive);
_plusObject.SetActive(isActive);
_tipGainedPanel.SetActive(isActive);
_minusObject.SetActive(isActive);
_goldSpentPanel.SetActive(isActive);
_totalGoldPanel.SetActive(isActive);
_minusPercentText.enabled = isActive;
_pressAnyKeyPanel.SetActive(isActive);
}
private void SetResultData()
{
Dictionary<string, int> selectedCards = TycoonManager.Instance.CardDataSo.GetselectedCard();
foreach (var element in selectedCards)
{
TycoonResultCard newCard = Instantiate(_cardObject, _cardLocation);
newCard.SetImage(TycoonManager.Instance.CardDataSo.GetDataByIdx(element.Key).Sprite);
newCard.SetText(TycoonManager.Instance.CardDataSo.GetDataByIdx(element.Key).ScriptText);
newCard.SetCount(element.Value);
newCard.name = element.Key;
}
_roundText.text = $"{Utils.GetLocalizedString("Round")} : {TycoonManager.Instance.GetCurrentLevelData().Idx}";
_playTimeText.text = $"{Utils.GetLocalizedString("PlayTime")} : {Mathf.FloorToInt(_playTime / 60f):D2} : {Mathf.FloorToInt(_playTime % 60f):D2}";
_goodServingText.text = _goodServingCount.ToString();
_failedServingText.text = _failedServingCount.ToString();
_missServingText.text = _missServingCount.ToString();
_goodCleaningText.text = _goodCleaningCount.ToString();
_failedCleaningText.text = _failedCleaningCount.ToString();
_goldGainedText.text = _goldGained.ToString("N0");
_tipGainedText.text = _tipGained.ToString("N0");
_goldSpentText.text = _goldSpent.ToString("N0");
_totalGoldText.text = $"{Utils.GetLocalizedString("TotalGold")} : {ES3.Load("EndGold", 0):N0}";
_minusPercentText.text = $"- {(int)(TycoonManager.Instance.TycoonStatus.EndGoldMultiplier * 100)}%";
_pressAnyKeyText.text = $"- {Utils.GetLocalizedString("PressAnyKey")} -";
_pressAnyKeyTween.Restart();
}
[Button("결과 즉시 테스트")]
private void ShowImmediately()
{
if (_showResultInstance != null)
{
StopCoroutine(_showResultInstance);
_showResultInstance = null;
}
SetResultData();
SetActiveUi(true);
_pressAnyKeyAction.Enable();
}
private void AddServingCount(Customer orderedCustomer, bool orderedCorrected)
{
if (orderedCorrected)
{
_goodServingCount++;
}
else
{
_failedServingCount++;
}
}
private void AddMissedServingCount() => _failedServingCount++;
private void AddCleaningCount(bool isSucceed)
{
if (isSucceed)
{
_goodCleaningCount++;
}
else
{
_failedCleaningCount++;
}
}
private void AddGoldCount(int addedGold, bool isTip)
{
if (addedGold < 0)
{
_goldSpent += Mathf.Abs(addedGold);
return;
}
if (isTip)
{
_tipGained += addedGold;
}
else
{
_goldGained += addedGold;
}
}
}
}