ExperimentalGear/scripts/api/audiosample.lua

50 lines
1.1 KiB
Lua

require "common.class"
---@type AudioSample
local exclusiveAudioSample = nil
---@class AudioSample
---@field path string
---@field exclusive boolean
---@field loop boolean
---@field playing boolean
local AudioSample = {}
---Create new AudioSample instance
---@param o AudioSample
---@return AudioSample
function AudioSample.new(o)
assert(o.path, "AudioSample.new() did not receive path to audio sample")
o.exclusive = o.exclusive or false
o.loop = o.loop or false
game.LoadSkinSample(o.path)
return CreateInstance(AudioSample, o)
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