174 lines
4.7 KiB
C#
174 lines
4.7 KiB
C#
using System.Collections.Generic;
|
|
using GameFramework.ObjectPool;
|
|
using GeometryTD.CustomEvent;
|
|
using GeometryTD.PoolObjectBase;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class CombatFinishForm : UGuiForm
|
|
{
|
|
[SerializeField] private TMP_Text _enemyKilledText;
|
|
[SerializeField] private TMP_Text _goldGainedText;
|
|
[SerializeField] private Transform _itemGainedParent;
|
|
[SerializeField] private RepoItem _itemTemplate;
|
|
[SerializeField] private int _instancePoolCapacity = 16;
|
|
|
|
private readonly List<RepoItem> _activeItems = new List<RepoItem>();
|
|
private IObjectPool<RepoItemObject> _itemPool;
|
|
private string _poolName;
|
|
private CombatFinishFormContext _context;
|
|
|
|
public void RefreshUI(CombatFinishFormContext context)
|
|
{
|
|
_context = context;
|
|
|
|
if (_enemyKilledText != null)
|
|
{
|
|
_enemyKilledText.text = context?.EnemyKilledText ?? "0";
|
|
}
|
|
|
|
if (_goldGainedText != null)
|
|
{
|
|
_goldGainedText.text = context?.GoldGainedText ?? "0";
|
|
}
|
|
|
|
RefreshRewardItems(context?.RewardItems);
|
|
}
|
|
|
|
public void OnReturnButtonClick()
|
|
{
|
|
if (_context != null && !_context.CanReturn)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.Event.Fire(this, CombatFinishReturnEventArgs.Create());
|
|
}
|
|
|
|
protected override void OnOpen(object userData)
|
|
{
|
|
base.OnOpen(userData);
|
|
|
|
if (userData is CombatFinishFormContext context)
|
|
{
|
|
RefreshUI(context);
|
|
return;
|
|
}
|
|
|
|
Log.Warning("CombatFinishForm requires CombatFinishFormContext as userData.");
|
|
}
|
|
|
|
protected override void OnClose(bool isShutdown, object userData)
|
|
{
|
|
_context = null;
|
|
|
|
if (_enemyKilledText != null)
|
|
{
|
|
_enemyKilledText.text = string.Empty;
|
|
}
|
|
|
|
if (_goldGainedText != null)
|
|
{
|
|
_goldGainedText.text = string.Empty;
|
|
}
|
|
|
|
ClearRewardItems();
|
|
base.OnClose(isShutdown, userData);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
ClearRewardItems();
|
|
_itemPool?.ReleaseAllUnused();
|
|
}
|
|
|
|
private void EnsurePool()
|
|
{
|
|
if (_itemPool != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_poolName = $"CombatFinishRepoItemPool_{GetInstanceID()}";
|
|
_itemPool = GameEntry.ObjectPool.CreateSingleSpawnObjectPool<RepoItemObject>(_poolName, _instancePoolCapacity);
|
|
}
|
|
|
|
private void RefreshRewardItems(RepoItemContext[] rewardItems)
|
|
{
|
|
ClearRewardItems();
|
|
|
|
if (rewardItems == null || rewardItems.Length <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
EnsurePool();
|
|
|
|
for (int i = 0; i < rewardItems.Length; i++)
|
|
{
|
|
RepoItem item = CreateOrSpawnItem();
|
|
if (item == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
item.gameObject.SetActive(true);
|
|
item.OnInit(rewardItems[i]);
|
|
_activeItems.Add(item);
|
|
}
|
|
}
|
|
|
|
private void ClearRewardItems()
|
|
{
|
|
for (int i = _activeItems.Count - 1; i >= 0; i--)
|
|
{
|
|
RepoItem item = _activeItems[i];
|
|
if (item == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
item.OnReset();
|
|
item.gameObject.SetActive(false);
|
|
_itemPool?.Unspawn(item);
|
|
}
|
|
|
|
_activeItems.Clear();
|
|
}
|
|
|
|
private RepoItem CreateOrSpawnItem()
|
|
{
|
|
if (_itemGainedParent == null)
|
|
{
|
|
Log.Warning("CombatFinishForm requires an item gained parent.");
|
|
return null;
|
|
}
|
|
|
|
if (_itemPool == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
RepoItemObject itemObject = _itemPool.Spawn();
|
|
RepoItem item = itemObject != null ? (RepoItem)itemObject.Target : null;
|
|
if (item == null)
|
|
{
|
|
if (_itemTemplate == null)
|
|
{
|
|
Log.Warning("CombatFinishForm requires an item template.");
|
|
return null;
|
|
}
|
|
|
|
item = Instantiate(_itemTemplate);
|
|
_itemPool.Register(RepoItemObject.Create(item), true);
|
|
}
|
|
|
|
item.transform.SetParent(_itemGainedParent, false);
|
|
return item;
|
|
}
|
|
}
|
|
}
|