support multiple tracks

This commit is contained in:
Nikita Vilunov 2024-03-10 03:19:17 +01:00
parent 19a4cbff68
commit 7e0ba2248f
1 changed files with 52 additions and 36 deletions

View File

@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using BepInEx; using BepInEx;
using HarmonyLib; using HarmonyLib;
using UnityEngine; using UnityEngine;
@ -10,58 +11,68 @@ namespace MuzikaGromche
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin public class Plugin : BaseUnityPlugin
{ {
public static AudioClip AudioStart; public static Track[] Tracks = [
public static AudioClip AudioLoop; new Track
{
Name = "MuzikaGromche",
WindUpTimer = 46.3f,
Bpm = 120f,
},
new Track
{
Name = "VseVZale",
WindUpTimer = 39f,
Bpm = 138f,
}
];
public static Coroutine JesterLightSwitching; public static Coroutine JesterLightSwitching;
public static Track CurrentTrack;
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());
UnityWebRequest audioClipStart = UnityWebRequestMultimedia.GetAudioClip("File://" + text + "MuzikaGromcheStart.mp3", AudioType.MPEG); UnityWebRequest[] requests = new UnityWebRequest[Tracks.Length * 2];
UnityWebRequest audioClipLoop = UnityWebRequestMultimedia.GetAudioClip("File://" + text + "MuzikaGromcheLoop.mp3", AudioType.MPEG); for (int i = 0; i < Tracks.Length; i++) {
audioClipStart.SendWebRequest(); Track track = Tracks[i];
audioClipLoop.SendWebRequest(); requests[i * 2] = UnityWebRequestMultimedia.GetAudioClip($"File://{text}{track.Name}Start.mp3", AudioType.MPEG);
while (!audioClipStart.isDone || !audioClipLoop.isDone) { } requests[i * 2 + 1] = UnityWebRequestMultimedia.GetAudioClip($"File://{text}{track.Name}Loop.mp3", AudioType.MPEG);
if (audioClipStart.result == UnityWebRequest.Result.Success && audioClipLoop.result == UnityWebRequest.Result.Success) { requests[i * 2].SendWebRequest();
AudioStart = DownloadHandlerAudioClip.GetContent(audioClipStart); requests[i * 2 + 1].SendWebRequest();
AudioLoop = DownloadHandlerAudioClip.GetContent(audioClipLoop); }
while (!requests.All(request => request.isDone)) { }
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]);
}
new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch)); new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch));
Logger.LogInfo("Muzika gromche, glaza zakryvaj! " + PluginInfo.PLUGIN_VERSION);
} else { } else {
Logger.LogError("Could not load audio file"); Logger.LogError("Could not load audio file");
} }
} }
} }
public class Track
{
public string Name;
public float WindUpTimer;
public float Bpm;
public AudioClip LoadedStart;
public AudioClip LoadedLoop;
}
[HarmonyPatch(typeof(JesterAI))] [HarmonyPatch(typeof(JesterAI))]
internal class JesterPatch internal class JesterPatch
{ {
[HarmonyPatch("Start")]
[HarmonyPostfix]
public static void AudioPatch(JesterAI __instance)
{
__instance.popGoesTheWeaselTheme = Plugin.AudioStart;
__instance.screamingSFX = Plugin.AudioLoop;
}
[HarmonyPatch("SetJesterInitialValues")]
[HarmonyPostfix]
public static void ForceTime(JesterAI __instance)
{
__instance.popUpTimer = 46.3f;
}
[HarmonyPatch("Update")] [HarmonyPatch("Update")]
[HarmonyPrefix] [HarmonyPrefix]
public static void DoNotStopTheMusicPrefix(JesterAI __instance, out State __state) public static void DoNotStopTheMusicPrefix(JesterAI __instance, out State __state)
{ {
__state = new State(); __state = new State();
__state.prevStateindex = __instance.previousState; __state.prevStateindex = __instance.previousState;
if (__instance.currentBehaviourStateIndex is 1 or 2) {
__state.farAudio = __instance.farAudio;
__instance.farAudio = __instance.creatureSFX;
}
} }
static List<Color> colors = [Color.magenta, Color.cyan, Color.green, Color.yellow]; static List<Color> colors = [Color.magenta, Color.cyan, Color.green, Color.yellow];
@ -81,7 +92,7 @@ namespace MuzikaGromche
i += 1; i += 1;
if (i >= colors.Count) i = 0; if (i >= colors.Count) i = 0;
yield return new WaitForSeconds(0.5f); yield return new WaitForSeconds(60f / Plugin.CurrentTrack.Bpm);
} }
} }
@ -89,16 +100,22 @@ namespace MuzikaGromche
[HarmonyPostfix] [HarmonyPostfix]
public static void DoNotStopTheMusic(JesterAI __instance, State __state) public static void DoNotStopTheMusic(JesterAI __instance, State __state)
{ {
if (__state.farAudio != null) if (__instance.currentBehaviourStateIndex is 1 && __state.prevStateindex != 1)
{ {
__instance.farAudio = __state.farAudio; __instance.farAudio.Stop();
__instance.farAudio.Pause();
} }
if (__instance.currentBehaviourStateIndex is 1 && !__instance.farAudio.isPlaying) if (__instance.currentBehaviourStateIndex is 1 && !__instance.farAudio.isPlaying)
{ {
var seed = RoundManager.Instance.dungeonGenerator.Generator.ChosenSeed;
var trackId = seed % Plugin.Tracks.Length;
Debug.Log($"Seed is {seed}, chosen track is {trackId} out of {Plugin.Tracks.Length} tracks");
Plugin.CurrentTrack = Plugin.Tracks[trackId];
__instance.popUpTimer = Plugin.CurrentTrack.WindUpTimer;
Debug.Log($"Playing start music: maxDistance: {__instance.farAudio.maxDistance}, minDistance: {__instance.farAudio.minDistance}, volume: {__instance.farAudio.volume}, spread: {__instance.farAudio.spread}"); Debug.Log($"Playing start music: maxDistance: {__instance.farAudio.maxDistance}, minDistance: {__instance.farAudio.minDistance}, volume: {__instance.farAudio.volume}, spread: {__instance.farAudio.spread}");
__instance.farAudio.maxDistance = 150; __instance.farAudio.maxDistance = 150;
__instance.farAudio.PlayOneShot(Plugin.AudioStart); __instance.farAudio.PlayOneShot(Plugin.CurrentTrack.LoadedStart);
} }
if (__instance.currentBehaviourStateIndex is 2 && __state.prevStateindex != 2) if (__instance.currentBehaviourStateIndex is 2 && __state.prevStateindex != 2)
@ -130,7 +147,7 @@ namespace MuzikaGromche
{ {
Debug.Log($"Playing loop music: maxDistance: {__instance.creatureVoice.maxDistance}, minDistance: {__instance.creatureVoice.minDistance}, volume: {__instance.creatureVoice.volume}, spread: {__instance.creatureVoice.spread}"); Debug.Log($"Playing loop music: maxDistance: {__instance.creatureVoice.maxDistance}, minDistance: {__instance.creatureVoice.minDistance}, volume: {__instance.creatureVoice.volume}, spread: {__instance.creatureVoice.spread}");
__instance.creatureVoice.maxDistance = 150; __instance.creatureVoice.maxDistance = 150;
__instance.creatureVoice.clip = Plugin.AudioLoop; __instance.creatureVoice.clip = Plugin.CurrentTrack.LoadedLoop;
__instance.creatureVoice.Play(); __instance.creatureVoice.Play();
} }
} }
@ -138,7 +155,6 @@ namespace MuzikaGromche
internal class State internal class State
{ {
public AudioSource farAudio;
public int prevStateindex; public int prevStateindex;
} }
} }