Separate state files
Posted: Tue Feb 21, 2017 7:00 pm
Is it possible to create game projects with separate files for each states with each having their own callback functions like phaser game engine?
Thanks.
Thanks.
Code: Select all
-- game.lua
local Gamestate = require "hump.gamestate"
local game = {}
function game:enter()
Entities.clear()
-- setup entities here
end
function game:update(dt)
Entities.update(dt)
end
function game:draw()
Entities.draw()
end
return game
-----------------------------------------------------------------
-- menu.lua
local Gamestate = require "hump.gamestate"
local game
local menu = {}
function menu:init()
game = require "game"
end
function menu:draw()
love.graphics.print("Press Enter to continue", 10, 10)
end
function menu:keyreleased(key, code)
if key == 'return' then
Gamestate.switch(game)
end
end
return menu
-----------------------------------------------------------------
-- main.lua
local Gamestate = require "hump.gamestate"
local menu, game
function love.load()
menu = require "menu"
game = require "game" -- This is only needed because of the registerEvents call below; if you're doing stuff manually, then this can be avoided here.
Gamestate.registerEvents()
Gamestate.switch(menu)
end
link is broken, it's supposed to be: https://github.com/rm-code/screenmanager
Code: Select all
STATE = require "menu" -- start with this state
function load_state(new_state)
if STATE.exit then STATE.exit() end
STATE = new_state
if STATE.enter then STATE.enter() end
end
function love.draw()
if STATE.draw then STATE.draw() end
end
function love.update(dt)
if STATE.update then STATE.update(dt) end
end
function love.keypressed(...)
if STATE.keypressed then STATE.keypressed(...) end
end
-- etc
Code: Select all
local selected_menu_entry
menu_entries = { "start", "leave" }
return {
enter = function ()
selected_menu_entry = 0
end,
keypressed = function (key)
if key == "up" then
selected_menu_entry = selected_menu_entry - 1
elseif key == "down"
selected_menu_entry = selected_menu_entry + 1
else
if menu_entries[selected_menu_entry] == "start" then
load_state(require "game")
end
end
end,
draw = function ()
love.graphics.print(menu_entries[selected_menu_entry])
end
}
Code: Select all
return {
draw = function()
-- draw game...
end,
update = function (dt)
-- update game...
end,
...
}
The thing I like about your solution is the ability to propagate some callbacks upward (drawing) and others downward (input). I'm not sure how the others handle that. If you don't have a lot of layers of stuff, though, maybe just a floating pause menu or something, you don't really even need a stack, you can just have your pause state hold a reference to the game state and have it draw your game state before it draws itself. If you don't need a stack, you probably don't really need a library either. Should be easy enough to have something like "game.activeState" and set that to whatever state instance you want.s-ol wrote:i have my own on github too but I think you really dont need it.