3DTD/Assets/Scripts/Manager/MoneyManager.cs

61 lines
1.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Assertions;
public class MoneyManager : MonoBehaviour
{
public TowerCollection towerCollection;
[Space(10)]
public GameObject[] ShopButtons;
public TMP_Text[] MoneyTexts;
/// <summary>
/// Invoked when player selects tower from shop
/// </summary>
public static event Action<TowerInfo> OnShopSelected;
private void OnEnable()
{
for (int i = 0; i < ShopButtons.Length; i++)
{
var eventScript = ShopButtons[i].GetComponentInChildren<UITooltips>();
eventScript.ButtonIndex = i;
eventScript.OnClick += OnShopButtonClicked;
}
}
private void Update()
{
// TODO: move to observer patter idgaf rn
for (int i = 0; i < MoneyTexts.Length; i++)
{
float price = towerCollection.Towers[i].price;
MoneyTexts[i].color = GameManager.Instance.Balance.Value >= price ? Color.green : Color.red;
}
}
private void Start()
{
// Show prices
for (int i = 0; i < towerCollection.Towers.Length; i++)
{
MoneyTexts[i].text = towerCollection.Towers[i].price.ToString();
}
}
private void OnShopButtonClicked(int index)
{
Assert.AreNotEqual(index, -1, "Shop button not init-ed with index");
TowerInfo info = towerCollection.Towers[index];
if (GameManager.Instance.Balance >= info.price)
{
OnShopSelected?.Invoke(info);
}
}
}