137 lines
4.9 KiB
C#
137 lines
4.9 KiB
C#
using System.Collections.Generic;
|
|
using GeometryTD.CustomUtility;
|
|
using GeometryTD.Definition;
|
|
using UnityEngine;
|
|
|
|
namespace GeometryTD.CustomComponent
|
|
{
|
|
public sealed class PlayerInventoryTowerRosterService
|
|
{
|
|
private readonly PlayerInventoryQueryModel _queryModel;
|
|
private readonly int _maxParticipantTowerCount;
|
|
|
|
public PlayerInventoryTowerRosterService(PlayerInventoryQueryModel queryModel, int maxParticipantTowerCount)
|
|
{
|
|
_queryModel = queryModel;
|
|
_maxParticipantTowerCount = Mathf.Max(1, maxParticipantTowerCount);
|
|
}
|
|
|
|
public ParticipantTowerAssignResult TryAddParticipantTower(long towerInstanceId, int maxCount)
|
|
{
|
|
int resolvedMaxCount = Mathf.Max(1, maxCount);
|
|
resolvedMaxCount = Mathf.Min(resolvedMaxCount, _maxParticipantTowerCount);
|
|
return InventoryParticipantUtility.TryAddParticipantTower(
|
|
_queryModel.Inventory,
|
|
towerInstanceId,
|
|
resolvedMaxCount);
|
|
}
|
|
|
|
public bool TryRemoveParticipantTower(long towerInstanceId)
|
|
{
|
|
return InventoryParticipantUtility.TryRemoveParticipantTower(
|
|
_queryModel.Inventory,
|
|
towerInstanceId,
|
|
_maxParticipantTowerCount);
|
|
}
|
|
|
|
public int ReduceTowerEndurance(IReadOnlyList<long> towerInstanceIds, float enduranceLoss)
|
|
{
|
|
float resolvedLoss = Mathf.Max(0f, enduranceLoss);
|
|
BackpackInventoryData inventory = _queryModel.Inventory;
|
|
if (resolvedLoss <= 0f ||
|
|
towerInstanceIds == null ||
|
|
towerInstanceIds.Count <= 0 ||
|
|
inventory.Towers == null ||
|
|
inventory.Towers.Count <= 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
Dictionary<long, MuzzleCompItemData> muzzleMap = BuildComponentMap(inventory.MuzzleComponents);
|
|
Dictionary<long, BearingCompItemData> bearingMap = BuildComponentMap(inventory.BearingComponents);
|
|
Dictionary<long, BaseCompItemData> baseMap = BuildComponentMap(inventory.BaseComponents);
|
|
HashSet<long> processedTowerIds = new HashSet<long>();
|
|
|
|
int affectedCount = 0;
|
|
for (int i = 0; i < towerInstanceIds.Count; i++)
|
|
{
|
|
long towerInstanceId = towerInstanceIds[i];
|
|
if (towerInstanceId <= 0 || !processedTowerIds.Add(towerInstanceId))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!InventoryParticipantUtility.TryGetTowerById(inventory, towerInstanceId, out TowerItemData tower) ||
|
|
tower == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
bool towerAffected = false;
|
|
if (muzzleMap.TryGetValue(tower.MuzzleComponentInstanceId, out MuzzleCompItemData muzzleComp))
|
|
{
|
|
towerAffected |= TryReduceComponentEndurance(muzzleComp, resolvedLoss);
|
|
}
|
|
|
|
if (bearingMap.TryGetValue(tower.BearingComponentInstanceId, out BearingCompItemData bearingComp))
|
|
{
|
|
towerAffected |= TryReduceComponentEndurance(bearingComp, resolvedLoss);
|
|
}
|
|
|
|
if (baseMap.TryGetValue(tower.BaseComponentInstanceId, out BaseCompItemData baseComp))
|
|
{
|
|
towerAffected |= TryReduceComponentEndurance(baseComp, resolvedLoss);
|
|
}
|
|
|
|
if (towerAffected)
|
|
{
|
|
affectedCount++;
|
|
}
|
|
}
|
|
|
|
return affectedCount;
|
|
}
|
|
|
|
private static bool TryReduceComponentEndurance(TowerCompItemData component, float enduranceLoss)
|
|
{
|
|
if (component == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
float originalEndurance = component.Endurance;
|
|
float nextEndurance = Mathf.Clamp(originalEndurance - Mathf.Max(0f, enduranceLoss), 0f, 100f);
|
|
if (nextEndurance >= originalEndurance)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
component.Endurance = nextEndurance;
|
|
return true;
|
|
}
|
|
|
|
private static Dictionary<long, TComp> BuildComponentMap<TComp>(List<TComp> components)
|
|
where TComp : TowerCompItemData
|
|
{
|
|
Dictionary<long, TComp> map = new Dictionary<long, TComp>();
|
|
if (components == null || components.Count <= 0)
|
|
{
|
|
return map;
|
|
}
|
|
|
|
for (int i = 0; i < components.Count; i++)
|
|
{
|
|
TComp component = components[i];
|
|
if (component == null || component.InstanceId <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
map[component.InstanceId] = component;
|
|
}
|
|
|
|
return map;
|
|
}
|
|
}
|
|
}
|