using System.Collections.Generic; using RimWorld; using Verse; namespace WulaFallenEmpire { public static class AreaShieldManager { private static Dictionary> activeShieldsByMap = new Dictionary>(); private static int lastUpdateTick = 0; private const int UPDATE_INTERVAL_TICKS = 60; public static IEnumerable GetActiveShieldsForMap(Map map) { if (map == null) yield break; if (Find.TickManager.TicksGame - lastUpdateTick > UPDATE_INTERVAL_TICKS) { UpdateShieldCache(); lastUpdateTick = Find.TickManager.TicksGame; } if (activeShieldsByMap.TryGetValue(map, out var shields)) { foreach (var shield in shields) { if (shield?.parent != null && !shield.parent.Destroyed && shield?.Active == true) yield return shield; } } } private static void UpdateShieldCache() { activeShieldsByMap.Clear(); foreach (var map in Find.Maps) { if (map == null) continue; var shieldSet = new HashSet(); // 搜索装备上的护盾 foreach (var pawn in map.mapPawns.AllPawnsSpawned) { if (pawn?.apparel == null || pawn.Destroyed) continue; foreach (var apparel in pawn.apparel.WornApparel) { if (apparel == null || apparel.Destroyed) continue; var shield = apparel.TryGetComp(); // 修改:只有立定且激活的护盾才加入缓存 if (shield != null && shield.Active && !shield.IsHolderMoving) { shieldSet.Add(shield); } } } // 搜索固定物品上的护盾(新增) foreach (var thing in map.listerThings.AllThings) { if (thing == null || thing.Destroyed || thing is Apparel) continue; var shield = thing.TryGetComp(); if (shield != null && shield.Active) { shieldSet.Add(shield); } } activeShieldsByMap[map] = shieldSet; } } public static void NotifyShieldStateChanged(ThingComp_AreaShield shield) { if (shield?.Holder?.Map != null) { lastUpdateTick = 0; } } public static void Cleanup() { var mapsToRemove = new List(); foreach (var map in activeShieldsByMap.Keys) { if (map == null || !Find.Maps.Contains(map)) mapsToRemove.Add(map); } foreach (var map in mapsToRemove) { activeShieldsByMap.Remove(map); } } } }