private static void SettleIntegrityForMaterial(GameObject location, WearNTear.MaterialType material) { Dictionary <WearNTear, float> remainingObjects = InitSettlingObjects(location, material); int numTotalObjectsDeleted = 0; var objectsToRemoveInIteration = new HashSet <WearNTear>(); var objectsToDelete = new HashSet <WearNTear>(); while (remainingObjects.Count > 0) { objectsToRemoveInIteration.Clear(); foreach (var wearAndOldSupport in remainingObjects) { UpdateSupport(wearAndOldSupport.Key); } foreach (var wearAndOldSupport in remainingObjects) { WearNTear wear = wearAndOldSupport.Key; float oldSupport = wearAndOldSupport.Value; UpdateSupport(wear); float newSupport = wear.m_support; if (newSupport == oldSupport) { objectsToRemoveInIteration.Add(wear); } else { remainingObjects[wear] = newSupport; } if (!wear.HaveSupport()) { objectsToDelete.Add(wear); } } foreach (var removeMe in objectsToRemoveInIteration) { remainingObjects.Remove(removeMe); } foreach (var deleteMe in objectsToDelete) { remainingObjects.Remove(deleteMe); UnityEngine.Object.DestroyImmediate(deleteMe.gameObject); } numTotalObjectsDeleted += objectsToDelete.Count; } if (numTotalObjectsDeleted > 0) { Jotunn.Logger.LogInfo($"Settling {location.name} - Deleted {numTotalObjectsDeleted} objects in {material} material pass"); } }
private static Dictionary <WearNTear, float> InitSettlingObjects(GameObject location, WearNTear.MaterialType material) { var result = new Dictionary <WearNTear, float>(); var integrityObjects = location.GetComponentsInChildren <WearNTear>(includeInactive: true); foreach (WearNTear wear in integrityObjects) { if (wear.m_materialType != material) { continue; } UpdateSupport(wear); float support = wear.m_support; if (support < wear.GetMaxSupport()) { result.Add(wear, support); } } return(result); }