2019-03-16 12:27:38 +01:00
|
|
|
-- scene.lua :: a basic scene management system, that work by sending the different
|
|
|
|
-- core functions to the scene, normally without the scene itself having to manage
|
|
|
|
-- them.
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Copyright © 2019 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 SceneManager = Object:extend()
|
|
|
|
|
2019-04-11 17:23:39 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialize and configure the scene manager
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function SceneManager:new(controller)
|
|
|
|
self.controller = controller
|
|
|
|
self.currentScene = nil
|
2019-03-16 15:15:01 +01:00
|
|
|
|
|
|
|
self.storage = {}
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2019-03-16 15:04:55 +01:00
|
|
|
function SceneManager:setScene(scene)
|
|
|
|
self.currentScene = scene
|
|
|
|
end
|
|
|
|
|
2019-03-16 15:15:01 +01:00
|
|
|
function SceneManager:storeCurrentScene(name)
|
|
|
|
self.storage[name] = self.currentScene
|
|
|
|
end
|
|
|
|
|
|
|
|
function SceneManager:setStoredScene(name)
|
|
|
|
local storedScene = self.storage[name]
|
|
|
|
if storedScene ~= nil then
|
|
|
|
self.currentScene = storedScene
|
|
|
|
self.storage[name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function SceneManager:clearStorage()
|
|
|
|
self.storage = {}
|
|
|
|
end
|
|
|
|
|
2019-04-11 17:23:39 +02:00
|
|
|
function SceneManager:clearScene()
|
|
|
|
self.currentScene = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
-- Update the current scene and its subobjects
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function SceneManager:update(dt)
|
|
|
|
if (self.currentScene ~= nil) then
|
2019-06-13 18:33:29 +02:00
|
|
|
self.currentScene:updateStart(dt)
|
2019-04-01 08:25:02 +02:00
|
|
|
self.currentScene:setKeys()
|
2019-03-16 12:27:38 +01:00
|
|
|
self.currentScene.assets:update(dt)
|
|
|
|
self.currentScene.menusystem:update(dt)
|
2019-05-30 13:27:41 +02:00
|
|
|
self.currentScene:updateWorld(dt)
|
2019-03-16 12:27:38 +01:00
|
|
|
self.currentScene:update(dt)
|
2019-06-13 18:33:29 +02:00
|
|
|
self.currentScene:updateEnd(dt)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-11 17:23:39 +02:00
|
|
|
-- MOUSE FUNCTIONS
|
|
|
|
-- Send pointer data to the scene
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function SceneManager:mousemoved(x, y, dx, dy)
|
2019-03-16 14:43:37 +01:00
|
|
|
if (self.currentScene ~= nil) then
|
|
|
|
self.currentScene.mouse.x,
|
|
|
|
self.currentScene.mouse.y = x, y
|
|
|
|
self.currentScene:mousemoved(x, y, dx, dy)
|
|
|
|
self.currentScene.menusystem:mousemoved(x, y, dx, dy)
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function SceneManager:mousepressed( x, y, button, istouch )
|
2019-03-16 14:43:37 +01:00
|
|
|
if (self.currentScene ~= nil) then
|
|
|
|
self.currentScene:mousepressed( x, y, button, istouch )
|
|
|
|
self.currentScene.menusystem:mousepressed( x, y, button, istouch )
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2019-04-14 19:45:52 +02:00
|
|
|
-- KEYBOARD FUNCTIONS
|
|
|
|
-- Add send keys functions to the scene
|
|
|
|
|
|
|
|
function SceneManager:keypressed( key, scancode, isrepeat )
|
|
|
|
self.currentScene:keypressed( key, scancode, isrepeat )
|
|
|
|
end
|
|
|
|
|
|
|
|
function SceneManager:keyreleased( key )
|
|
|
|
self.currentScene:keyreleased( key )
|
|
|
|
end
|
|
|
|
|
2019-04-11 17:23:39 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw the current scene
|
2019-03-16 12:27:38 +01:00
|
|
|
|
|
|
|
function SceneManager:draw()
|
|
|
|
self.controller.screen:apply()
|
|
|
|
if (self.currentScene ~= nil) then
|
2019-06-13 18:33:29 +02:00
|
|
|
self.currentScene:drawStart()
|
2019-05-30 13:37:30 +02:00
|
|
|
self.currentScene:drawWorld()
|
2019-06-13 18:33:29 +02:00
|
|
|
self.currentScene:draw()
|
2019-03-16 12:27:38 +01:00
|
|
|
self.currentScene.menusystem:draw()
|
2019-06-13 18:33:29 +02:00
|
|
|
self.currentScene:drawEnd()
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
self.controller.screen:cease()
|
|
|
|
end
|
|
|
|
|
|
|
|
return SceneManager
|