87 lines
2.4 KiB
Plaintext
87 lines
2.4 KiB
Plaintext
--@include libs/utils.txt
|
|
--@client
|
|
require("/koptilnya/libs/utils.txt")
|
|
|
|
EngineSound = class("EngineSound")
|
|
|
|
accessorFunc(EngineSound, "_masterVolume", "MasterVolume", 1)
|
|
accessorFunc(EngineSound, "_parent", "Parent", chip())
|
|
|
|
local function fadeIn(rpm, range)
|
|
local fadeIn = 1
|
|
|
|
if range and range[1] ~= range[2] then
|
|
fadeIn = math.min(math.max((rpm + (range[2] - range[1] * 2)) / (range[2] - range[1]), 1) - 1, 1)
|
|
elseif range and range[1] == range[2] then
|
|
fadeIn = rpm >= range[1] and 1 or 0
|
|
end
|
|
|
|
return fadeIn
|
|
end
|
|
|
|
local function fadeOut(rpm, range)
|
|
local fadeOut = 1
|
|
|
|
if range and range[1] ~= range[2] then
|
|
fadeOut = math.min(math.max((rpm + (range[1] - range[2] * 2)) / (range[1] - range[2]), 1) - 1, 1)
|
|
elseif range and range[1] == range[2] then
|
|
fadeOut = rpm >= range[1] and 1 or 0
|
|
end
|
|
|
|
return fadeOut
|
|
end
|
|
|
|
function EngineSound:initialize(soundsMap)
|
|
self.pitchDamping = 0.2
|
|
self.volumeDamping = 0.3
|
|
|
|
self._rpm = 0
|
|
self._soundsMap = soundsMap
|
|
|
|
for k, v in pairs(soundsMap) do
|
|
bass.loadURL(v.link, "3d noblock", function(snd, err, errtxt)
|
|
if snd then
|
|
snd:setPos(self:getParent():getPos())
|
|
snd:setLooping(true)
|
|
snd:setVolume(0)
|
|
|
|
v.source = snd
|
|
v.lastPitch = 1
|
|
v.lastVolume = 0
|
|
elseif errtext then
|
|
error(errtxt)
|
|
end
|
|
end)
|
|
end
|
|
|
|
hook.add("think", "EngineSound_think", function()
|
|
for _, v in pairs(self._soundsMap) do
|
|
if v.source then
|
|
v.source:setPos(self:getParent():getPos())
|
|
|
|
local fadeIn = fadeIn(self:getRPM(), v.fadeIn)
|
|
local fadeOut = fadeOut(self:getRPM(), v.fadeOut)
|
|
local targetVolume = self:getMasterVolume() * (fadeIn + fadeOut - 1)
|
|
local targetPitch = self:getRPM() / v.rootPitch
|
|
|
|
local pitch = math.lerp(self.pitchDamping, v.lastPitch, targetPitch)
|
|
local volume = math.lerp(self.volumeDamping, v.lastVolume, targetVolume)
|
|
|
|
v.source:setVolume(volume)
|
|
v.source:setPitch(pitch)
|
|
|
|
v.lastVolume = volume
|
|
v.lastPitch = pitch
|
|
end
|
|
end
|
|
end)
|
|
end
|
|
|
|
function EngineSound:setRPM(rpm)
|
|
self._rpm = math.max(rpm, 0)
|
|
end
|
|
|
|
function EngineSound:getRPM()
|
|
return self._rpm
|
|
end
|