168 lines
5.4 KiB
C#
168 lines
5.4 KiB
C#
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BulletHellTemplate
|
|
{
|
|
public class CurrencyDisplay : MonoBehaviour
|
|
{
|
|
[Header("Currency Settings")]
|
|
public string currencyID;
|
|
public TextMeshProUGUI currencyAmount;
|
|
public Image currencyIcon;
|
|
|
|
[Header("Recharge UI (only used if isRechargeable is true)")]
|
|
public bool isRechargeable;
|
|
public TextMeshProUGUI rechargeAmount;
|
|
public TextMeshProUGUI nextRechargeTime;
|
|
|
|
private Currency _currencyData;
|
|
|
|
private float timeLeftForNextCharge = 0f;
|
|
private bool isAtMax = false;
|
|
|
|
private void OnEnable()
|
|
{
|
|
MonetizationManager.OnCurrencyChanged += UpdateCurrencyDisplay;
|
|
ForceUpdateCurrency();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
MonetizationManager.OnCurrencyChanged -= UpdateCurrencyDisplay;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
StartCoroutine(InitializeCurrencyDisplay());
|
|
}
|
|
|
|
private IEnumerator InitializeCurrencyDisplay()
|
|
{
|
|
while (MonetizationManager.Singleton == null)
|
|
yield return null;
|
|
|
|
foreach (Currency item in MonetizationManager.Singleton.currencies)
|
|
{
|
|
if (item != null && item.coinID == currencyID)
|
|
{
|
|
_currencyData = item;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (_currencyData != null && currencyIcon != null)
|
|
currencyIcon.sprite = _currencyData.icon;
|
|
|
|
ForceUpdateCurrency();
|
|
|
|
if (isRechargeable && _currencyData != null && _currencyData.isRechargeableCurrency)
|
|
{
|
|
float chunkDuration = GetSecondsByScale(_currencyData.rechargeableTime, _currencyData.rechargeableTimeScale);
|
|
int current = MonetizationManager.GetCurrency(currencyID);
|
|
|
|
if (_currencyData.useMaxAmount && current >= _currencyData.maxAmount && !_currencyData.canExceedMaxValue)
|
|
{
|
|
timeLeftForNextCharge = 0;
|
|
isAtMax = true;
|
|
}
|
|
else
|
|
{
|
|
timeLeftForNextCharge = chunkDuration;
|
|
isAtMax = false;
|
|
}
|
|
}
|
|
|
|
UpdateRechargeUI(true);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isRechargeable || _currencyData == null || !_currencyData.isRechargeableCurrency)
|
|
return;
|
|
|
|
|
|
if (isAtMax) return;
|
|
timeLeftForNextCharge -= Time.deltaTime;
|
|
if (timeLeftForNextCharge <= 0f)
|
|
{
|
|
|
|
bool reachedMax = RechargeablesManager.Singleton.ApplyAllPendingCharges(_currencyData);
|
|
|
|
ForceUpdateCurrency();
|
|
|
|
if (reachedMax)
|
|
{
|
|
isAtMax = true;
|
|
timeLeftForNextCharge = 0f;
|
|
}
|
|
else
|
|
{
|
|
float chunkDuration = GetSecondsByScale(_currencyData.rechargeableTime, _currencyData.rechargeableTimeScale);
|
|
timeLeftForNextCharge = chunkDuration;
|
|
}
|
|
}
|
|
|
|
UpdateRechargeUI();
|
|
}
|
|
|
|
public void ForceUpdateCurrency()
|
|
{
|
|
UpdateCurrencyDisplay(currencyID, MonetizationManager.GetCurrency(currencyID));
|
|
}
|
|
|
|
private void UpdateCurrencyDisplay(string changedCurrency, int amount)
|
|
{
|
|
if (changedCurrency == currencyID && currencyAmount != null)
|
|
currencyAmount.text = amount.ToString();
|
|
}
|
|
|
|
private void UpdateRechargeUI(bool forceShow = false)
|
|
{
|
|
if (_currencyData == null || !_currencyData.isRechargeableCurrency || !isRechargeable)
|
|
{
|
|
if (rechargeAmount) rechargeAmount.gameObject.SetActive(false);
|
|
if (nextRechargeTime) nextRechargeTime.gameObject.SetActive(false);
|
|
return;
|
|
}
|
|
|
|
if (rechargeAmount) rechargeAmount.gameObject.SetActive(true);
|
|
if (nextRechargeTime) nextRechargeTime.gameObject.SetActive(true);
|
|
|
|
int current = MonetizationManager.GetCurrency(currencyID);
|
|
|
|
if (_currencyData.useMaxAmount && current >= _currencyData.maxAmount && !_currencyData.canExceedMaxValue)
|
|
{
|
|
if (rechargeAmount) rechargeAmount.text = "";
|
|
if (nextRechargeTime) nextRechargeTime.text = "";
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
|
|
if (rechargeAmount) rechargeAmount.text = $"+{_currencyData.rechargeAmount}";
|
|
}
|
|
|
|
if (nextRechargeTime)
|
|
{
|
|
if (timeLeftForNextCharge <= 0)
|
|
nextRechargeTime.text = "0s";
|
|
else
|
|
nextRechargeTime.text = $"{Mathf.CeilToInt(timeLeftForNextCharge)}s";
|
|
}
|
|
}
|
|
|
|
private float GetSecondsByScale(float timeValue, rechargeableTimeScale scale)
|
|
{
|
|
switch (scale)
|
|
{
|
|
case rechargeableTimeScale.Seconds: return timeValue;
|
|
case rechargeableTimeScale.Minutes: return timeValue * 60f;
|
|
case rechargeableTimeScale.Hours: return timeValue * 3600f;
|
|
default: return timeValue;
|
|
}
|
|
}
|
|
}
|
|
}
|