141 lines
4.5 KiB
C#
141 lines
4.5 KiB
C#
using System.Collections.Generic;
|
|
using GeometryTD.Definition;
|
|
using UnityEngine;
|
|
|
|
namespace GeometryTD.CustomUtility
|
|
{
|
|
public static class InventoryParticipantUtility
|
|
{
|
|
public static bool TryAddParticipantTower(BackpackInventoryData inventory, long towerInstanceId, int maxCount)
|
|
{
|
|
if (inventory == null || towerInstanceId <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int resolvedMaxCount = Mathf.Max(1, maxCount);
|
|
NormalizeParticipantState(inventory, resolvedMaxCount);
|
|
if (!TryGetTowerById(inventory, 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 static bool TryRemoveParticipantTower(BackpackInventoryData inventory, long towerInstanceId, int maxCount)
|
|
{
|
|
if (inventory == null || towerInstanceId <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
NormalizeParticipantState(inventory, maxCount);
|
|
if (inventory.ParticipantTowerInstanceIds == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool removed = inventory.ParticipantTowerInstanceIds.Remove(towerInstanceId);
|
|
if (!removed)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (TryGetTowerById(inventory, towerInstanceId, out TowerItemData tower))
|
|
{
|
|
tower.IsParticipatingInCombat = false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static void NormalizeParticipantState(BackpackInventoryData inventory, int maxCount)
|
|
{
|
|
if (inventory == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
int resolvedMaxCount = Mathf.Max(1, maxCount);
|
|
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;
|
|
}
|
|
}
|
|
|
|
HashSet<long> uniqueIds = new HashSet<long>();
|
|
List<long> normalizedIds = new List<long>(inventory.ParticipantTowerInstanceIds.Count);
|
|
for (int i = 0; i < inventory.ParticipantTowerInstanceIds.Count; i++)
|
|
{
|
|
if (normalizedIds.Count >= resolvedMaxCount)
|
|
{
|
|
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 bool TryGetTowerById(BackpackInventoryData inventory, long towerInstanceId, out TowerItemData tower)
|
|
{
|
|
tower = null;
|
|
if (inventory?.Towers == null || towerInstanceId <= 0)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|