129 lines
3.7 KiB
Lua
129 lines
3.7 KiB
Lua
-- Audio (ModuleScript) -> StarterPlayer > StarterPlayerScripts
|
|
-- Звуки у этого игрока. Три группы громкости: музыка, эффекты и окружение (окружение — по громкости эффектов).
|
|
-- Громкость берётся из атрибутов игрока VolMusic / VolSfx (их выставляет и сохраняет сервер).
|
|
-- Audio.play(name) — разовый звук из Config.Sounds
|
|
-- Audio.loop(name, group) — петля (молчит, пока не вызвать Audio.fade)
|
|
-- Audio.fade(sound, factor) — плавно к громкости «базовая × factor» (0 — тишина, звук ставится на паузу)
|
|
|
|
local Players = game:GetService("Players")
|
|
local SoundService = game:GetService("SoundService")
|
|
local TweenService = game:GetService("TweenService")
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
|
|
local Config = require(ReplicatedStorage:WaitForChild("FishConfig"))
|
|
local player = Players.LocalPlayer
|
|
local rng = Random.new()
|
|
|
|
local Audio = {}
|
|
|
|
local function group(name)
|
|
local g = SoundService:FindFirstChild(name)
|
|
if not g then
|
|
g = Instance.new("SoundGroup")
|
|
g.Name = name
|
|
g.Parent = SoundService
|
|
end
|
|
return g
|
|
end
|
|
Audio.groups = {
|
|
music = group("MusicGroup"),
|
|
sfx = group("SfxGroup"),
|
|
ambient = group("AmbientGroup"),
|
|
}
|
|
|
|
local function applyVolumes()
|
|
local music = player:GetAttribute("VolMusic") or Config.DefaultVolume.music
|
|
local sfx = player:GetAttribute("VolSfx") or Config.DefaultVolume.sfx
|
|
Audio.groups.music.Volume = music
|
|
Audio.groups.sfx.Volume = sfx
|
|
Audio.groups.ambient.Volume = sfx
|
|
end
|
|
applyVolumes()
|
|
player:GetAttributeChangedSignal("VolMusic"):Connect(applyVolumes)
|
|
player:GetAttributeChangedSignal("VolSfx"):Connect(applyVolumes)
|
|
|
|
function Audio.play(name, volumeMult)
|
|
local def = Config.Sounds[name]
|
|
if not def or not def.id then
|
|
return nil
|
|
end
|
|
local s = Instance.new("Sound")
|
|
s.SoundId = def.id
|
|
s.Volume = (def.volume or 0.5) * (volumeMult or 1)
|
|
if def.pitch then
|
|
s.PlaybackSpeed = rng:NextNumber(def.pitch[1], def.pitch[2])
|
|
end
|
|
s.SoundGroup = Audio.groups.sfx
|
|
s.Parent = SoundService
|
|
s:Play()
|
|
s.Ended:Connect(function()
|
|
s:Destroy()
|
|
end)
|
|
task.delay(30, function()
|
|
if s.Parent then
|
|
s:Destroy()
|
|
end
|
|
end)
|
|
return s
|
|
end
|
|
|
|
-- разовый звук по прямому id (например, случайный раскат грома из списка)
|
|
function Audio.playId(id, volume, groupName)
|
|
local s = Instance.new("Sound")
|
|
s.SoundId = id
|
|
s.Volume = volume or 0.5
|
|
s.SoundGroup = Audio.groups[groupName or "sfx"]
|
|
s.Parent = SoundService
|
|
s:Play()
|
|
s.Ended:Connect(function()
|
|
s:Destroy()
|
|
end)
|
|
return s
|
|
end
|
|
|
|
function Audio.loop(name, groupName)
|
|
local def = Config.Sounds[name]
|
|
if not def then
|
|
return nil
|
|
end
|
|
local s = Instance.new("Sound")
|
|
s.Name = name
|
|
s.SoundId = def.id
|
|
s.Looped = true
|
|
s.Volume = 0
|
|
s.SoundGroup = Audio.groups[groupName or "ambient"]
|
|
s:SetAttribute("BaseVolume", def.volume or 0.5)
|
|
s.Parent = SoundService
|
|
return s
|
|
end
|
|
|
|
local fades = {}
|
|
function Audio.fade(s, factor, time)
|
|
if not s then
|
|
return
|
|
end
|
|
local target = (s:GetAttribute("BaseVolume") or 0.5) * (factor or 0)
|
|
if fades[s] and fades[s].target == target then
|
|
return
|
|
end
|
|
if fades[s] then
|
|
fades[s].tween:Cancel()
|
|
end
|
|
if target > 0 and not s.IsPlaying then
|
|
s:Resume()
|
|
if not s.IsPlaying then
|
|
s:Play()
|
|
end
|
|
end
|
|
local tw = TweenService:Create(s, TweenInfo.new(time or 2), { Volume = target })
|
|
fades[s] = { tween = tw, target = target }
|
|
tw.Completed:Connect(function(state)
|
|
if state == Enum.PlaybackState.Completed and target <= 0.001 then
|
|
s:Pause()
|
|
end
|
|
end)
|
|
tw:Play()
|
|
end
|
|
|
|
return Audio
|