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

129 lines
4.1 KiB
C#
Raw Normal View History

2024-10-08 06:13:52 +00:00
using System.Collections;
using System.Collections.Generic;
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;
[SerializeField, Required]
private Slider _expSlider;
[SerializeField]
private Image _filledImage;
[SerializeField]
private float _animationTime = 0.2f;
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;
private Tween _tween;
private bool _isAnimating;
private void Awake()
{
EventManager.OnChangeExp += ChangeExp;
EventManager.OnLevelUp += ChangeLevel;
2024-11-07 09:13:54 +00:00
_expSlider.value = 0f;
2024-10-08 06:13:52 +00:00
_originalColor = _filledImage.color;
_tween = _filledImage.DOColor(Color.white, 0.25f)
.SetAutoKill(false)
.Pause()
.OnComplete(() =>
{
_filledImage.DOColor(_originalColor, 0.25f);
});
}
private void OnDestroy()
{
EventManager.OnChangeExp -= ChangeExp;
EventManager.OnLevelUp += ChangeLevel;
_tween.Kill();
}
private void ChangeLevel(LevelData levelData)
{
2024-11-07 09:13:54 +00:00
_levelText.text = $"Round.{levelData.Idx}";
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);
_expSlider.value = expClamp;
2024-10-20 17:21:39 +00:00
2024-10-08 06:13:52 +00:00
if (newExp >= requireExp)
{
_tween.Restart();
yield return _tween.WaitForCompletion();
TycoonManager.Instance.TycoonStatus.CurrentLevel++;
2024-10-10 08:42:20 +00:00
currentLevelData = TycoonManager.Instance.GetCurrentLevelData();
2024-10-08 06:13:52 +00:00
requireExp = currentLevelData.RequiredExp;
_expSlider.value = 0f;
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
}
}
}