From 4b24579070e9d33044ef9cefbb05e2e2ac117136 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 8 Sep 2019 16:23:30 +0200 Subject: [PATCH] feat(scenes): handle scene transitions --- gamecore/modules/scenes.lua | 5 ++-- gamecore/scenemanager.lua | 55 ++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/gamecore/modules/scenes.lua b/gamecore/modules/scenes.lua index 3484df6..c615f6f 100644 --- a/gamecore/modules/scenes.lua +++ b/gamecore/modules/scenes.lua @@ -43,6 +43,7 @@ function Scene:new() self.inputLocked = true self.inputLockedTimer = 2 self:flushKeys() + self.isActive = false self:initWorld() @@ -136,7 +137,7 @@ end -- Handle inputs from keyboard/controllers function Scene:setKeys() - if (self.inputLocked) then + if (self.inputLocked) or (not self.isActive) then self.inputLockedTimer = self.inputLockedTimer - 1 if (self.inputLockedTimer <= 0 ) then self.inputLocked = false @@ -154,7 +155,7 @@ function Scene:getKeys(sourceid) end local sourceid = sourceid or 1 - if (self.inputLocked) then + if (self.inputLocked) or (not self.isActive) then core.debug:print("scene", "inputs are currently locked") return core.input.fakekeys else diff --git a/gamecore/scenemanager.lua b/gamecore/scenemanager.lua index b1a8af6..69e4878 100644 --- a/gamecore/scenemanager.lua +++ b/gamecore/scenemanager.lua @@ -30,13 +30,21 @@ local SceneManager = Object:extend() function SceneManager:new(controller) self.controller = controller + self.timers = self.controller.modules.Timers(self) self.currentScene = nil self.storage = {} + + self:initTransitions() end function SceneManager:setScene(scene) - self.currentScene = scene + if self.transition.isPrepared then + self:startTransition(scene) + else + self.currentScene = scene + self.currentScene.isActive = true + end end function SceneManager:storeCurrentScene(name) @@ -63,6 +71,7 @@ end -- Update the current scene and its subobjects function SceneManager:update(dt) + self.timers:update(dt) if (self.currentScene ~= nil) then self.currentScene:updateStart(dt) self.currentScene:setKeys() @@ -104,6 +113,50 @@ function SceneManager:keyreleased( key ) self.currentScene:keyreleased( key ) end +-- TRANSITION FUNCTIONS +-- Prepare transitionning to the next scene + +function SceneManager:initTransitions() + self.transition = {} + + self.transition.easeIn = "inQuad" + self.transition.easeOut = "outQuad" + self.transition.duration = 1 + + self.transition.nextScene = nil + self.transition.isPrepared = false +end + +function SceneManager:prepareTransition(duration, easeIn, easeOut) + self.transition.easeIn = easeIn or self.transition.easeIn + self.transition.easeOut = easeOut or self.transition.easeOut + self.transition.duration = duration or self.transition.duration + + self.transition.isPrepared = true +end + +function SceneManager:startTransition(nextScene) + self.transition.nextScene = nextScene + self.currentScene:flushKeys(self.transition.duration) + self.currentScene.isActive = false + self.transition.nextScene.isActive = false + core.screen:fadeIn(self.transition.duration / 2.5, self.transition.easeIn) + self.timers:newTimer(self.transition.duration / 2, "fadeOut") +end + +function SceneManager:timerResponse(timer) + if timer == "fadeOut" then + self.currentScene = self.transition.nextScene + self.currentScene:flushKeys(self.transition.duration / 2.5) + self.currentScene.isActive = false + core.screen:fadeOut(self.transition.duration / 2.5, self.transition.easeOut) + self.transition.isPrepared = false + self.timers:newTimer(self.transition.duration / 2.5, "activateScene") + elseif timer == 'activateScene' then + self.currentScene.isActive = true + end +end + -- DRAW FUNCTIONS -- Draw the current scene