improvement: manage music via core.music

This commit is contained in:
Kazhnuz 2021-05-05 14:49:14 +02:00
parent b00f73c37f
commit 06f50c5e59
3 changed files with 120 additions and 13 deletions

View file

@ -34,6 +34,7 @@ local Input = require(cwd .. "input")
local Screen = require(cwd .. "screen") local Screen = require(cwd .. "screen")
local Lang = require(cwd .. "lang") local Lang = require(cwd .. "lang")
local SceneManager = require(cwd .. "scenemanager") local SceneManager = require(cwd .. "scenemanager")
local MusicManager = require(cwd .. "music")
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialize and configure the core object -- Initialize and configure the core object
@ -47,6 +48,7 @@ function CoreSystem:new()
self.screen = Screen(self) self.screen = Screen(self)
self.scenemanager = SceneManager(self) self.scenemanager = SceneManager(self)
self.lang = Lang(self) self.lang = Lang(self)
self.music = MusicManager(self)
end end
function CoreSystem:setDefaultConf() function CoreSystem:setDefaultConf()
@ -113,6 +115,7 @@ function CoreSystem:update(dt)
self.scenemanager:update(dt) self.scenemanager:update(dt)
self.screen:update(dt) self.screen:update(dt)
self.music:update(dt)
end end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS

View file

@ -0,0 +1,103 @@
-- core/music.lua :: A music system, in order to add some usefull features
-- and to make music not scene dependant
--[[
Copyright © 2020 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local MusicManager = Object:extend()
function MusicManager:new(core)
self.core = core
self.musics = {}
self.isPlaying = false
self.playNextMusic = false
end
function MusicManager:update(dt)
if (self.isPlaying and (self.musics[1] ~= nil)) then
if (not self.musics[1]:isPlaying()) then
if (self.playNextMusic and (self.musics[1] ~= nil)) then
self:skip()
self:playMusic()
self.playNextMusic = false
else
self.isPlaying = false
end
end
end
end
function MusicManager:getCurrentMusic()
return self.musics[1]
end
function MusicManager:haveMusic()
return (self.musics[1] ~= nil)
end
function MusicManager:playMusic()
if (self:haveMusic()) then
self.musics[1]:setVolume(self.core.options.data.audio.music / 100)
love.audio.play( self.musics[1] )
self.isPlaying = true
end
end
function MusicManager:isPlaying()
if (self:haveMusic()) then
return self.musics[1]:isPlaying( )
else
return false
end
end
function MusicManager:addMusic(filename, loop)
local music = love.audio.newSource(filename, "stream")
music:setLooping( loop )
table.insert(self.musics, music)
end
function MusicManager:silence()
if (self:haveMusic()) then
self.musics[1]:stop()
self.isPlaying = false
end
end
function MusicManager:skip()
if (self:haveMusic()) then
self.musics[1]:stop()
self.isPlaying = false
table.remove(self.musics, 1)
end
end
function MusicManager:purge()
if (self:haveMusic()) then
self.musics[1]:stop()
self.isPlaying = false
self.musics = {}
end
end
return MusicManager

View file

@ -167,28 +167,29 @@ function Assets:playSFX(filename)
end end
end end
function Assets:setMusic(filename) -- MUSIC FUNCTIONS
if filename ~= nil then -- All thse functions are deprecated, prefer core.music
if (self.music ~= nil) then
self.music:stop() function Assets:setMusic(filename, loop)
end core.debug:warning("assets", "Assets:setMusic is deprecated")
self.music = love.audio.newSource(filename, "stream" ) core.music:skip()
self.music:setVolume(core.options.data.audio.music / 100) core.music:addMusic(filename, (loop ~= false))
end core.music:playMusic()
end end
function Assets:silence() function Assets:silence()
love.audio.stop() core.debug:warning("assets", "Assets:silence is deprecated")
core.music:silence()
end end
function Assets:resetMusic() function Assets:resetMusic()
self.music = nil core.debug:warning("assets", "Assets:resetMusic is deprecated")
core.music:purge()
end end
function Assets:playMusic() function Assets:playMusic()
if not (self.music == nil) then core.debug:warning("assets", "Assets:playMusic is deprecated")
love.audio.play(self.music) core.music:playMusic()
end
end end
-- IMAGES FUNCTIONS -- IMAGES FUNCTIONS