1
0
Fork 0

organize code into functions, add some comments, add a null check

This commit is contained in:
ivan tkachenko 2025-06-17 16:10:46 +03:00
parent 566bc0993e
commit ceb11e36f4
1 changed files with 60 additions and 37 deletions

View File

@ -55,6 +55,54 @@ namespace MuzikaGromche
public static Coroutine JesterLightSwitching; public static Coroutine JesterLightSwitching;
public static Track CurrentTrack; 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<Color> 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() private void Awake()
{ {
string text = Info.Location.TrimEnd((PluginInfo.PLUGIN_NAME + ".dll").ToCharArray()); 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)) { if (requests.All(request => request.result == UnityWebRequest.Result.Success)) {
for (int i = 0; i < Tracks.Length; i++) { for (int i = 0; i < Tracks.Length; i++) {
Tracks[i].LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]); Track track = Tracks[i];
Tracks[i].LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]); track.LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]);
track.LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]);
} }
new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch)); new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch));
} else { } else {
@ -84,8 +133,14 @@ namespace MuzikaGromche
public class Track public class Track
{ {
public string Name; 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; 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 float Bpm;
public AudioClip LoadedStart; public AudioClip LoadedStart;
public AudioClip LoadedLoop; public AudioClip LoadedLoop;
} }
@ -107,27 +162,6 @@ namespace MuzikaGromche
} }
} }
static List<Color> 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")] [HarmonyPatch("Update")]
[HarmonyPostfix] [HarmonyPostfix]
public static void DoNotStopTheMusic(JesterAI __instance, State __state) public static void DoNotStopTheMusic(JesterAI __instance, State __state)
@ -170,24 +204,13 @@ namespace MuzikaGromche
if (__instance.currentBehaviourStateIndex is 2 && __state.prevStateindex != 2) if (__instance.currentBehaviourStateIndex is 2 && __state.prevStateindex != 2)
{ {
__instance.creatureVoice.Stop(); __instance.creatureVoice.Stop();
Plugin.StartLightSwitching(__instance);
if (Plugin.JesterLightSwitching != null) {
__instance.StopCoroutine(Plugin.JesterLightSwitching);
Plugin.JesterLightSwitching = null;
}
Plugin.JesterLightSwitching = __instance.StartCoroutine(rotateColors());
} }
if (__instance.currentBehaviourStateIndex != 2 && __state.prevStateindex == 2) if (__instance.currentBehaviourStateIndex != 2 && __state.prevStateindex == 2)
{ {
if (Plugin.JesterLightSwitching != null) { Plugin.StopLightSwitching(__instance);
__instance.StopCoroutine(Plugin.JesterLightSwitching); Plugin.ResetLightColor();
Plugin.JesterLightSwitching = null;
}
foreach (var light in RoundManager.Instance.allPoweredLights)
{
light.color = Color.white;
}
} }
if (__instance.currentBehaviourStateIndex is 2 && !__instance.creatureVoice.isPlaying) if (__instance.currentBehaviourStateIndex is 2 && !__instance.creatureVoice.isPlaying)