-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkedMusicExample.cs
More file actions
55 lines (46 loc) · 2.25 KB
/
Copy pathNetworkedMusicExample.cs
File metadata and controls
55 lines (46 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using SboxDirector;
namespace AdaptiveDirectorExamples;
/// <summary>Shows the authority/client boundary. Use your game's RPC or snapshot system for SendToOwningClient.</summary>
public sealed class NetworkedMusicAuthorityExample
{
readonly MusicDirectorCore _director = new(RecoveredMusicDirectorDefaults.CreateConfiguration(ExampleMusicSetup.CreateCues(), ExampleMusicSetup.CreateSpecialAlerts(), seed: 9001));
long _nextEventSequence = 1;
public MusicDecisionBatch Tick(MusicWorldSnapshot world) => _director.Tick(world);
public MusicDecisionBatch PlayScriptedCue(MusicWorldSnapshot world, string participantId, string eventId) => _director.Tick(world, new[]
{
new MusicGameplayEvent(_nextEventSequence++, MusicTriggerKind.Play, participantId, eventId)
});
// Replicate MusicDecision fields, not engine sound handles.
public void TickAndSend(MusicWorldSnapshot world, Action<string, MusicDecisionBatch> sendToOwningClient)
{
var batch = Tick(world);
foreach (var participant in batch.Decisions.Select(x => x.ParticipantId).Distinct(StringComparer.Ordinal))
{
var filtered = new MusicDecisionBatch { Time = batch.Time };
filtered.Decisions.AddRange(batch.Decisions.Where(x => x.ParticipantId == participant));
sendToOwningClient(participant, filtered);
}
}
}
public sealed class NetworkedMusicClientExample
{
readonly MusicRuntime _runtime;
readonly IMusicAudioSink _audio;
public NetworkedMusicClientExample(string localParticipantId, IMusicAudioSink audio)
{
_runtime = new MusicRuntime(localParticipantId, ExampleMusicSetup.CreateCatalog()); _audio = audio;
}
public MusicRuntimeBatch Receive(MusicDecisionBatch replicatedBatch, bool playerDead, bool scenarioEnded)
{
_runtime.SetLifecycle(playerDead, scenarioEnded);
var result = _runtime.Apply(replicatedBatch);
foreach (var action in result.Actions) _audio.Apply(action);
return result;
}
public MusicRuntimeBatch OnAudioFinished(long musicInstanceId, double time)
{
var result = _runtime.ReportCompleted(musicInstanceId, time);
foreach (var action in result.Actions) _audio.Apply(action);
return result;
}
}