1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-27 17:19:31 +00:00

Use require() to load scenes

This commit is contained in:
Étienne Fildadut 2019-12-29 17:54:18 +01:00
parent 15dfb18c65
commit a1801679b6
2 changed files with 5 additions and 37 deletions

View file

@ -1,6 +1,6 @@
project = "Ubiquitousse"
description = "Ubiquitousse Game Engine"
full_description = "A simple Lua game engine, made to run everywhere. See the Ubiquitousse module for more information."
full_description = "A simple Lua game framework, made to run everywhere. See the Ubiquitousse module for more information."
title = "Ubiquitousse Reference"
package = "ubiquitousse"

View file

@ -13,14 +13,8 @@ if not loaded then timer = nil end
-- make them scene-independent, for example by creating a scene-specific TimerRegistry (TimedFunctions that are keept accross
-- states are generally a bad idea). Theses scene-specific states should be created and available in the table returned by
-- ubiquitousse.scene.new.
-- Currently, the implementation always execute a scene's file before switching to it or adding it to the stack, but this may change in
-- the future or for some implementations (e.g., on a computer where memory isn't a problem, the scene may be put in a cache). The result
-- of this is that you can load assets, libraries, etc. outside of the enter callback, so they can be cached and not reloaded each time
-- the scene is entered, but all the other scene initialization should be done in the enter callback, since it won't be executed on
-- each enter otherwise.
-- FIXME: actually more useful to never cache?
-- The expected code-organisation is:
-- * each scene is in a file, identified by its module name (same identifier used by Lua's require)
-- * each scene is in a file, identified by its module name (scenes will be loaded using require("modulename"))
-- * each scene file create a new scene table using ubiquitousse.scene.new and returns it at the end of the file
-- Order of callbacks:
-- * all scene change callbacks are called after setting scene.current to the new scene but before changing scene.stack
@ -47,31 +41,6 @@ scene = {
-- @impl ubiquitousse
prefix = "scene.",
--- Function which load a scene file.
-- @impl ubiquitousse
load = function(sceneModule)
local scenePath = sceneModule:gsub("%.", "/")
for path in package.path:gmatch("[^;]+") do
path = path:gsub("%?", scenePath)
local f = io.open(path)
if f then
f:close()
return dofile(path)
end
end
if package.loaded["candran"] then -- Candran support
for path in package.path:gsub("%.lua", ".can"):gmatch("[^;]+") do
path = path:gsub("%?", scenePath)
local f = io.open(path)
if f then
f:close()
return package.loaded["candran"].dofile(path)
end
end
end
error("can't find scene "..tostring(sceneModule))
end,
--- Creates and returns a new Scene object.
-- @tparam[opt="unamed"] string name the new scene name
-- @impl ubiquitousse
@ -92,10 +61,9 @@ scene = {
draw = function(self, ...) end -- Called on each draw on the current scene.
}
end,
-- TODO: handle love.quit / exit all scenes in stack
--- Switch to a new scene.
-- The new scene will be loaded and the current scene will be replaced by the new one,
-- The new scene will be required() and the current scene will be replaced by the new one,
-- then the previous scene exit function will be called, then the enter callback is called on the new scence.
-- Then the stack is changed to replace the old scene with the new one.
-- @tparam string/table scenePath the new scene module name, or the scene table directly
@ -103,7 +71,7 @@ scene = {
-- @impl ubiquitousse
switch = function(scenePath, ...)
local previous = scene.current
scene.current = type(scenePath) == "string" and scene.load(scene.prefix..scenePath) or scenePath
scene.current = type(scenePath) == "string" and require(scene.prefix..scenePath) or scenePath
scene.timer = scene.current.timer
scene.signal = scene.current.signal
scene.current.name = scene.current.name or tostring(scenePath)
@ -124,7 +92,7 @@ scene = {
-- @impl ubiquitousse
push = function(scenePath, ...)
local previous = scene.current
scene.current = type(scenePath) == "string" and scene.load(scene.prefix..scenePath) or scenePath
scene.current = type(scenePath) == "string" and require(scene.prefix..scenePath) or scenePath
scene.timer = scene.current.timer
scene.signal = scene.current.signal
scene.current.name = scene.current.name or tostring(scenePath)