|
| 1 | +using System.Collections.Concurrent; |
| 2 | + |
| 3 | +namespace STS2RitsuLib.Audio |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Coordinates adaptive room/combat/victory music playback in response to game lifecycle transitions. |
| 7 | + /// </summary> |
| 8 | + public sealed class AudioAdaptiveMusicDirector : IDisposable |
| 9 | + { |
| 10 | + private readonly ConcurrentDictionary<AudioAdaptiveMusicHandle, AudioAdaptiveMusicPlan> _active = new(); |
| 11 | + private readonly IDisposable _combatEndedSubscription; |
| 12 | + private readonly IDisposable _combatStartingSubscription; |
| 13 | + private readonly IDisposable _combatVictorySubscription; |
| 14 | + private readonly IDisposable _roomEnteredSubscription; |
| 15 | + private readonly IDisposable _runEndedSubscription; |
| 16 | + private readonly IDisposable _runLoadedSubscription; |
| 17 | + private readonly IDisposable _runStartedSubscription; |
| 18 | + |
| 19 | + private AudioAdaptiveMusicDirector() |
| 20 | + { |
| 21 | + _runStartedSubscription = RitsuLibFramework.SubscribeLifecycle<RunStartedEvent>(_ => RefreshRoomState()); |
| 22 | + _runLoadedSubscription = RitsuLibFramework.SubscribeLifecycle<RunLoadedEvent>(_ => RefreshRoomState()); |
| 23 | + _roomEnteredSubscription = RitsuLibFramework.SubscribeLifecycle<RoomEnteredEvent>(_ => RefreshRoomState()); |
| 24 | + _combatStartingSubscription = |
| 25 | + RitsuLibFramework.SubscribeLifecycle<CombatStartingEvent>(_ => SwitchCombatState()); |
| 26 | + _combatVictorySubscription = |
| 27 | + RitsuLibFramework.SubscribeLifecycle<CombatVictoryEvent>(_ => SwitchVictoryState()); |
| 28 | + _combatEndedSubscription = |
| 29 | + RitsuLibFramework.SubscribeLifecycle<CombatEndedEvent>(_ => RestoreAfterCombat()); |
| 30 | + _runEndedSubscription = RitsuLibFramework.SubscribeLifecycle<RunEndedEvent>(_ => ClearAll(false)); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Shared singleton director. |
| 35 | + /// </summary> |
| 36 | + public static AudioAdaptiveMusicDirector Shared { get; } = new(); |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Disposes framework lifecycle subscriptions owned by this director. |
| 40 | + /// </summary> |
| 41 | + public void Dispose() |
| 42 | + { |
| 43 | + _runStartedSubscription.Dispose(); |
| 44 | + _runLoadedSubscription.Dispose(); |
| 45 | + _roomEnteredSubscription.Dispose(); |
| 46 | + _combatStartingSubscription.Dispose(); |
| 47 | + _combatVictorySubscription.Dispose(); |
| 48 | + _combatEndedSubscription.Dispose(); |
| 49 | + _runEndedSubscription.Dispose(); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Starts following the supplied adaptive music plan and returns a handle for later shutdown. |
| 54 | + /// </summary> |
| 55 | + public AudioAdaptiveMusicHandle Attach(AudioAdaptiveMusicPlan plan) |
| 56 | + { |
| 57 | + var handle = new AudioAdaptiveMusicHandle(plan); |
| 58 | + _active[handle] = plan; |
| 59 | + RefreshRoomState(handle, plan); |
| 60 | + return handle; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Removes a previously attached adaptive music handle from lifecycle tracking. |
| 65 | + /// </summary> |
| 66 | + public void Detach(AudioAdaptiveMusicHandle handle) |
| 67 | + { |
| 68 | + _active.TryRemove(handle, out _); |
| 69 | + } |
| 70 | + |
| 71 | + private void RefreshRoomState() |
| 72 | + { |
| 73 | + foreach (var pair in _active) |
| 74 | + RefreshRoomState(pair.Key, pair.Value); |
| 75 | + } |
| 76 | + |
| 77 | + private static void RefreshRoomState(AudioAdaptiveMusicHandle handle, AudioAdaptiveMusicPlan plan) |
| 78 | + { |
| 79 | + if (plan.RoomSource is null) |
| 80 | + { |
| 81 | + if (plan.RefreshVanillaRoomStateOnRoomEnter) |
| 82 | + AudioVanillaBridge.RefreshTrackAndAmbience(); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + var music = GameFmod.Playback.PlayMusic(plan.RoomSource, plan.RoomOptions); |
| 87 | + handle.SwitchTo(music); |
| 88 | + } |
| 89 | + |
| 90 | + private void SwitchCombatState() |
| 91 | + { |
| 92 | + foreach (var pair in _active) |
| 93 | + { |
| 94 | + if (pair.Value.CombatSource is null) |
| 95 | + continue; |
| 96 | + |
| 97 | + var music = GameFmod.Playback.PlayMusic(pair.Value.CombatSource, pair.Value.CombatOptions); |
| 98 | + pair.Key.SwitchTo(music); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void SwitchVictoryState() |
| 103 | + { |
| 104 | + foreach (var pair in _active) |
| 105 | + { |
| 106 | + if (pair.Value.VictorySource is null) |
| 107 | + continue; |
| 108 | + |
| 109 | + var music = GameFmod.Playback.PlayMusic(pair.Value.VictorySource, pair.Value.VictoryOptions); |
| 110 | + pair.Key.SwitchTo(music); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void RestoreAfterCombat() |
| 115 | + { |
| 116 | + foreach (var pair in _active) |
| 117 | + { |
| 118 | + if (pair.Value.RestoreVanillaMusicOnCombatEnd) |
| 119 | + { |
| 120 | + pair.Key.Stop(); |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + RefreshRoomState(pair.Key, pair.Value); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void ClearAll(bool restoreVanillaMusic) |
| 129 | + { |
| 130 | + foreach (var handle in _active.Keys) |
| 131 | + handle.Stop(restoreVanillaMusic); |
| 132 | + |
| 133 | + _active.Clear(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments