Merge pull request #70 from billmakes/prev_state_name

state_machine: include prev_state_name
This commit is contained in:
Max Cahill 2023-12-18 16:19:07 +11:00 committed by GitHub
commit 768afaf02d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,6 +33,7 @@ local state_machine = class({
function state_machine:new(states, start_in_state) function state_machine:new(states, start_in_state)
self.states = states or {} self.states = states or {}
self.current_state_name = "" self.current_state_name = ""
self.prev_state_name = ""
self.reset_state_name = start_in_state or "" self.reset_state_name = start_in_state or ""
self:reset() self:reset()
end end
@ -87,7 +88,7 @@ end
--add a state --add a state
function state_machine:add_state(name, state) function state_machine:add_state(name, state)
if self:has_state(name) then if self:has_state(name) then
error("error: added duplicate state "..name) error("error: added duplicate state " .. name)
else else
self.states[name] = state self.states[name] = state
if self:in_state(name) then if self:in_state(name) then
@ -101,7 +102,7 @@ end
--remove a state --remove a state
function state_machine:remove_state(name) function state_machine:remove_state(name)
if not self:has_state(name) then if not self:has_state(name) then
error("error: removed missing state "..name) error("error: removed missing state " .. name)
else else
if self:in_state(name) then if self:in_state(name) then
self:_call("exit") self:_call("exit")
@ -151,6 +152,7 @@ end
function state_machine:set_state(name, reset) function state_machine:set_state(name, reset)
if self.current_state_name ~= name or reset then if self.current_state_name ~= name or reset then
self:_call("exit") self:_call("exit")
self.prev_state_name = self.current_state_name
self.current_state_name = name self.current_state_name = name
self:_call_and_transition("enter", self) self:_call_and_transition("enter", self)
end end