local ease = require "lib.utils.easing" local AnimationNode = require "lib.animation_node" --- @class Audio --- @field musicVolume number --- @field soundVolume number --- @field animationNode AnimationNode? --- @field from love.Source --- @field to love.Source audio = {} audio.__index = audio --- здесь мы должны выгружать значения из файлика с сохранением настроек local function new(musicVolume, soundVolume) return setmetatable({ musicVolume = musicVolume, soundVolume = soundVolume }, audio) end function audio:update(dt) if self.animationNode and self.animationNode.state == "running" then self.animationNode:update(dt) self.from:setVolume(self.musicVolume - self.animationNode:getValue() * self.musicVolume) self.to:setVolume(self.animationNode:getValue() * self.musicVolume) -- print(self.animationNode.t) elseif self.animationNode and self.animationNode.state == "finished" then self.from:stop() self.animationNode:finish() self.animationNode = nil end end --- if from is nil, than we have fade in to; --- if to is nil, than we have fade out from --- --- also we should guarantee, that from and to have the same volume --- @param from love.Source --- @param to love.Source --- @param ms number? in milliseconds function audio:crossfade(from, to, ms) print("[Audio]: Triggered crossfade") to:setVolume(0) to:play() self.from = from self.to = to self.animationNode = AnimationNode { function(node) end, onEnd = function() self.from:setVolume(0) self.to:setVolume(self.musicVolume) print("[Audio]: Crossfade done") end, duration = ms or 1000, easing = ease.easeOutCubic, } self.animationNode:run() end --- @param source love.Source function audio:play(source) if source:getType() == "stream" then source:setVolume(self.musicVolume) return source:play() end source:setVolume(self.soundVolume) return source:play() end function audio:setMusicVolume(volume) self.musicVolume = volume end function audio:setSoundVolume(volume) self.soundVolume = volume end return { new = new }