using System.Collections.Generic; using GeometryTD.CustomUtility; using GeometryTD.Definition; using UnityEngine; namespace GeometryTD.CustomComponent { internal 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 ReduceAllTowerEndurance(float enduranceLoss) { float resolvedLoss = Mathf.Max(0f, enduranceLoss); BackpackInventoryData inventory = _queryModel.Inventory; if (resolvedLoss <= 0f || inventory.Towers == null || inventory.Towers.Count <= 0) { return 0; } Dictionary muzzleMap = BuildComponentMap(inventory.MuzzleComponents); Dictionary bearingMap = BuildComponentMap(inventory.BearingComponents); Dictionary baseMap = BuildComponentMap(inventory.BaseComponents); int affectedCount = 0; for (int i = 0; i < inventory.Towers.Count; i++) { TowerItemData tower = inventory.Towers[i]; if (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 BuildComponentMap(List components) where TComp : TowerCompItemData { Dictionary map = new Dictionary(); 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; } } }