Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Biome Handler #511

Merged
merged 19 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add handling for custom biome sounds
  • Loading branch information
LeeTwentyThree committed Dec 19, 2023
commit 7ba4bb1ea643914d0e8db8e0eceaa64340808f50
22 changes: 22 additions & 0 deletions Nautilus/Handlers/BiomeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ public static void RegisterBiome(string name, WaterscapeVolume.Settings settings
BiomePatcher.RegisterBiome(new BiomePatcher.CustomBiomeData(name, settings, sky));
}

/// <summary>
/// Adds music that plays in the given biome(s). The sound emitter is played when the given conditions are ended, until those conditions are no longer true, and then a fadeout is allowed.
/// </summary>
/// <param name="biomeName">The name of the biome that this music can play in. Prefix matching and case insensitive, so using "canyon" for this value would affect biomes named "canyon_one" and "canyon_TWO".</param>
/// <param name="musicAsset">The sound asset that plays in this biome.</param>
/// <param name="interiorState">Determines how this sound is affected by being indoors or outside.</param>
public static void AddBiomeMusic(string biomeName, FMODAsset musicAsset, FMODGameParams.InteriorState interiorState = FMODGameParams.InteriorState.Always)
{
BiomePatcher.RegisterBiomeSoundData(new BiomePatcher.CustomBiomeSoundData(BiomePatcher.CustomBiomeSoundData.Type.Music, biomeName, musicAsset, interiorState));
}

/// <summary>
/// Adds an ambient sound that plays in the given biome(s). The sound emitter is played when the given conditions are ended, until those conditions are no longer true, and then a fadeout is allowed.
/// </summary>
/// <param name="biomeName">The name of the biome that this ambient sound can play in. Prefix matching and case insensitive, so using "canyon" for this value would affect biomes named "canyon_one" and "canyon_TWO".</param>
/// <param name="ambienceAsset">The sound asset that plays in this biome.</param>
/// <param name="interiorState">Determines how this sound is affected by being indoors or outside.</param>
public static void AddBiomeAmbience(string biomeName, FMODAsset ambienceAsset, FMODGameParams.InteriorState interiorState)
{
BiomePatcher.RegisterBiomeSoundData(new BiomePatcher.CustomBiomeSoundData(BiomePatcher.CustomBiomeSoundData.Type.Ambience, biomeName, ambienceAsset, interiorState));
}

/// <summary>
/// Defines a reference to a new or existing Sky prefab.
/// </summary>
Expand Down
75 changes: 69 additions & 6 deletions Nautilus/Patchers/BiomePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace Nautilus.Patchers;
internal static class BiomePatcher
{
private static readonly List<CustomBiomeData> CustomBiomes = new();

private static readonly List<CustomBiomeSoundData> CustomBiomeSoundDatas = new();

internal static void Patch(Harmony harmony)
{
harmony.PatchAll(typeof(BiomePatcher));
Expand All @@ -26,6 +27,18 @@ internal static void RegisterBiome(CustomBiomeData biome)
}
}

internal static void RegisterBiomeSoundData(CustomBiomeSoundData biomeSoundData)
{
CustomBiomeSoundDatas.Add(biomeSoundData);

// Water ambience is the parent of all biome sounds. If it exists, we missed our chance to add it automatically, so add it now.
var waterAmbienceComponent = Player.main.GetComponentInChildren<WaterAmbience>();
if (waterAmbienceComponent != null)
{
AddBiomeSoundEmitterToWaterAmbience(waterAmbienceComponent, biomeSoundData);
}
}

[HarmonyPatch(typeof(WaterBiomeManager), nameof(WaterBiomeManager.Start))]
[HarmonyPostfix]
internal static void WaterBiomeManagerStartPostfix(WaterBiomeManager __instance)
Expand All @@ -36,30 +49,57 @@ internal static void WaterBiomeManagerStartPostfix(WaterBiomeManager __instance)
}
}

[HarmonyPatch(typeof(WaterAmbience), nameof(WaterAmbience.Start))]
[HarmonyPostfix]
internal static void WaterAmbienceStartPostfix(WaterAmbience __instance)
{
foreach (var soundData in CustomBiomeSoundDatas)
{
AddBiomeSoundEmitterToWaterAmbience(__instance, soundData);
}
}

internal static void AddBiomeToWaterBiomeManager(WaterBiomeManager manager, CustomBiomeData biome)
{
var skyPrefab = biome.Sky.GetSkyPrefabAtRuntime(manager);
manager.biomeSettings.Add(new WaterBiomeManager.BiomeSettings(){ name = biome.Name, settings = biome.Settings, skyPrefab = skyPrefab});
manager.biomeSettings.Add(new WaterBiomeManager.BiomeSettings()
{name = biome.Name, settings = biome.Settings, skyPrefab = skyPrefab});
var newIndexInBiomeLookup = manager.biomeLookup.Count;
Sky biomeSky = null;
if (skyPrefab != null && MarmoSkies.main != null)
{
biomeSky = MarmoSkies.main.GetSky(skyPrefab);
}

manager.biomeSkies.Add(biomeSky);
if (manager.biomeLookup.ContainsKey(biome.Name))
{
Debug.LogWarningFormat("WaterBiomeManager: biomeSettings contains multiple instances of the same biome name: {0}", new object[]
{
biome.Name
});
Debug.LogWarningFormat(
"WaterBiomeManager: biomeSettings contains multiple instances of the same biome name: {0}", new object[]
{
biome.Name
});
}
else
{
manager.biomeLookup.Add(biome.Name, newIndexInBiomeLookup);
}
}

internal static void AddBiomeSoundEmitterToWaterAmbience(WaterAmbience waterAmbience, CustomBiomeSoundData biomeSoundData)
{
var parent = waterAmbience.transform.Find(biomeSoundData.SoundType == CustomBiomeSoundData.Type.Music ? "music" : "background");
var emitterObject = new GameObject(biomeSoundData.BiomeName + (biomeSoundData.SoundType == CustomBiomeSoundData.Type.Music ? "Music" : "Ambience"));
emitterObject.transform.parent = parent;
var emitter = emitterObject.AddComponent<FMOD_CustomLoopingEmitter>();
emitter.SetAsset(biomeSoundData.SoundAsset);
emitter.stopImmediatelyOnDisable = true;
var gameParams = emitterObject.AddComponent<FMODGameParams>();
gameParams.loopingEmitter = emitter;
gameParams.onlyInBiome = biomeSoundData.BiomeName;
gameParams.interiorState = biomeSoundData.InteriorState;
}

internal class CustomBiomeData
{
public string Name { get; }
Expand All @@ -73,4 +113,27 @@ public CustomBiomeData(string name, WaterscapeVolume.Settings settings, BiomeHan
Sky = sky;
}
}

internal class CustomBiomeSoundData
{
public Type SoundType { get; }
public string BiomeName { get; }
public FMODAsset SoundAsset { get; }
public FMODGameParams.InteriorState InteriorState { get; }

public CustomBiomeSoundData(Type soundType, string biomeName, FMODAsset soundAsset,
FMODGameParams.InteriorState interiorState)
{
SoundType = soundType;
BiomeName = biomeName;
SoundAsset = soundAsset;
InteriorState = interiorState;
}

public enum Type
{
Ambience,
Music
}
}
}