Last active
September 19, 2019 17:24
-
-
Save Nimblz/3c854e18318c6db2ce516c071c725d4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- trying to make an ergonomic state machine | |
-- maybe im just making madness | |
local FSM = {} | |
function FSM.new(states,startState) | |
local self = setmetatable({},{__index=FSM}) | |
self.states = states | |
self:transition(startState) | |
return self | |
end | |
function FSM:transition(newState, ...) | |
if not newState then return end | |
local targetState = self.states[newState] | |
self.currentState = targetState | |
return self:transition(targetState.enter(...)) | |
end | |
function FSM:step(...) | |
if not self.currentState.step then return end | |
if not typeof(self.currentState.step) == "function" then return end | |
self:transition(self.currentState.step(...)) | |
end | |
return FSM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment