Game State Machine "StateManager"

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
darkfrei
Party member
Posts: 1250
Joined: Sat Feb 08, 2020 11:09 pm

Game State Machine "StateManager"

Post by darkfrei »

Hi everyone!

It was always interesting to make the finite state machine for game modules in Love2D.

There is the StateManager!

Code: Select all

-- state-manager.lua

local StateManager = {}
StateManager.currentState = nil

function StateManager.switchState(newState)
	-- [switches to a new state and initializes it]
	if StateManager.currentState and StateManager.currentState.exit then
		StateManager.currentState.exit()
	end
	
	StateManager.currentState = newState
	
	if StateManager.currentState and StateManager.currentState.enter then
		StateManager.currentState.enter()
	end
	
	for i, v in pairs (StateManager) do
		print ('StateManager', i, v)
	end
end

function StateManager.update(dt)
	-- [updates the current state]
	if StateManager.currentState and StateManager.currentState.update then
		StateManager.currentState.update(dt)
	end
end

function StateManager.draw()
	-- [draws the current state]
	if StateManager.currentState and StateManager.currentState.draw then
		StateManager.currentState.draw()
	end
end

function StateManager.handleEvent(event, ...)
	-- [handles events in the current state]
	if StateManager.currentState and StateManager.currentState[event] then
		StateManager.currentState[event](...)
	end
end

return StateManager
All you need is to call it in the main.lua and set the love functions to it as shown:

Code: Select all

-- main.lua

StateManager = require("state-manager")
Editor = require("editor") -- example state


function love.load()
	-- [sets the initial state to editor]
	StateManager.switchState(Editor)
end

function love.update(dt)
	-- [updates the current state]
	StateManager.update(dt)
end

function love.draw()
	-- [renders the current state]
	StateManager.draw()
end

function love.keypressed(key)
	-- [handles keypress events in the current state]
	StateManager.handleEvent("keypressed", key)
end

function love.mousepressed(x, y, button)
	-- [handles mouse press events in the current state]
	StateManager.handleEvent("mousepressed", x, y, button)
end

function love.mousereleased(x, y, button)
	-- [handles mouse release events in the current state]
	StateManager.handleEvent("mousereleased", x, y, button)
end

function love.mousemoved(x, y, dx, dy)
	-- [handles mouse move events in the current state]
	StateManager.handleEvent("mousemoved", x, y, dx, dy)
end

function love.wheelmoved(x, y)
    -- [handles mouse wheel events]
    StateManager.handleEvent("wheelmoved", x, y)
end
It's all! Now the editor.lua can be just:

Code: Select all

-- editor.lua

function Editor.enter()
	-- [initializes editor state]
	print("Editor state entered")
end

function Editor.update(dt)
	-- [updates editor logic]
end

function Editor.draw()
	-- [draws editor]
end

function Editor.exit()
	-- [cleans up editor state]
	print("Editor state exited")
end

function Editor.keypressed(key, scancode)

end

function Editor.mousepressed(x, y, button)

end

function Editor.mousemoved(x, y, dx, dy)

end

function Editor.wheelmoved(x, y)

end

return Editor -- most important thing in the file!
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 765
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Game State Machine "StateManager"

Post by dusoft »

Hello,

it's an interesting exercise, I tried it once too.

For the development purposes I use HUMP's Gamestate that is feature-complete as far as I am concerned:
https://hump.readthedocs.io/en/latest/g ... -functions
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest