-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
174 lines (147 loc) · 4 KB
/
client.lua
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
local SoundIncrementer = 0
local SoundInstanceIncrementer = 0
local SoundsMap = {}
local ServerSoundsMap = {}
function getCameraDirection()
local cameraRotation = GetGameplayCamRot(0)
local radiansZ = (cameraRotation.z * 0.0174532924)
local radiansX = (cameraRotation.x * 0.0174532924)
local xCos = math.abs(math.cos(radiansX))
return {
x = (-math.sin(radiansZ) * xCos),
y = (math.cos(radiansZ) * xCos),
z = math.sin(radiansX),
}
end
Citizen.CreateThread(function()
local function _() -- This crazy "thingy" is to allow guard clauses
local playerPed = GetPlayerPed(-1)
if not DoesEntityExist(playerPed) then
return
end
local playerCoordinates = GetEntityCoords(playerPed)
local cameraDirection = getCameraDirection()
SendNUIMessage({
type = 'PlayerUpdate',
args = {
position = playerCoordinates,
orientation = cameraDirection
}
})
end
while true do
_()
Citizen.Wait(10)
end
end)
function Create(args)
if args.id == nil then
SoundIncrementer = SoundIncrementer + 1
args.id = SoundIncrementer
end
SendNUIMessage({
type = 'Create',
sound = args.id,
args = args
})
return args.id
end
function Play(sound, creationTime)
SoundInstanceIncrementer = SoundInstanceIncrementer + 1
local soundInstance = SoundInstanceIncrementer
SoundsMap[soundInstance] = sound
SendNUIMessage({
type = 'Play',
sound = sound,
args = {
soundInstance = soundInstance,
creationTime = creationTime,
}
})
return soundInstance
end
function Stop(soundInstance)
SendNUIMessage({
type = 'Stop',
sound = SoundsMap[soundInstance],
args = {
soundInstance = soundInstance
}
})
end
function Pause(soundInstance)
SendNUIMessage({
type = 'Pause',
sound = SoundsMap[soundInstance],
args = {
soundInstance = soundInstance
}
})
end
function Seek(soundInstance, duration)
SendNUIMessage({
type = 'Seek',
sound = SoundsMap[soundInstance],
args = {
soundInstance = soundInstance,
duration = duration
},
})
end
function Position(soundInstance, position)
SendNUIMessage({
type = 'Position',
sound = SoundsMap[soundInstance],
args = {
soundInstance = soundInstance,
position = position
},
})
end
function Loop(soundInstance, loop)
SendNUIMessage({
type = 'Loop',
sound = SoundsMap[soundInstance],
args = {
soundInstance = soundInstance,
loop = loop
},
})
end
RegisterNetEvent("spatial_audio:addServerSound", function(soundInstanceTbl)
if soundInstanceTbl.stop then
return
end
local cSoundInstance = Play(soundInstanceTbl.sound, soundInstanceTbl.creation)
ServerSoundsMap[soundInstanceTbl.soundInstance] = cSoundInstance
local position = soundInstanceTbl.position
if (position) then
Position(cSoundInstance, position)
end
local loop = soundInstanceTbl.loop
if (loop) then
Loop(cSoundInstance, loop)
end
end)
RegisterNetEvent("spatial_audio:action", function(action, soundInstance, args)
local cSoundInstance = ServerSoundsMap[soundInstance]
if (action == "Position") then
Position(cSoundInstance, args)
elseif (action == "Loop") then
Loop(cSoundInstance, args)
elseif (action == "Stop") then
Stop(cSoundInstance)
end
end)
RegisterNUICallback("ready", function(_, cb)
TriggerEvent("spatial_audio:ready")
TriggerServerEvent("spatial_audio:requestServerSounds")
cb({})
end)
exports("Create", Create)
exports("Play", Play)
exports("Stop", Stop)
exports("Pause", Pause)
exports("Seek", Seek)
exports("Position", Position)
exports("Loop", Loop)