From ceb11e36f4957db6879ef867b9663cbf7d88ea97 Mon Sep 17 00:00:00 2001 From: ivan tkachenko Date: Tue, 17 Jun 2025 16:10:46 +0300 Subject: [PATCH] organize code into functions, add some comments, add a null check --- MuzikaGromche/Plugin.cs | 97 +++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 37 deletions(-) diff --git a/MuzikaGromche/Plugin.cs b/MuzikaGromche/Plugin.cs index 1bb10c8..63d8e0f 100644 --- a/MuzikaGromche/Plugin.cs +++ b/MuzikaGromche/Plugin.cs @@ -55,6 +55,54 @@ namespace MuzikaGromche public static Coroutine JesterLightSwitching; public static Track CurrentTrack; + public static void StartLightSwitching(MonoBehaviour __instance) + { + StopLightSwitching(__instance); + JesterLightSwitching = __instance.StartCoroutine(rotateColors()); + } + + public static void StopLightSwitching(MonoBehaviour __instance) + { + if (JesterLightSwitching != null) { + __instance.StopCoroutine(JesterLightSwitching); + JesterLightSwitching = null; + } + } + + public static void SetLightColor(Color color) + { + foreach (var light in RoundManager.Instance.allPoweredLights) + { + light.color = color; + } + } + + public static void ResetLightColor() + { + SetLightColor(Color.white); + } + + // TODO: Move to Track class to make them customizable per-song + static List colors = [Color.magenta, Color.cyan, Color.green, Color.yellow]; + + public static IEnumerator rotateColors() + { + Debug.Log("Starting color rotation"); + var i = 0; + while (true) + { + var color = colors[i]; + Debug.Log("Chose color " + color); + SetLightColor(color); + i = (i + 1) % colors.Count; + if (CurrentTrack != null) { + yield return new WaitForSeconds(60f / CurrentTrack.Bpm); + } else { + yield break; + } + } + } + private void Awake() { string text = Info.Location.TrimEnd((PluginInfo.PLUGIN_NAME + ".dll").ToCharArray()); @@ -71,8 +119,9 @@ namespace MuzikaGromche if (requests.All(request => request.result == UnityWebRequest.Result.Success)) { for (int i = 0; i < Tracks.Length; i++) { - Tracks[i].LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]); - Tracks[i].LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]); + Track track = Tracks[i]; + track.LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]); + track.LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]); } new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch)); } else { @@ -84,8 +133,14 @@ namespace MuzikaGromche public class Track { public string Name; + // Wind-up time can be shorter than the Start audio track, so that + // the "pop" effect can be baked in the Start audio and kept away + // from the looped part. public float WindUpTimer; + // BPM for light switching in sync with the music. There is no offset, + // so the Loop track should start precisely on a beat. public float Bpm; + public AudioClip LoadedStart; public AudioClip LoadedLoop; } @@ -107,27 +162,6 @@ namespace MuzikaGromche } } - static List colors = [Color.magenta, Color.cyan, Color.green, Color.yellow]; - - public static IEnumerator rotateColors() - { - Debug.Log("Starting color rotation"); - var i = 0; - while (true) - { - var color = colors[i]; - Debug.Log("Chose color " + color); - foreach (var light in RoundManager.Instance.allPoweredLights) - { - light.color = color; - } - - i += 1; - if (i >= colors.Count) i = 0; - yield return new WaitForSeconds(60f / Plugin.CurrentTrack.Bpm); - } - } - [HarmonyPatch("Update")] [HarmonyPostfix] public static void DoNotStopTheMusic(JesterAI __instance, State __state) @@ -170,24 +204,13 @@ namespace MuzikaGromche if (__instance.currentBehaviourStateIndex is 2 && __state.prevStateindex != 2) { __instance.creatureVoice.Stop(); - - if (Plugin.JesterLightSwitching != null) { - __instance.StopCoroutine(Plugin.JesterLightSwitching); - Plugin.JesterLightSwitching = null; - } - Plugin.JesterLightSwitching = __instance.StartCoroutine(rotateColors()); + Plugin.StartLightSwitching(__instance); } if (__instance.currentBehaviourStateIndex != 2 && __state.prevStateindex == 2) { - if (Plugin.JesterLightSwitching != null) { - __instance.StopCoroutine(Plugin.JesterLightSwitching); - Plugin.JesterLightSwitching = null; - } - foreach (var light in RoundManager.Instance.allPoweredLights) - { - light.color = Color.white; - } + Plugin.StopLightSwitching(__instance); + Plugin.ResetLightColor(); } if (__instance.currentBehaviourStateIndex is 2 && !__instance.creatureVoice.isPlaying)