using System; using System.Collections.Generic; using TMPro; using UnityEngine; public class CurrencySystem : MonoBehaviour { private static Dictionary CurrencyAmounts = new Dictionary(); [SerializeField] private List texts; private Dictionary currencyTexts = new Dictionary(); private void Awake() { for (int i = 0; i < texts.Count; i++) { CurrencyAmounts.Add((CurrencyType)i, 0); currencyTexts.Add((CurrencyType)i, texts[i].transform.GetChild(0).GetComponent()); } } private void Start() { CurrencyAmounts[CurrencyType.Coins] = 100; CurrencyAmounts[CurrencyType.Crystals] = 10; UpdateUI(); EventManager.Instance.AddListener(OnCurrencyChange); EventManager.Instance.AddListener(OnNotEnough); } private void UpdateUI() { for (int i = 0; i < texts.Count; i++) { currencyTexts[(CurrencyType) i].text = CurrencyAmounts[(CurrencyType) i].ToString(); } } private void OnCurrencyChange(CurrencyChangeGameEvent info) { if (info.amount < 0) { if (CurrencyAmounts[info.currencyType] < Math.Abs(info.amount)) { EventManager.Instance.QueueEvent(new NotEnoughCurrencyGameEvent(info.amount, info.currencyType)); return; } EventManager.Instance.QueueEvent(new EnoughCurrencyGameEvent()); } CurrencyAmounts[info.currencyType] += info.amount; currencyTexts[info.currencyType].text = CurrencyAmounts[info.currencyType].ToString(); UpdateUI(); } private void OnNotEnough(NotEnoughCurrencyGameEvent info) { Debug.Log($"You don't have enough of {info.amount} {info.currencyType}"); } } public enum CurrencyType { Coins, Crystals }