forked from jeff-1amstudios/OpenC1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundCache.cs
157 lines (137 loc) · 5.15 KB
/
SoundCache.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Text;
using OpenC1.Parsers;
using Microsoft.Xna.Framework;
using OneAmEngine.Audio;
using OneAmEngine;
namespace OpenC1
{
static class SoundIds
{
public const int Repair = 5200;
public const int CrashStart = 5000;
public const int CrashEnd = 5004;
public const int ScrapeStart = 5010;
public const int ScrapeEnd = 5012;
public const int SkidStart = 9000;
public const int SkidEnd = 9002;
public const int ScrubStart = 9003;
public const int ScrubEnd = 9004;
public const int OutOfTime = 8010;
public const int RaceCompleted = 8011;
public const int Checkpoint = 8012;
public const int WrongCheckpoint = 8013;
public const int Clapping = 8015;
public const int CopSiren = 5350;
public const int UI_UpDown = 3000;
public const int UI_Ok = 3004;
public const int UI_Esc = 3005;
public const int TimeBuzzer = 1001;
public const int PedSquelch = 4020;
}
static class SoundCache
{
static bool _enabled = true;
static List<SoundDesc> _soundDescriptions;
public static bool IsInitialized;
static List<ISound> _playerInstances = new List<ISound>();
static List<ISound> _aiInstances = new List<ISound>();
static ISound _currentSkid, _currentScrub, _currentCrash;
public static void Initialize()
{
SoundsFile soundFile = new SoundsFile(GameVars.BasePath + "sound\\sound.txt");
_soundDescriptions = soundFile.Sounds;
IsInitialized = true;
if (!_enabled) return;
//foreach (SoundDesc desc in _soundDescriptions)
// CreateInstance(desc.Id, true);
CreateInstance(5000, true);
CreateInstance(5001, true);
CreateInstance(5002, true);
CreateInstance(5003, true);
CreateInstance(5004, true);
}
public static ISound CreateInstance(int id)
{
return CreateInstance(id, false);
}
public static ISound CreateInstance(int id, bool is3d)
{
if (!_enabled) return null;
SoundDesc csound = _soundDescriptions.Find(a => a.Id == id);
if (csound == null || csound.FailedToLoad) return null;
ISound instance = GameEngine.Audio.Load(GameVars.BasePath + "sound\\" + csound.FileName, is3d);
if (instance == null)
{
csound.FailedToLoad = true;
return null;
}
if (_playerInstances.Exists(a => a.Id == id))
{
}
instance.MinimumDistance = 10;
instance.MaximumDistance = 200;
instance.Id = csound.Id;
_playerInstances.Add(instance);
return instance;
}
public static ISound Play(int id, Vehicle vehicle, bool is3d)
{
if (vehicle == null
|| (vehicle.Driver is CpuDriver && ((CpuDriver)vehicle.Driver).InPlayersView)
|| vehicle.Driver is PlayerDriver)
{
ISound instance = _playerInstances.Find(a => a.Id == id);
if (instance == null)
{
instance = CreateInstance(id, is3d);
}
if (instance != null)
{
if (is3d) instance.Position = vehicle.Position;
instance.Owner = vehicle;
instance.Play(false);
//GameConsole.WriteEvent("PlaySound " + id.ToString());
}
else
{
}
return instance;
}
else
return null;
}
public static void PlayCrash(Vehicle vehicle, float force)
{
PlayGroup(SoundIds.CrashStart, SoundIds.CrashEnd, ref _currentCrash, vehicle);
}
public static void PlayScrape(Vehicle vehicle)
{
PlayGroup(SoundIds.ScrapeStart, SoundIds.ScrapeEnd, ref _currentCrash, vehicle);
}
public static void PlaySkid(Vehicle vehicle, float factor)
{
if (factor > 0.35f)
PlayGroup(SoundIds.SkidStart, SoundIds.SkidEnd, ref _currentSkid, vehicle);
PlayGroup(SoundIds.ScrubStart, SoundIds.ScrubEnd, ref _currentScrub, vehicle);
}
private static void PlayGroup(int startId, int endId, ref ISound instance, Vehicle vehicle)
{
if (instance != null && instance.IsPlaying && instance.Owner != vehicle)
{
if (vehicle.Driver is PlayerDriver) //priority
instance.Reset();
else if (((Vehicle)instance.Owner).Driver is PlayerDriver)
{
return; //dont steal player's sound
}
}
if (instance == null || !instance.IsPlaying)
{
int id = GameEngine.Random.Next(startId, endId+1);
instance = Play(id, vehicle, true);
}
}
}
}