forked from Unity-Technologies/ECS-Network-Racing-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateAudioSourceSystem.cs
More file actions
139 lines (122 loc) · 4.22 KB
/
Copy pathUpdateAudioSourceSystem.cs
File metadata and controls
139 lines (122 loc) · 4.22 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Dots.Racing;
using Unity.Burst;
using Unity.Entities.Racing.Common;
using static Unity.Entities.SystemAPI;
namespace Unity.Entities.Racing.Gameplay
{
/// <summary>
/// Update the Player Audio Sources position and volume
/// </summary>
[BurstCompile]
[UpdateAfter(typeof(UpdateCameraTargetSystem))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct UpdateAudioSourceSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
}
public void OnDestroy(ref SystemState state)
{
foreach (var car in Query<PlayerAspect>().WithAll<AudioSourceTag>())
{
PlayerAudioManager.Instance.DeleteAudioSource(car.Self);
}
}
public void OnUpdate(ref SystemState state)
{
if (PlayerAudioManager.Instance == null)
{
return;
}
var ecb = new EntityCommandBuffer(state.WorldUpdateAllocator);
foreach (var car in Query<PlayerAspect>().WithNone<AudioSourceTag>())
{
var isLocalUser = state.EntityManager.HasComponent<LocalUser>(car.Self);
PlayerAudioManager.Instance.AddAudioSource(car.Self, isLocalUser);
ecb.AddComponent<AudioSourceTag>(car.Self);
}
foreach (var audio in Query<AudioAspect>())
{
PlayerAudioManager.Instance.UpdatePitchAndVolume(audio.Self, audio.GetVolumeRange());
PlayerAudioManager.Instance.UpdatePosition(audio.Self, audio.LocalToWorld.Position,
state.EntityManager);
}
ecb.Playback(state.EntityManager);
ecb.Dispose();
}
}
/// <summary>
/// Update music clips
/// </summary>
[BurstCompile]
[UpdateAfter(typeof(UpdateCameraTargetSystem))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct UpdateMusicSourceSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
}
public void OnDestroy(ref SystemState state)
{
}
public void OnUpdate(ref SystemState state)
{
if (PlayerAudioManager.Instance == null)
{
return;
}
PlayerAudioManager.Instance.CreateAndPlayMusicAudioSourceOnce();
if (!TryGetSingleton<Race>(out var race))
return;
if (race.NotStarted)
{
PlayerAudioManager.Instance.PlayLobbyMusic();
}
else if (race.IsRaceStarting)
{
PlayerAudioManager.Instance.PlayRaceMusic();
}
else if (race.IsInProgress)
{
foreach (var localPlayer in Query<LocalPlayerAspect>())
{
if (localPlayer.LapProgress.HasArrived)
{
PlayerAudioManager.Instance.PlayCelebrationMusic();
}
}
}
else if (race.HasFinished)
{
PlayerAudioManager.Instance.PlayCelebrationMusic();
}
}
}
/// <summary>
/// Update the Player Audio Sources position and volume
/// </summary>
[BurstCompile]
[UpdateAfter(typeof(UpdateCameraTargetSystem))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct UpdateUIAudioSourceSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
}
public void OnDestroy(ref SystemState state)
{
foreach (var car in Query<PlayerAspect>().WithAll<AudioSourceTag>())
{
PlayerAudioManager.Instance.DeleteAudioSource(car.Self);
}
}
public void OnUpdate(ref SystemState state)
{
if (PlayerAudioManager.Instance == null)
{
return;
}
PlayerAudioManager.Instance.CreateUIAudioSource();
}
}
}