feature/audioengine #26
@ -5,6 +5,8 @@ local AnimationNode = require "lib.animation_node"
|
||||
--- @field musicVolume number
|
||||
--- @field soundVolume number
|
||||
--- @field animationNode AnimationNode?
|
||||
--- @field from love.Source
|
||||
--- @field to love.Source
|
||||
audio = {}
|
||||
audio.__index = audio
|
||||
|
||||
@ -17,10 +19,15 @@ local function new(musicVolume, soundVolume)
|
||||
end
|
||||
|
||||
function audio:update(dt)
|
||||
if self.animationNode then
|
||||
if self.animationNode and self.animationNode.state == "running" then
|
||||
self.animationNode:update(dt)
|
||||
print(self.animationNode.t)
|
||||
print(self.animationNode:getValue())
|
||||
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
|
||||
|
||||
@ -32,64 +39,22 @@ end
|
||||
--- @param to love.Source
|
||||
--- @param ms number? in milliseconds
|
||||
function audio:crossfade(from, to, ms)
|
||||
print("[Audio]: Triggered crossfade")
|
||||
to:setVolume(0)
|
||||
to:play()
|
||||
local fromVol = from:getVolume() or self.musicVolume
|
||||
local toVol = to:getVolume() or self.musicVolume
|
||||
local dt = love.timer.getDelta() * (ms or 1000)
|
||||
|
||||
while fromVol > 0 and toVol < self.musicVolume do
|
||||
print(fromVol, toVol)
|
||||
fromVol = self.musicVolume - fromVol + dt / self.musicVolume
|
||||
from:setVolume(ease.easeOutQuad(fromVol))
|
||||
toVol = toVol + dt / self.musicVolume
|
||||
to:setVolume(ease.easeInQuad(toVol))
|
||||
print(fromVol, toVol)
|
||||
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:crossfadeAnim(from, to, ms)
|
||||
to:setVolume(0)
|
||||
to:play()
|
||||
print(from:getVolume(), to:getVolume())
|
||||
local t = 0
|
||||
self.from = from
|
||||
self.to = to
|
||||
self.animationNode = AnimationNode {
|
||||
function(node)
|
||||
from:setVolume(self.musicVolume - node:getValue() * self.musicVolume)
|
||||
to:setVolume(node:getValue() * self.musicVolume)
|
||||
t = node.t
|
||||
print(node.duration, node.t)
|
||||
end,
|
||||
function(node) end,
|
||||
onEnd = function()
|
||||
from:setVolume(0)
|
||||
to:setVolume(self.musicVolume)
|
||||
self.from:setVolume(0)
|
||||
self.to:setVolume(self.musicVolume)
|
||||
print("[Audio]: Crossfade done")
|
||||
end,
|
||||
duration = ms or 1000,
|
||||
easing = ease.linear,
|
||||
-- children = {
|
||||
-- AnimationNode {
|
||||
-- function(node)
|
||||
-- from:setVolume(self.musicVolume - node:getValue() * self.musicVolume)
|
||||
-- end
|
||||
-- },
|
||||
-- AnimationNode {
|
||||
-- function(node)
|
||||
-- to:setVolume(node:getValue() * self.musicVolume)
|
||||
-- print(node.count)
|
||||
-- end
|
||||
-- }
|
||||
-- }
|
||||
easing = ease.easeOutCubic,
|
||||
}
|
||||
self.animationNode:run()
|
||||
-- anim:finish()
|
||||
print(from:getVolume(), to:getVolume(), t, t, t)
|
||||
end
|
||||
|
||||
--- @param source love.Source
|
||||
|
||||
@ -92,8 +92,8 @@ function regenerateMana:cast(caster, target)
|
||||
function(node)
|
||||
local audioPath = Tree.assets.files.audio
|
||||
sprite:animate("hurt", node)
|
||||
Tree.audio:crossfadeAnim(audioPath.music.level1.battle,
|
||||
audioPath.music.level1.choral, 2000)
|
||||
Tree.audio:crossfade(audioPath.music.level1.battle,
|
||||
audioPath.music.level1.choral, 5000)
|
||||
end,
|
||||
onEnd = function() caster:has(Tree.behaviors.spellcaster):endCast() end
|
||||
}:run()
|
||||
|
||||
@ -13,7 +13,7 @@ Tree.audio = (require "lib.audio").new(1, 1)
|
||||
Tree.level = (require "lib.level.level").new("procedural", "flower_plains") -- для теста у нас только один уровень, который сразу же загружен
|
||||
|
||||
Tree.behaviors = (require "lib.utils.behavior_loader")("lib/character/behaviors") --- @todo написать нормальную загрузку поведений
|
||||
Tree.audio = (require "lib.audio").new(1, 1)
|
||||
-- Tree.audio = (require "lib.audio").new(1, 1)
|
||||
-- Tree.behaviors.map = require "lib.character.behaviors.map"
|
||||
-- Tree.behaviors.spellcaster = require "lib.character.behaviors.spellcaster"
|
||||
-- Tree.behaviors.sprite = require "lib.character.behaviors.sprite"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user