I wouldn't even call that a scene management function. It's a loader.
In the sense that you mean, scenes are more often called gamestates. "Scene" is a term more commonly used for a tree of transformations and sprites, and is less commonly used in 2D (such as love) than in 3D (such as amulet or irrlicht). Look for gamestate libraries, there are a number of them out there. One of the most famous gamestate management libraries is probably hump. I like to keep gamestate management very simple (but not just a loader), but for certain projects more sophisticated management is necessary.
In a basic gamestate manager, the idea is that the events such as love.draw, love.update, love.keypressed etc. call the corresponding events of the currently selected gamestate (or call it screen, if you want). Some gamestates may not need certain events, so you usually have to protect the calls against the case that the event does not exist in that gamestate.
For example, in a simple gamestate manager, a game with three gamestates (menu, game, game over) could look like this:
states.lua:
Code: Select all
local states = {}
function states.changeTo(newState)
states.state = newState
end
return states
main.lua:
Code: Select all
local states = require('states') -- load the states library
states.menu = require('menu') -- load a file with the definition of the menu state
states.game = require('game') -- load a file with the game itself
states.gameover = require('gameover') -- load a file with the definition of the gameover state
states.changeTo(states.menu) -- set the menu state at start
-- Let's imagine that no gamestates use any events other than update, draw, and keypressed
function love.keypressed(...)
if states.state.keypressed then -- if the keypressed event exists in the current state
states.state.keypressed(...) -- call it with all arguments received
end
end
function love.update(dt)
if states.state.update then -- if the update event exists in the current state
states.state.update(dt) -- call it
end
end
function love.draw()
if states.state.draw then -- if the draw event exists in the current state
states.state.draw() -- call it
end
end
menu.lua:
Code: Select all
local states = require('states')
local menu = {}
function menu.keypressed(k, s)
-- key "1" starts the game
if s == "1" then
states.changeTo(states.game) -- set the "game" state
end
end
function menu.draw()
love.graphics.print("Menu\nPress 1 to start game\nThere are no other options!")
end
return menu
game.lua:
Code: Select all
local states = require('states')
local game = {}
local time = 0
function game.keypressed(k, s)
if s == "space" then
_G["WON"] = true -- won the game
states.changeTo(states.gameover)
end
end
function game.update(dt)
time = time + dt
if time >= 2 then
_G["WON"] = false -- set the WON global to false
states.changeTo(states.gameover)
end
end
function game.draw()
love.graphics.print("Press space in less than two seconds to win!")
end
return game
gameover.lua:
Code: Select all
local states = require('states')
local gameover = {}
local wonlost
if _G["WON"] then
wonlost = "won"
else
wonlost = "lost"
end
function gameover.keypressed(k, s)
if s == "escape" then return love.event.quit() end
end
function gameover.draw()
love.graphics.print("You " .. wonlost .. "!\nPress escape to exit")
end
return gameover
That's a VERY barebones example. You may want to add events to reinitialize the state (like the counter in the game screen, in order to be able to play again without restarting the whole thing). Also events to load state-specific assets. Also events to pause and resume the current state, which you can use for example to pause/resume the game music. It's very flexible and you can add a lot of things with little effort.