Created
May 2, 2021 00:31
-
-
Save vendethiel/cd8ecd0c42bf99ac4a5ccd4198382991 to your computer and use it in GitHub Desktop.
State machine
This file contains hidden or 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
package.path = package.path .. ";../?/init.lua;../?.lua" | |
require "PLoop"(function (_ENV) | |
class "__State_Machine__" (function(_ENV) | |
extend "IApplyAttribute" | |
function ApplyAttribute(self, target, targettype, manager, owner, name, stack) | |
if manager then | |
Environment.Apply(manager, function(_ENV) | |
property "State" { default = self.State } | |
end) | |
end | |
end | |
property "AttributeTarget" { default = AttributeTargets.Interface + AttributeTargets.Class } | |
property "State" { } | |
-- TODO allowed states | |
end) | |
class "__State__"(function(_ENV) | |
extend "IInitAttribute" | |
function InitDefinition(self, target, targettype, definition, owner, name, stack) | |
return function (o, ...) | |
print(o .. " [" .. (o.State or "nil") .. "] --> [" .. (self.To or "nil") .. "]") | |
if o.State == self.From then | |
definition(o, ...) | |
print(" [" .. (o.State or "nil") .. "] --> [" .. (self.To or "nil") .. "]") | |
o.State = self.To | |
else | |
throw("Cannot start with a state [" .. (o.State or "nil") .. "], expected state [" .. (self.From or "nil") .. "]") | |
end | |
end | |
end | |
property "From" {} | |
property "To" {} | |
end) | |
__State_Machine__{ State = "Start" } | |
class "MyMachine"(function(_ENV) | |
__State__{ From = "Start", To = "Stop" } | |
function SetState() | |
end | |
end) | |
local instance = MyMachine() | |
print("iz "..instance.State) | |
MyMachine:SetState() | |
MyMachine:SetState() | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.