add looping to music

This commit is contained in:
neckrat 2026-01-18 13:32:45 +03:00
parent 2c8b65e1ae
commit ae03ee3adb

View File

@ -4,9 +4,10 @@ local AnimationNode = require "lib.animation_node"
--- @class Audio --- @class Audio
--- @field musicVolume number --- @field musicVolume number
--- @field soundVolume number --- @field soundVolume number
--- @field looped boolean
--- @field animationNode AnimationNode? --- @field animationNode AnimationNode?
--- @field from love.Source --- @field from love.Source?
--- @field to love.Source --- @field to love.Source?
audio = {} audio = {}
audio.__index = audio audio.__index = audio
@ -14,7 +15,8 @@ audio.__index = audio
local function new(musicVolume, soundVolume) local function new(musicVolume, soundVolume)
return setmetatable({ return setmetatable({
musicVolume = musicVolume, musicVolume = musicVolume,
soundVolume = soundVolume soundVolume = soundVolume,
looped = true
}, audio) }, audio)
end end
@ -40,8 +42,8 @@ end
--- @param ms number? in milliseconds --- @param ms number? in milliseconds
function audio:crossfade(from, to, ms) function audio:crossfade(from, to, ms)
print("[Audio]: Triggered crossfade") print("[Audio]: Triggered crossfade")
self:play(to)
to:setVolume(0) to:setVolume(0)
to:play()
self.from = from self.from = from
self.to = to self.to = to
self.animationNode = AnimationNode { self.animationNode = AnimationNode {
@ -60,6 +62,7 @@ end
--- @param source love.Source --- @param source love.Source
function audio:play(source) function audio:play(source)
if source:getType() == "stream" then if source:getType() == "stream" then
source:setLooping(self.looped)
source:setVolume(self.musicVolume) source:setVolume(self.musicVolume)
return source:play() return source:play()
end end