113 lines
3.3 KiB
C#
113 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using GeometryTD.CustomEvent;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class SellArea : MonoBehaviour
|
|
{
|
|
[SerializeField] private Transform _content;
|
|
[SerializeField] private RepoItem _itemTemplate;
|
|
[SerializeField] private TowerRepoItem _towerItemTemplate;
|
|
[SerializeField] private TMP_Text _totalPrice;
|
|
|
|
private readonly List<RepoItem> _activeComponentItems = new List<RepoItem>();
|
|
private readonly List<TowerRepoItem> _activeTowerItems = new List<TowerRepoItem>();
|
|
private SellAreaContext _context;
|
|
|
|
public void OnInit(SellAreaContext context)
|
|
{
|
|
OnReset();
|
|
_context = context;
|
|
|
|
if (_totalPrice != null)
|
|
{
|
|
_totalPrice.text = context?.TotalPriceText ?? string.Empty;
|
|
}
|
|
|
|
if (context == null || _content == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (context.ComponentItems != null)
|
|
{
|
|
foreach (RepoItemContext itemContext in context.ComponentItems)
|
|
{
|
|
if (itemContext == null || _itemTemplate == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
RepoItem item = Instantiate(_itemTemplate, _content);
|
|
item.gameObject.SetActive(true);
|
|
item.OnInit(itemContext);
|
|
_activeComponentItems.Add(item);
|
|
}
|
|
}
|
|
|
|
if (context.TowerItems != null)
|
|
{
|
|
foreach (TowerRepoItemContext itemContext in context.TowerItems)
|
|
{
|
|
if (itemContext == null || _towerItemTemplate == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
TowerRepoItem item = Instantiate(_towerItemTemplate, _content);
|
|
item.gameObject.SetActive(true);
|
|
item.OnInit(itemContext);
|
|
_activeTowerItems.Add(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
for (int i = _activeComponentItems.Count - 1; i >= 0; i--)
|
|
{
|
|
RepoItem item = _activeComponentItems[i];
|
|
if (item != null)
|
|
{
|
|
Destroy(item.gameObject);
|
|
}
|
|
}
|
|
|
|
for (int i = _activeTowerItems.Count - 1; i >= 0; i--)
|
|
{
|
|
TowerRepoItem item = _activeTowerItems[i];
|
|
if (item != null)
|
|
{
|
|
Destroy(item.gameObject);
|
|
}
|
|
}
|
|
|
|
_activeComponentItems.Clear();
|
|
_activeTowerItems.Clear();
|
|
_context = null;
|
|
|
|
if (_totalPrice != null)
|
|
{
|
|
_totalPrice.text = string.Empty;
|
|
}
|
|
}
|
|
|
|
public void OnCancelButtonClick()
|
|
{
|
|
GameEntry.Event.Fire(this, RepoSellCancelRequestedEventArgs.Create());
|
|
}
|
|
|
|
public void OnConfirmButtonClick()
|
|
{
|
|
if (_context == null || !_context.CanConfirmSell)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.Event.Fire(this, RepoSellConfirmRequestedEventArgs.Create());
|
|
}
|
|
}
|
|
}
|