CapersProject/Assets/02.Scripts/Prop/Tycoon/MoneyCounter.cs

194 lines
5.0 KiB
C#
Raw Normal View History

2024-11-11 02:02:24 +00:00
using System;
using System.Collections;
2024-11-17 15:36:08 +00:00
using System.Collections.Generic;
using System.Linq;
2024-11-11 02:02:24 +00:00
using BlueWater.Uis;
using BlueWater.Utility;
using Sirenix.OdinInspector;
2024-11-25 12:36:48 +00:00
using TMPro;
2024-11-11 02:02:24 +00:00
using UnityEngine;
namespace BlueWater.Tycoons
{
[Serializable]
public class MoneyCounter : InteractionFurniture
{
2024-11-25 12:36:48 +00:00
[SerializeField]
private TMP_Text _totalGoldText;
2024-11-11 02:02:24 +00:00
[SerializeField]
2024-11-21 11:05:59 +00:00
private float _playerHoldingTime = 1f;
2024-11-11 02:02:24 +00:00
2024-11-25 12:36:48 +00:00
[Title("연출")]
2024-11-11 02:02:24 +00:00
[SerializeField]
private PayMoneyUi _payMoneyUiObject;
[SerializeField]
private Vector3 _offset = new(0f, 1.5f, 0f);
2024-11-17 15:36:08 +00:00
[SerializeField]
private float _delay = 0.2f;
[SerializeField]
private float _payMoneyUiDuration = 0.5f;
2024-11-25 12:36:48 +00:00
[Title("이미지")]
2024-11-11 02:02:24 +00:00
[SerializeField]
private Sprite _empty;
[SerializeField]
private Sprite _level1;
[SerializeField]
private Sprite _level2;
[SerializeField]
private Sprite _level3;
2024-11-17 15:36:08 +00:00
private Queue<int> _addedGolds = new();
2024-11-11 02:02:24 +00:00
private SpriteRenderer _spriteRenderer;
private Coroutine _gainAutoInstance;
private bool _isPlayerInteracting;
2024-11-17 15:36:08 +00:00
private bool _isGainGoldCoroutine;
2024-11-11 02:02:24 +00:00
protected override void Awake()
{
base.Awake();
2024-11-17 15:36:08 +00:00
2024-11-11 02:02:24 +00:00
EventManager.OnGainAutoMoneyCounter += GainAuto;
}
protected override void OnEnable()
{
base.OnEnable();
Initialize();
}
private void Update()
{
if (IsShowing)
{
EventManager.InvokeHoldInteracting(HoldingElapsedTime);
}
if (HoldingElapsedTime >= 1f)
{
GainMoney();
}
if (_isPlayerInteracting)
{
HoldingElapsedTime += Time.deltaTime / _playerHoldingTime;
}
else
{
if (HoldingElapsedTime > 0f)
{
HoldingElapsedTime -= Time.deltaTime;
}
}
}
private void OnDestroy()
{
if (_gainAutoInstance != null)
{
StopCoroutine(_gainAutoInstance);
_gainAutoInstance = null;
}
EventManager.OnGainAutoMoneyCounter -= GainAuto;
}
public void Initialize()
{
_spriteRenderer = VisualLook.GetComponent<SpriteRenderer>();
2024-11-25 12:36:48 +00:00
_totalGoldText.text = _addedGolds.Sum().ToString();
2024-11-11 02:02:24 +00:00
}
public override void Interaction()
{
_isPlayerInteracting = true;
}
public override void CancelInteraction()
{
_isPlayerInteracting = false;
}
public override bool CanInteraction()
{
2024-11-17 15:36:08 +00:00
return _addedGolds.Count > 0 && !GameManager.Instance.CurrentTycoonPlayer.TycoonPickupHandler.IsPickedUpAnything();
2024-11-11 02:02:24 +00:00
}
2024-11-17 15:36:08 +00:00
public void AddCurrentGold(int gold)
2024-11-11 02:02:24 +00:00
{
2024-11-17 15:36:08 +00:00
_addedGolds.Enqueue(gold);
2024-11-25 12:36:48 +00:00
_totalGoldText.text = _addedGolds.Sum().ToString();
2024-11-11 02:02:24 +00:00
ChangeSprite();
}
private void ChangeSprite()
{
2024-11-17 15:36:08 +00:00
var sprite = _addedGolds.Sum() switch
2024-11-11 02:02:24 +00:00
{
> 1000 => _level3,
> 500 => _level2,
> 0 => _level1,
_ => _empty
};
_spriteRenderer.sprite = sprite;
}
public void GainMoney()
{
2024-11-17 15:36:08 +00:00
if (_isGainGoldCoroutine) return;
StartCoroutine(GainGoldCoroutine());
}
private IEnumerator GainGoldCoroutine()
{
_isGainGoldCoroutine = true;
2024-11-11 02:02:24 +00:00
_isPlayerInteracting = false;
HoldingElapsedTime = 0f;
2024-11-17 15:36:08 +00:00
WaitForSeconds delay = new WaitForSeconds(_delay);
while (_addedGolds.Count > 0)
{
var addedGold = _addedGolds.Dequeue();
var payMoneyUi = Instantiate(_payMoneyUiObject, transform.position + _offset,
Quaternion.identity, TycoonUiManager.Instance.WorldCanvas.transform);
payMoneyUi.Initialize(addedGold, false, _payMoneyUiDuration);
yield return delay;
}
2024-11-15 07:28:13 +00:00
2024-11-25 12:36:48 +00:00
_totalGoldText.text = _addedGolds.Sum().ToString();
2024-11-11 02:02:24 +00:00
ChangeSprite();
2024-11-17 15:36:08 +00:00
_isGainGoldCoroutine = false;
2024-11-11 02:02:24 +00:00
}
public void GainAuto(int waitTime)
{
Utils.StartUniqueCoroutine(this, ref _gainAutoInstance, GainAutoCoroutine(waitTime));
}
private IEnumerator GainAutoCoroutine(int waitTime)
{
while (true)
{
yield return new WaitForSeconds(waitTime);
2024-11-17 15:36:08 +00:00
if (_addedGolds.Count > 0)
2024-11-11 02:02:24 +00:00
{
GainMoney();
}
}
}
}
}