feat(scenes): handle scene transitions
This commit is contained in:
parent
8385a69636
commit
4b24579070
2 changed files with 57 additions and 3 deletions
|
@ -43,6 +43,7 @@ function Scene:new()
|
||||||
self.inputLocked = true
|
self.inputLocked = true
|
||||||
self.inputLockedTimer = 2
|
self.inputLockedTimer = 2
|
||||||
self:flushKeys()
|
self:flushKeys()
|
||||||
|
self.isActive = false
|
||||||
|
|
||||||
self:initWorld()
|
self:initWorld()
|
||||||
|
|
||||||
|
@ -136,7 +137,7 @@ end
|
||||||
-- Handle inputs from keyboard/controllers
|
-- Handle inputs from keyboard/controllers
|
||||||
|
|
||||||
function Scene:setKeys()
|
function Scene:setKeys()
|
||||||
if (self.inputLocked) then
|
if (self.inputLocked) or (not self.isActive) then
|
||||||
self.inputLockedTimer = self.inputLockedTimer - 1
|
self.inputLockedTimer = self.inputLockedTimer - 1
|
||||||
if (self.inputLockedTimer <= 0 ) then
|
if (self.inputLockedTimer <= 0 ) then
|
||||||
self.inputLocked = false
|
self.inputLocked = false
|
||||||
|
@ -154,7 +155,7 @@ function Scene:getKeys(sourceid)
|
||||||
end
|
end
|
||||||
|
|
||||||
local sourceid = sourceid or 1
|
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")
|
core.debug:print("scene", "inputs are currently locked")
|
||||||
return core.input.fakekeys
|
return core.input.fakekeys
|
||||||
else
|
else
|
||||||
|
|
|
@ -30,13 +30,21 @@ local SceneManager = Object:extend()
|
||||||
|
|
||||||
function SceneManager:new(controller)
|
function SceneManager:new(controller)
|
||||||
self.controller = controller
|
self.controller = controller
|
||||||
|
self.timers = self.controller.modules.Timers(self)
|
||||||
self.currentScene = nil
|
self.currentScene = nil
|
||||||
|
|
||||||
self.storage = {}
|
self.storage = {}
|
||||||
|
|
||||||
|
self:initTransitions()
|
||||||
end
|
end
|
||||||
|
|
||||||
function SceneManager:setScene(scene)
|
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
|
end
|
||||||
|
|
||||||
function SceneManager:storeCurrentScene(name)
|
function SceneManager:storeCurrentScene(name)
|
||||||
|
@ -63,6 +71,7 @@ end
|
||||||
-- Update the current scene and its subobjects
|
-- Update the current scene and its subobjects
|
||||||
|
|
||||||
function SceneManager:update(dt)
|
function SceneManager:update(dt)
|
||||||
|
self.timers:update(dt)
|
||||||
if (self.currentScene ~= nil) then
|
if (self.currentScene ~= nil) then
|
||||||
self.currentScene:updateStart(dt)
|
self.currentScene:updateStart(dt)
|
||||||
self.currentScene:setKeys()
|
self.currentScene:setKeys()
|
||||||
|
@ -104,6 +113,50 @@ function SceneManager:keyreleased( key )
|
||||||
self.currentScene:keyreleased( key )
|
self.currentScene:keyreleased( key )
|
||||||
end
|
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 FUNCTIONS
|
||||||
-- Draw the current scene
|
-- Draw the current scene
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue