ExperimentalGear/scripts/api/audiosample.lua

52 lines
1.2 KiB
Lua
Raw Normal View History

require "common.class"
---@type AudioSample
local exclusiveAudioSample = nil
---@class AudioSample
---@field path string
---@field exclusive boolean
---@field loop boolean
---@field playing 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()
if not self.playing then
if self.exclusive then
if exclusiveAudioSample and self ~= exclusiveAudioSample then
exclusiveAudioSample:stop()
end
exclusiveAudioSample = self
end
self.playing = true
game.PlaySample(self.path)
end
end
function AudioSample:stop()
if self.playing then
if self.exclusive and self == exclusiveAudioSample then
exclusiveAudioSample = nil
end
self.playing = false
game.StopSample(self.path)
end
end
return AudioSample