statemachine.lua
Posted: Tue Nov 20, 2012 9:42 am
I needed a state machine for my game. All the libraries I found didn't suit my needs, so I wrote my own
https://github.com/kyleconroy/lua-state-machine
Basic example creating a stoplight state machine.
https://github.com/kyleconroy/lua-state-machine
Basic example creating a stoplight state machine.
Code: Select all
local machine = require('statemachine')
local fsm = machine.create({
initial = 'green',
events = {
{ name = 'warn', from = 'green', to = 'yellow' },
{ name = 'panic', from = 'yellow', to = 'red' },
{ name = 'calm', from = 'red', to = 'yellow' },
{ name = 'clear', from = 'yellow', to = 'green' }
}})
fsm:warn() -- transition from 'green' to 'yellow'
fsm:panic() -- transition from 'yellow' to 'red'
fsm:calm() -- transition from 'red' to 'yellow'
fsm:clear() -- transition from 'yellow' to 'green'