init
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using GeometryTD.CustomEvent;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class CombatInfoForm : UGuiForm
|
||||
{
|
||||
[SerializeField] private TMP_Text _levelMetaText;
|
||||
|
||||
[SerializeField] private TMP_Text _levelPhaseText;
|
||||
|
||||
[SerializeField] private TMP_Text _coinText;
|
||||
|
||||
[SerializeField] private TMP_Text _baseHpText;
|
||||
|
||||
[SerializeField] private TMP_Text _enemyHpRateText;
|
||||
|
||||
[SerializeField] private CommonButton _pauseButton;
|
||||
|
||||
[SerializeField] private CommonButton _endButton;
|
||||
|
||||
private CombatInfoFormContext _context;
|
||||
|
||||
public void RefreshUI(CombatInfoFormContext context)
|
||||
{
|
||||
_context = context;
|
||||
|
||||
if (_levelMetaText != null)
|
||||
{
|
||||
_levelMetaText.text = context?.LevelMetaText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_levelPhaseText != null)
|
||||
{
|
||||
_levelPhaseText.text = context?.LevelPhaseText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_coinText != null)
|
||||
{
|
||||
_coinText.text = context?.CoinText ?? "0";
|
||||
}
|
||||
|
||||
if (_baseHpText != null)
|
||||
{
|
||||
_baseHpText.text = context?.BaseHpText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_enemyHpRateText != null)
|
||||
{
|
||||
_enemyHpRateText.text = context?.EnemyHpRateText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_pauseButton != null)
|
||||
{
|
||||
_pauseButton.Interactive = context?.CanPause ?? false;
|
||||
}
|
||||
|
||||
if (_endButton != null)
|
||||
{
|
||||
_endButton.gameObject.SetActive(context?.CanEnd ?? false);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPauseButtonClick()
|
||||
{
|
||||
if (_context != null && !_context.CanPause)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Fire(this, CombatPauseEventArgs.Create());
|
||||
}
|
||||
|
||||
public void OnEndButtonClick()
|
||||
{
|
||||
if (_context != null && !_context.CanEnd)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Fire(this, CombatEndEventArgs.Create());
|
||||
}
|
||||
|
||||
public void OnFailButtonClick()
|
||||
{
|
||||
GameEntry.Event.Fire(this, CombatDebugFailEventArgs.Create());
|
||||
}
|
||||
|
||||
protected override void OnOpen(object userData)
|
||||
{
|
||||
base.OnOpen(userData);
|
||||
|
||||
if (userData is CombatInfoFormContext context)
|
||||
{
|
||||
RefreshUI(context);
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Warning("CombatInfoForm requires CombatInfoFormContext as userData.");
|
||||
}
|
||||
|
||||
protected override void OnClose(bool isShutdown, object userData)
|
||||
{
|
||||
_context = null;
|
||||
|
||||
if (_levelMetaText != null)
|
||||
{
|
||||
_levelMetaText.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_levelPhaseText != null)
|
||||
{
|
||||
_levelPhaseText.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_coinText != null)
|
||||
{
|
||||
_coinText.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_baseHpText != null)
|
||||
{
|
||||
_baseHpText.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_enemyHpRateText != null)
|
||||
{
|
||||
_enemyHpRateText.text = string.Empty;
|
||||
}
|
||||
|
||||
base.OnClose(isShutdown, userData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class CombatSelectBuildArea : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TowerSelectItem[] _towerBuildItems;
|
||||
|
||||
public void OnInit(TowerSelectItemContext[] itemContexts)
|
||||
{
|
||||
if (_towerBuildItems == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _towerBuildItems.Length; i++)
|
||||
{
|
||||
TowerSelectItem item = _towerBuildItems[i];
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
TowerSelectItemContext itemContext =
|
||||
itemContexts != null && i < itemContexts.Length ? itemContexts[i] : null;
|
||||
bool isVisible = itemContext != null && itemContext.IsVisible;
|
||||
|
||||
item.gameObject.SetActive(isVisible);
|
||||
if (isVisible)
|
||||
{
|
||||
item.OnInit(itemContext);
|
||||
}
|
||||
else
|
||||
{
|
||||
item.OnReset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
if (_towerBuildItems == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _towerBuildItems.Length; i++)
|
||||
{
|
||||
TowerSelectItem item = _towerBuildItems[i];
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
item.OnReset();
|
||||
item.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class CombatSelectForm : UGuiForm
|
||||
{
|
||||
[SerializeField] private RectTransform _content;
|
||||
|
||||
[SerializeField] private CombatSelectBuildArea _buildArea;
|
||||
|
||||
[SerializeField] private CombatSelectFuncArea _funcArea;
|
||||
|
||||
private CombatSelectFormContext _context;
|
||||
|
||||
public void RefreshUI(CombatSelectFormContext context)
|
||||
{
|
||||
_context = context;
|
||||
EnsureContentReference();
|
||||
|
||||
if (_content == null)
|
||||
{
|
||||
Log.Warning("CombatSelectForm content is missing.");
|
||||
return;
|
||||
}
|
||||
|
||||
bool isVisible = context != null && context.IsVisible;
|
||||
_content.gameObject.SetActive(isVisible);
|
||||
if (!isVisible)
|
||||
{
|
||||
_buildArea?.OnReset();
|
||||
_funcArea?.OnReset();
|
||||
return;
|
||||
}
|
||||
|
||||
_content.anchoredPosition = ResolveContentAnchoredPosition(context.ScreenPosition);
|
||||
|
||||
if (_buildArea != null)
|
||||
{
|
||||
bool showBuildArea = context.ShowBuildArea;
|
||||
_buildArea.gameObject.SetActive(showBuildArea);
|
||||
if (showBuildArea)
|
||||
{
|
||||
_buildArea.OnInit(context.BuildItems);
|
||||
}
|
||||
else
|
||||
{
|
||||
_buildArea.OnReset();
|
||||
}
|
||||
}
|
||||
|
||||
if (_funcArea != null)
|
||||
{
|
||||
bool showFuncArea = context.ShowFuncArea;
|
||||
_funcArea.gameObject.SetActive(showFuncArea);
|
||||
if (showFuncArea)
|
||||
{
|
||||
_funcArea.OnInit(context.UpgradeItem, context.DestroyItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
_funcArea.OnReset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnOpen(object userData)
|
||||
{
|
||||
base.OnOpen(userData);
|
||||
|
||||
if (userData is CombatSelectFormContext context)
|
||||
{
|
||||
RefreshUI(context);
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Warning("CombatSelectForm requires CombatSelectFormContext as userData.");
|
||||
RefreshUI(null);
|
||||
}
|
||||
|
||||
protected override void OnClose(bool isShutdown, object userData)
|
||||
{
|
||||
_context = null;
|
||||
_buildArea?.OnReset();
|
||||
_funcArea?.OnReset();
|
||||
|
||||
if (_content != null)
|
||||
{
|
||||
_content.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
base.OnClose(isShutdown, userData);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
EnsureContentReference();
|
||||
}
|
||||
|
||||
private void EnsureContentReference()
|
||||
{
|
||||
if (_content != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Transform contentTransform = transform.Find("Content");
|
||||
_content = contentTransform as RectTransform;
|
||||
}
|
||||
|
||||
private Vector2 ResolveContentAnchoredPosition(Vector2 screenPosition)
|
||||
{
|
||||
if (_content == null)
|
||||
{
|
||||
return Vector2.zero;
|
||||
}
|
||||
|
||||
RectTransform parentRect = _content.parent as RectTransform;
|
||||
if (parentRect == null)
|
||||
{
|
||||
return screenPosition;
|
||||
}
|
||||
|
||||
Canvas canvas = _content.GetComponentInParent<Canvas>();
|
||||
Canvas rootCanvas = canvas != null ? canvas.rootCanvas : null;
|
||||
Camera uiCamera = null;
|
||||
if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay)
|
||||
{
|
||||
uiCamera = rootCanvas.worldCamera != null ? rootCanvas.worldCamera : Camera.main;
|
||||
}
|
||||
|
||||
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRect, screenPosition, uiCamera,
|
||||
out Vector2 localPoint))
|
||||
{
|
||||
return Vector2.zero;
|
||||
}
|
||||
|
||||
return localPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class CombatSelectFuncArea : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TowerSelectItem _upgradeItem;
|
||||
|
||||
[SerializeField] private TowerSelectItem _destroyItem;
|
||||
|
||||
public void OnInit(TowerSelectItemContext upgradeItemContext, TowerSelectItemContext destroyItemContext)
|
||||
{
|
||||
RefreshItem(_upgradeItem, upgradeItemContext);
|
||||
RefreshItem(_destroyItem, destroyItemContext);
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
ResetItem(_upgradeItem);
|
||||
ResetItem(_destroyItem);
|
||||
}
|
||||
|
||||
private static void RefreshItem(TowerSelectItem item, TowerSelectItemContext context)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool isVisible = context != null && context.IsVisible;
|
||||
item.gameObject.SetActive(isVisible);
|
||||
if (isVisible)
|
||||
{
|
||||
item.OnInit(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
item.OnReset();
|
||||
}
|
||||
}
|
||||
|
||||
private static void ResetItem(TowerSelectItem item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
item.OnReset();
|
||||
item.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using GeometryTD.CustomEvent;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class TowerSelectItem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Image _icon;
|
||||
|
||||
[SerializeField] private TMP_Text _price;
|
||||
|
||||
[SerializeField] private CommonButton _button;
|
||||
|
||||
[SerializeField] private TowerIconArea _towerIconArea;
|
||||
|
||||
private TowerSelectItemContext _context;
|
||||
|
||||
private Sprite _defaultIcon;
|
||||
|
||||
private bool _hasCachedDefaultIcon;
|
||||
|
||||
public void OnInit(TowerSelectItemContext context)
|
||||
{
|
||||
CacheDefaultIcon();
|
||||
_context = context;
|
||||
|
||||
if (context.ActionType == CombatSelectActionType.BuildTower)
|
||||
{
|
||||
_icon?.gameObject.SetActive(false);
|
||||
_towerIconArea?.OnInit(_context.TowerIconAreaContext);
|
||||
|
||||
if (_price != null)
|
||||
{
|
||||
_price.text = context.PriceText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_button != null)
|
||||
{
|
||||
_button.Interactive = context.IsInteractable;
|
||||
}
|
||||
}
|
||||
else if (context.ActionType is CombatSelectActionType.UpgradeTower or CombatSelectActionType.DestroyTower)
|
||||
{
|
||||
if (_icon != null)
|
||||
{
|
||||
_icon.sprite = _context.Icon ?? _defaultIcon;
|
||||
_icon.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
_towerIconArea?.OnReset();
|
||||
|
||||
if (_price != null)
|
||||
{
|
||||
_price.text = context.PriceText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_button != null)
|
||||
{
|
||||
_button.Interactive = context.IsInteractable;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_icon?.gameObject.SetActive(false);
|
||||
_towerIconArea?.OnReset();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
_context = null;
|
||||
CacheDefaultIcon();
|
||||
|
||||
if (_icon != null)
|
||||
{
|
||||
_icon.sprite = _defaultIcon;
|
||||
_icon.enabled = _icon.sprite != null;
|
||||
}
|
||||
|
||||
_towerIconArea.OnReset();
|
||||
|
||||
if (_price != null)
|
||||
{
|
||||
_price.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_button != null)
|
||||
{
|
||||
_button.Interactive = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnClick()
|
||||
{
|
||||
if (_context == null || !_context.IsInteractable)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Fire(
|
||||
this,
|
||||
CombatSelectItemClickEventArgs.Create(_context.ActionType, _context.ActionIndex));
|
||||
}
|
||||
|
||||
private void CacheDefaultIcon()
|
||||
{
|
||||
if (_hasCachedDefaultIcon || _icon == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_defaultIcon = _icon.sprite;
|
||||
_hasCachedDefaultIcon = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user