diff --git a/CHANGELOG.md b/CHANGELOG.md index fe98193..e3fa9a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## MuzikaGromche 13.37.9001 - Fixed more missing flickering behaviours for some animators controllers. +- Fixed some powered lights not fully turning off or flickering when there are multiple Light components per container. ## MuzikaGromche 13.37.1337 - Photosensitivity Warning Edition diff --git a/MuzikaGromche/Plugin.cs b/MuzikaGromche/Plugin.cs index 1031202..135ca88 100644 --- a/MuzikaGromche/Plugin.cs +++ b/MuzikaGromche/Plugin.cs @@ -543,6 +543,7 @@ namespace MuzikaGromche harmony.PatchAll(typeof(JesterPatch)); harmony.PatchAll(typeof(EnemyAIPatch)); harmony.PatchAll(typeof(PoweredLightsAnimatorsPatch)); + harmony.PatchAll(typeof(AllPoweredLightsPatch)); } else { diff --git a/MuzikaGromche/PoweredLightsAnimators.cs b/MuzikaGromche/PoweredLightsAnimators.cs index c8093dd..ae280df 100644 --- a/MuzikaGromche/PoweredLightsAnimators.cs +++ b/MuzikaGromche/PoweredLightsAnimators.cs @@ -144,4 +144,54 @@ namespace MuzikaGromche PoweredLightsAnimators.Patch(__instance); } } + + [HarmonyPatch(typeof(RoundManager))] + static class AllPoweredLightsPatch + { + // Vanilla method assumes that GameObjects with tag "PoweredLight" only contain a single Light component each. + // This is, however, not true for certains double-light setups, such as: + // - double PointLight (even though one of them is 'Point' another is 'Spot') inside CeilingFanAnimContainer in BedroomTile/BedroomTileB; + // - MineshaftSpotlight when it has not only `Point Light` but also `IndirectLight` in BirthdayRoomTile; + // - (maybe more?) + // In order to fix that, replace singular GetComponentInChildren with plural GetComponentsInChildren version. + [HarmonyPatch(nameof(RoundManager.RefreshLightsList))] + [HarmonyPrefix] + private static bool OnRefreshLightsList(RoundManager __instance) + { + RefreshLightsListPatched(__instance); + // Skip the original method + return false; + } + + private static void RefreshLightsListPatched(RoundManager self) + { + // Reusable list to reduce allocations + List lights = []; + + self.allPoweredLights.Clear(); + self.allPoweredLightsAnimators.Clear(); + + GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("PoweredLight"); + if (gameObjects == null) + { + return; + } + + foreach (var gameObject in gameObjects) + { + Animator animator = gameObject.GetComponentInChildren(); + if (!(animator == null)) + { + self.allPoweredLightsAnimators.Add(animator); + // Patched section: Use list instead of singular GetComponentInChildren + gameObject.GetComponentsInChildren(includeInactive: true, lights); + self.allPoweredLights.AddRange(lights); + } + } + foreach (var animator in self.allPoweredLightsAnimators) + { + animator.SetFloat("flickerSpeed", UnityEngine.Random.Range(0.6f, 1.4f)); + } + } + } }