CombatNode 相关逻辑补充
- CombatFinishForm:战斗节点的结算页面(杀死的敌人、获得的金币、获得的道具) - CombatSelectForm:战斗节点建造防御塔的选择 UI 以及对防御塔进行操作的 UI(只实现了建造逻辑,在 MapEntity 里做射线检测) - 构建了基本的防御塔三组件(ShooterMuzzleComp/BasicBearingComp/BasicBaseComp)和防御塔主控(DefenseTowerController)以及相应的实体类(DefenseTowerEntity)
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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9679d912cb0ce9e4d95db41021d94463
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73470d4b91d03924b9233b58311e917c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,111 @@
|
||||
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 = context.ContentPosition;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e62b0f7fcf3ccfd4e88da34d80cc14ab
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 891304d1e879da34c9b989ca2fcd14a7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,96 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using GeometryTD.CustomEvent;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class TowerSelectItem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Image _icon;
|
||||
|
||||
[SerializeField] private TMP_Text _price;
|
||||
|
||||
[SerializeField] private CommonButton _button;
|
||||
|
||||
private TowerSelectItemContext _context;
|
||||
private Sprite _defaultIcon;
|
||||
private bool _hasCachedDefaultIcon;
|
||||
|
||||
public void OnInit(TowerSelectItemContext context)
|
||||
{
|
||||
CacheDefaultIcon();
|
||||
_context = context;
|
||||
|
||||
if (_icon != null)
|
||||
{
|
||||
if (context != null && context.Icon != null)
|
||||
{
|
||||
_icon.sprite = context.Icon;
|
||||
}
|
||||
else if (_defaultIcon != null)
|
||||
{
|
||||
_icon.sprite = _defaultIcon;
|
||||
}
|
||||
|
||||
_icon.enabled = _icon.sprite != null;
|
||||
}
|
||||
|
||||
if (_price != null)
|
||||
{
|
||||
_price.text = context?.PriceText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_button != null)
|
||||
{
|
||||
_button.Interactive = context != null && context.IsInteractable;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
_context = null;
|
||||
CacheDefaultIcon();
|
||||
|
||||
if (_icon != null)
|
||||
{
|
||||
_icon.sprite = _defaultIcon;
|
||||
_icon.enabled = _icon.sprite != null;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4da5ef45ee18db44ebe3c015dddb61d6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user