添加参战防御塔功能(UI)
This commit is contained in:
@@ -12,6 +12,7 @@ namespace GeometryTD.CustomComponent
|
||||
public class PlayerInventoryComponent : GameFrameworkComponent
|
||||
{
|
||||
private const int TowerLevelCount = 5;
|
||||
private const int MaxParticipantTowerCount = 4;
|
||||
private BackpackInventoryData _inventory = new BackpackInventoryData();
|
||||
private long _nextInstanceId = 1;
|
||||
private bool _initialized;
|
||||
@@ -31,6 +32,7 @@ namespace GeometryTD.CustomComponent
|
||||
public void OnInit()
|
||||
{
|
||||
_inventory = CloneInventory(RepoFormUseCase.SampleInventory());
|
||||
NormalizeParticipantState();
|
||||
RebuildNextInstanceId();
|
||||
_initialized = true;
|
||||
Log.Info(
|
||||
@@ -178,6 +180,55 @@ namespace GeometryTD.CustomComponent
|
||||
_inventory.Gold += resolvedGain;
|
||||
}
|
||||
|
||||
public bool TryAddParticipantTower(long towerInstanceId, int maxCount = 4)
|
||||
{
|
||||
EnsureInitialized();
|
||||
int resolvedMaxCount = Mathf.Max(1, maxCount);
|
||||
resolvedMaxCount = Mathf.Min(resolvedMaxCount, MaxParticipantTowerCount);
|
||||
if (!TryGetTowerById(towerInstanceId, out TowerItemData tower))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_inventory.ParticipantTowerInstanceIds ??= new List<long>();
|
||||
if (_inventory.ParticipantTowerInstanceIds.Contains(towerInstanceId))
|
||||
{
|
||||
tower.IsParticipatingInCombat = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_inventory.ParticipantTowerInstanceIds.Count >= resolvedMaxCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_inventory.ParticipantTowerInstanceIds.Add(towerInstanceId);
|
||||
tower.IsParticipatingInCombat = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryRemoveParticipantTower(long towerInstanceId)
|
||||
{
|
||||
EnsureInitialized();
|
||||
if (towerInstanceId <= 0 || _inventory.ParticipantTowerInstanceIds == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool removed = _inventory.ParticipantTowerInstanceIds.Remove(towerInstanceId);
|
||||
if (!removed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TryGetTowerById(towerInstanceId, out TowerItemData tower))
|
||||
{
|
||||
tower.IsParticipatingInCombat = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryAssembleTower(
|
||||
long muzzleInstanceId,
|
||||
long bearingInstanceId,
|
||||
@@ -337,6 +388,27 @@ namespace GeometryTD.CustomComponent
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryGetTowerById(long towerInstanceId, out TowerItemData tower)
|
||||
{
|
||||
tower = null;
|
||||
if (towerInstanceId <= 0 || _inventory.Towers == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _inventory.Towers.Count; i++)
|
||||
{
|
||||
TowerItemData candidate = _inventory.Towers[i];
|
||||
if (candidate != null && candidate.InstanceId == towerInstanceId)
|
||||
{
|
||||
tower = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryBuildTowerStats(
|
||||
MuzzleCompItemData muzzleComp,
|
||||
BearingCompItemData bearingComp,
|
||||
@@ -555,6 +627,57 @@ namespace GeometryTD.CustomComponent
|
||||
_nextInstanceId = Math.Max(1, maxInstanceId + 1);
|
||||
}
|
||||
|
||||
private void NormalizeParticipantState()
|
||||
{
|
||||
if (_inventory == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_inventory.ParticipantTowerInstanceIds ??= new List<long>();
|
||||
Dictionary<long, TowerItemData> towerMap = new 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;
|
||||
}
|
||||
}
|
||||
|
||||
List<long> normalizedIds = new List<long>(_inventory.ParticipantTowerInstanceIds.Count);
|
||||
HashSet<long> uniqueIds = new HashSet<long>();
|
||||
for (int i = 0; i < _inventory.ParticipantTowerInstanceIds.Count; i++)
|
||||
{
|
||||
if (normalizedIds.Count >= MaxParticipantTowerCount)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
long id = _inventory.ParticipantTowerInstanceIds[i];
|
||||
if (id <= 0 || !uniqueIds.Add(id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!towerMap.TryGetValue(id, out TowerItemData tower))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
tower.IsParticipatingInCombat = true;
|
||||
normalizedIds.Add(id);
|
||||
}
|
||||
|
||||
_inventory.ParticipantTowerInstanceIds = normalizedIds;
|
||||
}
|
||||
|
||||
private static BackpackInventoryData CloneInventory(BackpackInventoryData source)
|
||||
{
|
||||
BackpackInventoryData cloned = new BackpackInventoryData();
|
||||
@@ -613,6 +736,18 @@ namespace GeometryTD.CustomComponent
|
||||
}
|
||||
}
|
||||
|
||||
if (source.ParticipantTowerInstanceIds != null)
|
||||
{
|
||||
for (int i = 0; i < source.ParticipantTowerInstanceIds.Count; i++)
|
||||
{
|
||||
long id = source.ParticipantTowerInstanceIds[i];
|
||||
if (id > 0)
|
||||
{
|
||||
cloned.ParticipantTowerInstanceIds.Add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cloned;
|
||||
}
|
||||
|
||||
@@ -675,6 +810,7 @@ namespace GeometryTD.CustomComponent
|
||||
InstanceId = source.InstanceId,
|
||||
Name = source.Name,
|
||||
Rarity = source.Rarity,
|
||||
IsParticipatingInCombat = source.IsParticipatingInCombat,
|
||||
MuzzleComponentInstanceId = source.MuzzleComponentInstanceId,
|
||||
BearingComponentInstanceId = source.BearingComponentInstanceId,
|
||||
BaseComponentInstanceId = source.BaseComponentInstanceId,
|
||||
@@ -717,4 +853,4 @@ namespace GeometryTD.CustomComponent
|
||||
return source != null ? (TagType[])source.Clone() : Array.Empty<TagType>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user