Update
This commit is contained in:
@@ -5,6 +5,7 @@ require("/koptilnya/libs/utils.txt")
|
||||
|
||||
EngineSound = class("EngineSound")
|
||||
|
||||
accessorFunc(EngineSound, "_rpm", "RPM", 0)
|
||||
accessorFunc(EngineSound, "_masterVolume", "MasterVolume", 1)
|
||||
accessorFunc(EngineSound, "_parent", "Parent", chip())
|
||||
|
||||
@@ -29,10 +30,11 @@ local function fadeOut(rpm, range)
|
||||
end
|
||||
|
||||
function EngineSound:initialize(soundsMap)
|
||||
self._rpm = 0
|
||||
self._targetRPM = 0
|
||||
self.pitchDamping = 0.2
|
||||
self.volumeDamping = 0.3
|
||||
|
||||
self._soundsMap = soundsMap
|
||||
self._sources = {}
|
||||
self._lastRPM = 0
|
||||
|
||||
for k, v in pairs(soundsMap) do
|
||||
bass.loadURL(v.link, "3d noblock", function(snd, err, errtxt)
|
||||
@@ -42,39 +44,35 @@ function EngineSound:initialize(soundsMap)
|
||||
snd:setVolume(0)
|
||||
|
||||
v.source = snd
|
||||
table.insert(self._sources, snd)
|
||||
v.lastPitch = 1
|
||||
v.lastVolume = 0
|
||||
elseif errtext then
|
||||
error(errtxt)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
hook.add("think", "EngineSound_think", function()
|
||||
self._rpm = math.lerp(0.2, self._rpm, self._targetRPM)
|
||||
|
||||
hook.add("think", "EngineSound_think", function()
|
||||
for _, v in pairs(self._soundsMap) do
|
||||
if v.source then
|
||||
v.source:setPos(self:getParent():getPos())
|
||||
|
||||
local pitch = self._rpm / v.rootPitch
|
||||
local fadeIn = fadeIn(self._rpm, v.fadeIn)
|
||||
local fadeOut = fadeOut(self._rpm, v.fadeOut)
|
||||
local fadeOut = fadeOut(self._rpm, v.fadeOut)
|
||||
local targetVolume = self:getMasterVolume() * (fadeIn + fadeOut - 1)
|
||||
local targetPitch = self._rpm / v.rootPitch
|
||||
|
||||
v.source:setVolume(math.max(self:getMasterVolume() * (fadeIn + fadeOut - 1), 0.01))
|
||||
v.source:setPitch(math.max(pitch, 0.01))
|
||||
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
|
||||
|
||||
_lastRPM = _rpm
|
||||
end)
|
||||
end
|
||||
|
||||
function EngineSound:setRPM(rpm)
|
||||
self._targetRPM = rpm
|
||||
end
|
||||
|
||||
function EngineSound:getRPM()
|
||||
return self._rpm
|
||||
end
|
||||
|
||||
function EngineSound:getSources()
|
||||
return self._sources
|
||||
end
|
||||
Reference in New Issue
Block a user