using System.Collections.Generic; using GeometryTD.DataTable; using UnityEngine; namespace GeometryTD.CustomComponent { internal sealed class EnemyLifecycleTracker { private readonly HashSet _aliveBossEntityIds = new(); private readonly HashSet _trackedEnemyEntityIds = new(); private readonly Dictionary _trackedEnemyConfigByEntityId = new(); private readonly Dictionary _bossFlagsByEntityId = new(); public int AliveEnemyCount { get; private set; } public bool HasAliveBoss => _aliveBossEntityIds.Count > 0; public void Reset() { _aliveBossEntityIds.Clear(); _bossFlagsByEntityId.Clear(); _trackedEnemyEntityIds.Clear(); _trackedEnemyConfigByEntityId.Clear(); AliveEnemyCount = 0; } public void TrackEnemy(int entityId, DREnemy enemyConfig, bool isBoss) { _trackedEnemyEntityIds.Add(entityId); _trackedEnemyConfigByEntityId[entityId] = enemyConfig; _bossFlagsByEntityId[entityId] = isBoss; } public bool Contains(int entityId) { return _trackedEnemyEntityIds.Contains(entityId); } public void HandleShowSuccess(int entityId) { if (_trackedEnemyEntityIds.Contains(entityId)) { AliveEnemyCount++; if (_bossFlagsByEntityId.TryGetValue(entityId, out bool isBoss) && isBoss) { _aliveBossEntityIds.Add(entityId); } } } public void HandleShowFailure(int entityId) { _aliveBossEntityIds.Remove(entityId); _bossFlagsByEntityId.Remove(entityId); _trackedEnemyEntityIds.Remove(entityId); _trackedEnemyConfigByEntityId.Remove(entityId); } public bool TryHandleHideComplete(int entityId, out DREnemy enemyConfig) { enemyConfig = null; if (!_trackedEnemyEntityIds.Remove(entityId)) { _aliveBossEntityIds.Remove(entityId); _bossFlagsByEntityId.Remove(entityId); _trackedEnemyConfigByEntityId.Remove(entityId); return false; } _trackedEnemyConfigByEntityId.TryGetValue(entityId, out enemyConfig); _aliveBossEntityIds.Remove(entityId); _bossFlagsByEntityId.Remove(entityId); _trackedEnemyConfigByEntityId.Remove(entityId); AliveEnemyCount = Mathf.Max(0, AliveEnemyCount - 1); return true; } public void CopyTrackedEntityIdsTo(List buffer) { if (buffer == null) { return; } buffer.Clear(); foreach (int trackedEnemyEntityId in _trackedEnemyEntityIds) { buffer.Add(trackedEnemyEntityId); } } } }