108 lines
3.1 KiB
C#
108 lines
3.1 KiB
C#
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using WarGame.Data;
|
|
using WarGame.Managers;
|
|
|
|
namespace WarGame.UI
|
|
{
|
|
public class ShopUI : MonoBehaviour
|
|
{
|
|
[Header("UI References")]
|
|
[SerializeField] private TMP_Text goldDisplayText;
|
|
[SerializeField] private Transform cardsContainer;
|
|
[SerializeField] private GameObject cardPrefab;
|
|
[SerializeField] private GameObject shopPanel;
|
|
|
|
[Header("Shop Catalog")]
|
|
[SerializeField] private List<UnitDataSO> purchasableUnits = new List<UnitDataSO>();
|
|
|
|
private readonly List<ShopCardUI> spawnedCards = new List<ShopCardUI>();
|
|
|
|
private void Start()
|
|
{
|
|
if (BattleManager.Instance != null)
|
|
{
|
|
BattleManager.Instance.OnGoldChanged += HandleGoldChanged;
|
|
BattleManager.Instance.OnBattleStateChanged += HandleBattleStateChanged;
|
|
HandleGoldChanged(BattleManager.Instance.CurrentGold);
|
|
}
|
|
|
|
BuildShopCards();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (BattleManager.Instance != null)
|
|
{
|
|
BattleManager.Instance.OnGoldChanged -= HandleGoldChanged;
|
|
BattleManager.Instance.OnBattleStateChanged -= HandleBattleStateChanged;
|
|
}
|
|
}
|
|
|
|
public void SetCatalog(List<UnitDataSO> units)
|
|
{
|
|
purchasableUnits = units;
|
|
BuildShopCards();
|
|
}
|
|
|
|
private void BuildShopCards()
|
|
{
|
|
if (cardsContainer == null || cardPrefab == null) return;
|
|
|
|
// Clear previous
|
|
foreach (Transform child in cardsContainer)
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
spawnedCards.Clear();
|
|
|
|
for (int i = 0; i < purchasableUnits.Count; i++)
|
|
{
|
|
UnitDataSO data = purchasableUnits[i];
|
|
if (data == null) continue;
|
|
|
|
GameObject cardObj = Instantiate(cardPrefab, cardsContainer);
|
|
ShopCardUI card = cardObj.GetComponent<ShopCardUI>();
|
|
if (card != null)
|
|
{
|
|
card.Setup(data);
|
|
spawnedCards.Add(card);
|
|
}
|
|
}
|
|
|
|
UpdateAllCards();
|
|
}
|
|
|
|
private void HandleGoldChanged(int newGold)
|
|
{
|
|
if (goldDisplayText != null)
|
|
{
|
|
goldDisplayText.text = $"Gold: {newGold}";
|
|
}
|
|
UpdateAllCards();
|
|
}
|
|
|
|
private void HandleBattleStateChanged(BattleState newState)
|
|
{
|
|
if (shopPanel != null)
|
|
{
|
|
// Only allow recruiting during Preparation phase
|
|
shopPanel.SetActive(newState == BattleState.Preparation);
|
|
}
|
|
UpdateAllCards();
|
|
}
|
|
|
|
private void UpdateAllCards()
|
|
{
|
|
for (int i = 0; i < spawnedCards.Count; i++)
|
|
{
|
|
if (spawnedCards[i] != null)
|
|
{
|
|
spawnedCards[i].UpdateInteractability();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|