From e8e75ac4db93512f8c178ef487daf1242b2355ca Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 9 Aug 2020 14:51:52 +0200 Subject: [PATCH] improvement: manage music via core.music Fix #43 --- CHANGELOG.md | 2 + birb/core/init.lua | 3 + birb/core/music.lua | 103 +++++++++++++++++++++++++++++++++++ birb/modules/assets/init.lua | 20 +++---- 4 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 birb/core/music.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a9482c..c008a23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **core:** The core is now separated from the rest of birb +- **core+assets:** Music are now managed directly by the core + ## [0.6.0] - 2019-07-20 - Meta: Add proper crediting diff --git a/birb/core/init.lua b/birb/core/init.lua index 1608b59..8f374bb 100644 --- a/birb/core/init.lua +++ b/birb/core/init.lua @@ -33,6 +33,7 @@ local Input = require(cwd .. "input") local Screen = require(cwd .. "screen") local Lang = require(cwd .. "lang") local SceneManager = require(cwd .. "scenemanager") +local MusicManager = require(cwd .. "music") -- INIT FUNCTIONS -- Initialize and configure the core object @@ -49,6 +50,7 @@ function CoreSystem:new(debugLevel) self.screen = Screen(self) self.scenemanager = SceneManager(self) self.lang = Lang(self) + self.music = MusicManager(self) self.debug:logDebug("birbcore","Birb initialized") end @@ -103,6 +105,7 @@ function CoreSystem:update(dt) self.debug:update(dt) self.input:update(dt) self.screen:update(dt) + self.music:update(dt) if (self.game ~= nil) then self.game:update(dt) diff --git a/birb/core/music.lua b/birb/core/music.lua new file mode 100644 index 0000000..9556677 --- /dev/null +++ b/birb/core/music.lua @@ -0,0 +1,103 @@ +-- core/langs.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:skipMusic() + 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 diff --git a/birb/modules/assets/init.lua b/birb/modules/assets/init.lua index 84b0e86..700b139 100644 --- a/birb/modules/assets/init.lua +++ b/birb/modules/assets/init.lua @@ -167,26 +167,24 @@ function Assets:playSFX(filename) end end -function Assets:setMusic(filename) - if filename ~= nil then - love.audio.stop( ) - self.music = love.audio.newSource(filename, "stream" ) - self.music:setVolume(core.options.data.audio.music / 100) - end +function Assets:setMusic(filename, loop) + core.debug:logDebug("assets", "Assets:setMusic is deprecated") + core.music:addMusic(filename, true) end function Assets:silence() - love.audio.stop() + core.debug:logDebug("assets", "Assets:silence is deprecated") + core.music:silence() end function Assets:resetMusic() - self.music = nil + core.debug:logDebug("assets", "Assets:resetMusic is deprecated") + core.music:purge() end function Assets:playMusic() - if not (self.music == nil) then - love.audio.play(self.music) - end + core.debug:logDebug("assets", "Assets:playMusic is deprecated") + core.music:playMusic() end -- IMAGES FUNCTIONS