diff --git a/src/game/gui/init.lua b/src/game/gui/init.lua index 1bbb4e9..f09ecc4 100644 --- a/src/game/gui/init.lua +++ b/src/game/gui/init.lua @@ -170,6 +170,13 @@ function GUI:back() end end +function GUI:showMenu() + if self.infoPanel:isShown() then + self:_closeInfoPanel() + end + GameState.push(InGameMenu) +end + function GUI:update(dt) if self.menuButton:isPressed() then local x, y = screen:getCoordinate(love.mouse.getPosition()) diff --git a/src/game/init.lua b/src/game/init.lua index 156e48f..4e9544a 100644 --- a/src/game/init.lua +++ b/src/game/init.lua @@ -294,14 +294,17 @@ function Game:leave() self:_save() Timer.clear() self.engine.systemRegistry["SoundSystem"]:stopAll() + soundManager:stopAll() end function Game:pause() self.engine.systemRegistry["SoundSystem"]:pauseAll() + soundManager:pauseAll() end function Game:resume() self.engine.systemRegistry["SoundSystem"]:playAll() + soundManager:resumeAll() end function Game:quit() @@ -598,9 +601,15 @@ end function Game:focus(focused) local os = love.system.getOS() - -- Focus is lost on mobile when switching out of the app, so best to save now. - if not focused and (os == "Android" or os == "iOS") then - self.saveGameTimer:update(self.saveGameHandle.limit - self.saveGameHandle.time) + if os == "Android" or os == "iOS" then + if focused then + soundManager:playMusic() + else + -- Focus is lost on mobile when switching out of the app, so best to save now. + self.saveGameTimer:update(self.saveGameHandle.limit - self.saveGameHandle.time) + self.gui:showMenu() + soundManager:pauseMusic() + end end end diff --git a/src/ingamemenu/init.lua b/src/ingamemenu/init.lua index f61ce90..3e5bd20 100644 --- a/src/ingamemenu/init.lua +++ b/src/ingamemenu/init.lua @@ -76,6 +76,10 @@ function InGameMenu:leave() soundManager:playEffect("main_menu_closed") end +function InGameMenu:focus(f) + self.oldState:focus(f) +end + function InGameMenu:update(dt) for _,button in ipairs(self.buttons) do if button:isPressed() then diff --git a/src/mainmenu/init.lua b/src/mainmenu/init.lua index 6214c43..59c1faf 100644 --- a/src/mainmenu/init.lua +++ b/src/mainmenu/init.lua @@ -30,6 +30,7 @@ local Button = require "src.game.gui.button" local ScaledSprite = require "src.game.scaledsprite" local screen = require "src.screen" +local soundManager = require "src.soundmanager" local spriteSheet = require "src.game.spritesheet" local MainMenu = {} @@ -115,6 +116,8 @@ end function MainMenu:enter(previous, init) self:reevaluate() + soundManager:playMusic() + if init and self.latest then return GameState.switch(Game, self.latest) end diff --git a/src/soundmanager.lua b/src/soundmanager.lua index f6dcb8a..d729ee1 100644 --- a/src/soundmanager.lua +++ b/src/soundmanager.lua @@ -90,7 +90,6 @@ function SoundManager:initialize() self.music = love.audio.newSource("asset/sfx/"..SoundManager.MUSIC, "stream") self.music:setLooping(true) - self.music:play() self:setEffectVolume(self:getEffectVolume()) self:setMusicVolume(self:getMusicVolume()) @@ -160,6 +159,38 @@ function SoundManager:playEffect(effect, gi, gj) source:play() end +function SoundManager:playMusic() + self.music:play() +end + +function SoundManager:pauseMusic() + self.music:pause() +end + +function SoundManager:pauseAll() + for _,source in ipairs(self.sources) do + if source:isPlaying() then + source:pause() + end + end +end + +function SoundManager:resumeAll() + for _,source in ipairs(self.sources) do + if source:isPlaying() then + source:play() + end + end +end + +function SoundManager:stopAll() + for _,source in ipairs(self.sources) do + if source:isPlaying() then + source:stop() + end + end +end + function SoundManager:_getFreeSource() for _,source in ipairs(self.sources) do if source:getFreeBufferCount() > 0 then