添加参战防御塔功能(UI)

This commit is contained in:
SepComet
2026-03-04 20:36:05 +08:00
parent 298345fa17
commit a62309e8c1
25 changed files with 1160 additions and 20 deletions
@@ -4,11 +4,14 @@ namespace GeometryTD.UI
{
public class RepoFormUseCase : IUIUseCase
{
private const int MaxParticipantCount = 4;
private BackpackInventoryData _fallbackInventory;
public RepoFormRawData CreateInitialModel()
{
BackpackInventoryData sample = GameEntry.PlayerInventory != null
? GameEntry.PlayerInventory.GetInventorySnapshot()
: SampleInventory();
: GetOrCreateFallbackInventory();
return new RepoFormRawData
{
Inventory = sample
@@ -29,6 +32,169 @@ namespace GeometryTD.UI
out _);
}
public bool TryAddParticipantTower(long towerItemId)
{
if (GameEntry.PlayerInventory == null)
{
BackpackInventoryData fallbackInventory = GetOrCreateFallbackInventory();
return TryAddParticipantTowerInternal(fallbackInventory, towerItemId, MaxParticipantCount);
}
return GameEntry.PlayerInventory.TryAddParticipantTower(towerItemId, 4);
}
public bool TryRemoveParticipantTower(long towerItemId)
{
if (GameEntry.PlayerInventory == null)
{
BackpackInventoryData fallbackInventory = GetOrCreateFallbackInventory();
return TryRemoveParticipantTowerInternal(fallbackInventory, towerItemId);
}
return GameEntry.PlayerInventory.TryRemoveParticipantTower(towerItemId);
}
private BackpackInventoryData GetOrCreateFallbackInventory()
{
_fallbackInventory ??= SampleInventory();
NormalizeParticipantState(_fallbackInventory);
return _fallbackInventory;
}
private static bool TryAddParticipantTowerInternal(
BackpackInventoryData inventory,
long towerItemId,
int maxCount)
{
if (inventory == null || towerItemId <= 0)
{
return false;
}
NormalizeParticipantState(inventory);
if (!TryGetTowerById(inventory, towerItemId, out TowerItemData tower))
{
return false;
}
inventory.ParticipantTowerInstanceIds ??= new System.Collections.Generic.List<long>();
if (inventory.ParticipantTowerInstanceIds.Contains(towerItemId))
{
tower.IsParticipatingInCombat = true;
return false;
}
if (inventory.ParticipantTowerInstanceIds.Count >= UnityEngine.Mathf.Max(1, maxCount))
{
return false;
}
inventory.ParticipantTowerInstanceIds.Add(towerItemId);
tower.IsParticipatingInCombat = true;
return true;
}
private static bool TryRemoveParticipantTowerInternal(BackpackInventoryData inventory, long towerItemId)
{
if (inventory == null || towerItemId <= 0)
{
return false;
}
NormalizeParticipantState(inventory);
if (inventory.ParticipantTowerInstanceIds == null)
{
return false;
}
bool removed = inventory.ParticipantTowerInstanceIds.Remove(towerItemId);
if (!removed)
{
return false;
}
if (TryGetTowerById(inventory, towerItemId, out TowerItemData tower))
{
tower.IsParticipatingInCombat = false;
}
return true;
}
private static bool TryGetTowerById(BackpackInventoryData inventory, long towerItemId, out TowerItemData tower)
{
tower = null;
if (inventory?.Towers == null || towerItemId <= 0)
{
return false;
}
for (int i = 0; i < inventory.Towers.Count; i++)
{
TowerItemData candidate = inventory.Towers[i];
if (candidate != null && candidate.InstanceId == towerItemId)
{
tower = candidate;
return true;
}
}
return false;
}
private static void NormalizeParticipantState(BackpackInventoryData inventory)
{
if (inventory == null)
{
return;
}
inventory.ParticipantTowerInstanceIds ??= new System.Collections.Generic.List<long>();
System.Collections.Generic.Dictionary<long, TowerItemData> towerMap =
new System.Collections.Generic.Dictionary<long, TowerItemData>();
if (inventory.Towers != null)
{
for (int i = 0; i < inventory.Towers.Count; i++)
{
TowerItemData tower = inventory.Towers[i];
if (tower == null || tower.InstanceId <= 0)
{
continue;
}
tower.IsParticipatingInCombat = false;
towerMap[tower.InstanceId] = tower;
}
}
System.Collections.Generic.HashSet<long> uniqueIds = new System.Collections.Generic.HashSet<long>();
System.Collections.Generic.List<long> normalizedIds =
new System.Collections.Generic.List<long>(inventory.ParticipantTowerInstanceIds.Count);
for (int i = 0; i < inventory.ParticipantTowerInstanceIds.Count; i++)
{
if (normalizedIds.Count >= MaxParticipantCount)
{
break;
}
long towerId = inventory.ParticipantTowerInstanceIds[i];
if (towerId <= 0 || !uniqueIds.Add(towerId))
{
continue;
}
if (!towerMap.TryGetValue(towerId, out TowerItemData tower))
{
continue;
}
tower.IsParticipatingInCombat = true;
normalizedIds.Add(towerId);
}
inventory.ParticipantTowerInstanceIds = normalizedIds;
}
public static BackpackInventoryData SampleInventory()
{
BackpackInventoryData inventory = new BackpackInventoryData
@@ -167,6 +333,7 @@ namespace GeometryTD.UI
InstanceId = 90001,
Name = "测试防御塔-A",
Rarity = RarityType.Green,
IsParticipatingInCombat = true,
MuzzleComponentInstanceId = muzzle.InstanceId,
BearingComponentInstanceId = bearing.InstanceId,
BaseComponentInstanceId = baseComp.InstanceId,
@@ -189,6 +356,7 @@ namespace GeometryTD.UI
InstanceId = 90002,
Name = "测试防御塔-B",
Rarity = RarityType.Blue,
IsParticipatingInCombat = false,
MuzzleComponentInstanceId = 0,
BearingComponentInstanceId = 0,
BaseComponentInstanceId = 0,
@@ -205,6 +373,8 @@ namespace GeometryTD.UI
}
});
inventory.ParticipantTowerInstanceIds.Add(90001);
return inventory;
}
}