Add basic gamestate API
parent
1c9a1f8ff3
commit
b4a8950e7d
|
@ -1,10 +1,46 @@
|
|||
lzr_gamestate = {}
|
||||
|
||||
-- List of game states
|
||||
lzr_gamestate.MENU = 1 -- When in the main menu
|
||||
lzr_gamestate.LEVEL = 2 -- When playing a level
|
||||
lzr_gamestate.EDITOR = 3 -- TODO: When making a level in the level editor
|
||||
lzr_gamestate.MENU = 1 -- When in the main menu
|
||||
lzr_gamestate.LEVEL = 2 -- When playing a level
|
||||
lzr_gamestate.LEVEL_COMPLETE = 3 -- When a level was won and waiting for the next level
|
||||
lzr_gamestate.EDITOR = 4 -- TODO: When making a level in the level editor
|
||||
|
||||
-- Always start at the MENU state
|
||||
lzr_gamestate.state = lzr_gamestate.MENU
|
||||
local current_state = lzr_gamestate.MENU
|
||||
|
||||
-- Registered callbacks
|
||||
lzr_gamestate.registered_on_enter_states = {}
|
||||
lzr_gamestate.registered_on_exit_states = {}
|
||||
|
||||
------------
|
||||
-- API calls
|
||||
------------
|
||||
|
||||
lzr_gamestate.set_state = function(new_state)
|
||||
local state_changed = current_state ~= new_state
|
||||
local old_state = current_state
|
||||
|
||||
if state_changed then
|
||||
for _, callback in pairs(lzr_gamestate.registered_on_enter_states) do
|
||||
callback(new_state)
|
||||
end
|
||||
for _, callback in pairs(lzr_gamestate.registered_on_exit_states) do
|
||||
callback(old_state)
|
||||
end
|
||||
end
|
||||
|
||||
current_state = new_state
|
||||
end
|
||||
|
||||
lzr_gamestate.get_state = function()
|
||||
return current_state
|
||||
end
|
||||
|
||||
lzr_gamestate.register_on_enter_state = function(callback)
|
||||
table.insert(lzr_gamestate.registered_on_enter_states, callback)
|
||||
end
|
||||
|
||||
lzr_gamestate.register_on_exit_state = function(callback)
|
||||
table.insert(lzr_gamestate.registered_on_exit_states, callback)
|
||||
end
|
||||
|
|
|
@ -91,7 +91,7 @@ function lzr_levels.start_level(level)
|
|||
player:set_pos({x=5,y=2,z=5})
|
||||
lzr_levels.prepare_and_build_level(level)
|
||||
lzr_messages.show_message(player, S("Level @1", level), 3)
|
||||
lzr_gamestate.state = lzr_gamestate.LEVEL
|
||||
lzr_gamestate.set_state(lzr_gamestate.LEVEL)
|
||||
end
|
||||
|
||||
function lzr_levels.next_level()
|
||||
|
@ -109,7 +109,7 @@ function lzr_levels.leave_level()
|
|||
current_level = nil
|
||||
clear_inventory(player)
|
||||
player:set_pos(vector.add(lzr_globals.MENU_SHIP_POS, lzr_globals.MENU_SHIP_PLAYER_SPAWN_OFFSET))
|
||||
lzr_gamestate.state = lzr_gamestate.MENU
|
||||
lzr_gamestate.set_state(lzr_gamestate.MENU)
|
||||
end
|
||||
|
||||
minetest.register_chatcommand("level", {
|
||||
|
@ -135,7 +135,7 @@ minetest.register_chatcommand("restart", {
|
|||
params = "",
|
||||
description = S("Restart current level"),
|
||||
func = function(name, param)
|
||||
if lzr_gamestate.state == lzr_gamestate.LEVEL then
|
||||
if lzr_gamestate.get_state() == lzr_gamestate.LEVEL then
|
||||
lzr_levels.start_level(current_level)
|
||||
return true
|
||||
else
|
||||
|
@ -149,7 +149,7 @@ minetest.register_chatcommand("leave", {
|
|||
params = "",
|
||||
description = S("Leave current level"),
|
||||
func = function(name, param)
|
||||
if lzr_gamestate.state == lzr_gamestate.LEVEL then
|
||||
if lzr_gamestate.get_state() == lzr_gamestate.LEVEL then
|
||||
lzr_levels.leave_level(current_level)
|
||||
return true
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue