-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsounds.lua
More file actions
134 lines (117 loc) · 3.87 KB
/
Copy pathsounds.lua
File metadata and controls
134 lines (117 loc) · 3.87 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
---@name Sounds
---@author AstricUnion
---@shared
---@class astrosounds
local astrosounds = {}
if SERVER then
---Play sound
---@param name string Identifier of sound
---@param offset Vector? Position or offset of this sound
---@param parent Entity? Entity, parent this sound to
---@param plys table | Player | nil Players to send the sound
function astrosounds.play(name, offset, parent, plys)
net.start("playSound")
net.writeString(name)
net.writeVector(offset or Vector())
net.writeBool(parent ~= nil)
if parent then
net.writeEntity(parent)
end
net.send(plys)
end
---Stop sound
---@param name string Identifier of sound
---@param plys table | Player | nil Players to stop the sound
function astrosounds.stop(name, plys)
net.start("stopSound")
net.writeString(name)
net.send(plys)
end
else
local SOUNDS = {}
local PARENTS = {}
local ERRORS = {}
local function message(...)
printConsole(Color(255, 0, 0), "[AstroSound] ", Color(255, 255, 255), ...)
end
---Preload sound
---@param name string Name to identify this sound
---@param volume number Volume of this sound
---@param loop boolean Loop this sound
---@param play boolean Play this sound after preload
---@param url string URL to download sound
function astrosounds.preload(name, volume, loop, play, url)
local noplay = (!play and " noplay" or "")
bass.loadURL(url, "3d noblock" .. noplay, function(snd, _, errname)
if !snd then
local attempts = ERRORS[name] or 3
message(string.format(
"Sound \"%s\" error: %s. Attempts remain: %i",
name, errname, attempts
))
if attempts and attempts > 0 then
ERRORS[name] = attempts - 1
timer.simple(1, function()
astrosounds.preload(name, volume, loop, play, url)
end)
end
return
end
SOUNDS[name] = snd
message(string.format("Sound \"%s\" loaded!", name))
snd:setVolume(volume)
snd:setLooping(loop)
end)
end
---Play sound
---@param name string Identifier of sound
---@param offset Vector? Position or offset of this sound
---@param parent Entity? Entity, parent this sound to
function astrosounds.play(name, offset, parent)
offset = offset or Vector()
local sound = SOUNDS[name]
if sound then
if !sound:isLooping() then
sound:setTime(0)
end
local parentPos = (parent and parent:getPos() or Vector())
sound:setPos(parentPos + offset)
sound:play()
PARENTS[name] = {parent, offset}
end
end
---Stop sound
---@param name string Identifier of sound
function astrosounds.stop(name)
local sound = SOUNDS[name]
if sound then
sound:pause()
sound:setTime(0)
end
end
net.receive("playSound", function()
local name = net.readString()
local pos = net.readVector()
local is_parent = net.readBool()
if is_parent then
net.readEntity(function(ent)
astrosounds.play(name, pos, ent)
end)
else
astrosounds.play(name, pos)
end
end)
net.receive("stopSound", function()
local name = net.readString()
astrosounds.stop(name)
end)
hook.add("Think", "soundParent", function()
for name, parent in pairs(PARENTS) do
local snd = SOUNDS[name]
if snd and isValid(parent[1]) then
snd:setPos(parent[1]:getPos() + parent[2])
end
end
end)
end
return astrosounds