ExperimentalGear/scripts/audio/audiosample.lua

49 lines
1.2 KiB
Lua
Raw Permalink Normal View History

require "common.class"
---@type AudioSample
local exclusiveAudioSample = nil
---@class AudioSample
---@field path string
---@field exclusive boolean
---@field loop boolean
local AudioSample = {
__name = "AudioSample"
}
---Create new AudioSample instance
---@param params AudioSample
---@return AudioSample
function AudioSample.new(params)
assert(params.path, "AudioSample.new() did not receive path to audio sample")
params.exclusive = params.exclusive or false
params.loop = params.loop or false
game.LoadSkinSample(params.path)
return CreateInstance(AudioSample, params)
end
function AudioSample:play()
2022-06-29 00:23:16 +02:00
if not game.IsSamplePlaying(self.path) then
if self.exclusive then
if exclusiveAudioSample and self ~= exclusiveAudioSample then
exclusiveAudioSample:stop()
end
exclusiveAudioSample = self
end
game.PlaySample(self.path)
end
end
function AudioSample:stop()
2022-06-29 00:23:16 +02:00
if game.IsSamplePlaying(self.path) then
if self.exclusive and self == exclusiveAudioSample then
exclusiveAudioSample = nil
end
game.StopSample(self.path)
end
end
return AudioSample