CapersProject/Assets/02.Scripts/Ui/Tycoon/ExpUi.cs

161 lines
5.0 KiB
C#
Raw Normal View History

2024-10-08 06:13:52 +00:00
using System.Collections;
using System.Collections.Generic;
2024-11-17 15:36:08 +00:00
using BlueWater.Audios;
2024-10-08 06:13:52 +00:00
using BlueWater.Tycoons;
using BlueWater.Utility;
using DG.Tweening;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BlueWater.Uis
{
public class ExpUi : MonoBehaviour
{
[SerializeField, Required]
private TMP_Text _levelText;
2024-11-25 01:40:15 +00:00
[SerializeField]
2024-10-08 06:13:52 +00:00
private Slider _expSlider;
[SerializeField]
private Image _filledImage;
[SerializeField]
private float _animationTime = 0.2f;
2024-11-12 12:07:23 +00:00
2024-11-17 15:36:08 +00:00
[SerializeField]
private string _levelUpSfxName = "LevelUp";
2024-11-12 12:07:23 +00:00
[Title("레벨 텍스트")]
[SerializeField]
private Vector3 _punchScale = new(1.5f, 1.5f, 1.5f);
[SerializeField]
private float _duration = 1f;
2024-10-08 06:13:52 +00:00
2024-10-20 17:21:39 +00:00
private Queue<int> _expQueue = new();
2024-10-08 06:13:52 +00:00
private Coroutine _changeExpInstance;
private Color _originalColor;
2024-11-12 12:07:23 +00:00
private Tween _expSliderTween;
private Tween _levelTextTween;
2024-10-08 06:13:52 +00:00
private bool _isAnimating;
private void Awake()
{
EventManager.OnChangeExp += ChangeExp;
EventManager.OnLevelUp += ChangeLevel;
2024-11-25 01:40:15 +00:00
if (_expSlider)
{
_expSlider.value = 0f;
}
2024-10-08 06:13:52 +00:00
_originalColor = _filledImage.color;
2024-11-12 12:07:23 +00:00
_expSliderTween = _filledImage.DOColor(Color.white, 0.25f)
2024-10-08 06:13:52 +00:00
.SetAutoKill(false)
.Pause()
.OnComplete(() =>
{
_filledImage.DOColor(_originalColor, 0.25f);
});
2024-11-12 12:07:23 +00:00
_levelTextTween = _levelText.transform.DOPunchScale(_punchScale, _duration, 1)
.SetAutoKill(false)
.Pause();
2024-10-08 06:13:52 +00:00
}
private void OnDestroy()
{
EventManager.OnChangeExp -= ChangeExp;
EventManager.OnLevelUp += ChangeLevel;
2024-11-12 12:07:23 +00:00
_expSliderTween.Kill();
_levelTextTween.Kill();
2024-10-08 06:13:52 +00:00
}
private void ChangeLevel(LevelData levelData)
{
2024-11-07 09:13:54 +00:00
_levelText.text = $"Round.{levelData.Idx}";
2024-11-15 07:28:13 +00:00
if (levelData.Idx != "1")
{
_levelTextTween.Restart();
}
2024-10-08 06:13:52 +00:00
}
2024-10-20 17:21:39 +00:00
private void ChangeExp(int addedExp)
2024-10-08 06:13:52 +00:00
{
2024-10-20 17:21:39 +00:00
_expQueue.Enqueue(addedExp);
2024-10-08 06:13:52 +00:00
if (!_isAnimating)
{
Utils.StartUniqueCoroutine(this, ref _changeExpInstance, AnimateExpChange());
}
}
private IEnumerator AnimateExpChange()
{
_isAnimating = true;
while (_expQueue.Count > 0)
{
2024-10-20 17:21:39 +00:00
var currentLevelData = TycoonManager.Instance.GetCurrentLevelData();
var startExp = TycoonManager.Instance.TycoonStatus.CurrentExp;
var addedExp = _expQueue.Dequeue();
2024-10-08 06:13:52 +00:00
var requireExp = currentLevelData.RequiredExp;
var endExp = startExp + addedExp;
var remainExp = endExp - requireExp;
var elapsedTime = 0f;
while (true)
{
var newExp = Mathf.Lerp(startExp, endExp, elapsedTime / _animationTime);
2024-10-20 17:21:39 +00:00
TycoonManager.Instance.TycoonStatus.CurrentExp = (int)newExp;
2024-10-08 06:13:52 +00:00
var expClamp = Mathf.Clamp01(newExp / requireExp);
2024-11-25 01:40:15 +00:00
if (_expSlider)
{
_expSlider.value = expClamp;
}
2024-10-20 17:21:39 +00:00
2024-10-08 06:13:52 +00:00
if (newExp >= requireExp)
{
2024-11-12 12:07:23 +00:00
_expSliderTween.Restart();
2024-10-08 06:13:52 +00:00
2024-11-12 12:07:23 +00:00
yield return _expSliderTween.WaitForCompletion();
2024-10-08 06:13:52 +00:00
TycoonManager.Instance.TycoonStatus.CurrentLevel++;
2024-11-17 15:36:08 +00:00
AudioManager.Instance.PlaySfx(_levelUpSfxName);
2024-10-10 08:42:20 +00:00
currentLevelData = TycoonManager.Instance.GetCurrentLevelData();
2024-10-08 06:13:52 +00:00
requireExp = currentLevelData.RequiredExp;
2024-11-25 01:40:15 +00:00
if (_expSlider)
{
_expSlider.value = 0f;
}
2024-10-08 06:13:52 +00:00
newExp = 0;
2024-10-20 17:21:39 +00:00
TycoonManager.Instance.TycoonStatus.CurrentExp = (int)newExp;
2024-10-08 06:13:52 +00:00
startExp = 0;
endExp = remainExp;
2024-10-10 08:42:20 +00:00
remainExp = endExp - requireExp;
2024-10-08 06:13:52 +00:00
elapsedTime = 0f;
}
else
{
elapsedTime += Time.deltaTime;
}
if (newExp >= endExp) break;
yield return null;
}
}
_isAnimating = false;
_changeExpInstance = null;
}
[Button("경험치 증가 테스트")]
private void Test(int exp)
{
2024-10-20 17:21:39 +00:00
EventManager.InvokeChangeExp(exp);
2024-10-08 06:13:52 +00:00
}
}
}