-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_better.luau
171 lines (144 loc) · 3.98 KB
/
main_better.luau
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
type State = {
onEnter: (context: any) -> (),
onExit: (context: any) -> (),
onUpdate: (context: any, deltaTime: number) -> (),
}
type Config = {
[string]: FSM | State
}
type FSM = {
currentState: string,
_transitions: {
[string]: {
[string]: (context: string) -> bool
}
},
_context: {any},
_states: Config,
addTransition: (self: FSM, from: string, to: string, condition: (context: {any}) -> boolean) -> (),
addTwoWayTransition: (self: FSM, from: string, to: string, condition: (context: {any}) -> boolean) -> (),
getChildOf: (self: FSM, state: string) -> FSM?,
setDefaultState: (self: FSM, state: string) -> (),
setContext: (self: FSM, context: {any}) -> (),
patchContext: (self: FSM, context: {any}) -> (),
update: (self: FSM, deltaTime: number) -> ()
}
local function addTransition(self: FSM, from: string, to: string, condition: (context: {any}) -> boolean): ()
local transitions = self._transitions
if not transitions[from] then
transitions[from] = {}
end
transitions[from][to] = condition
end
local function addTwoWayTransition(self: FSM, from: string, to: string, condition: (context: {any}) -> boolean): ()
addTransition(self, from, to, condition)
addTransition(self, to, from, function(context)
return not condition(context)
end)
end
local function getChildOf(self: FSM, state: string): FSM?
local child = self._states[state]
return child.parent and child or nil
end
local function setContext(self: FSM, context: {any}): ()
self._context = context
end
local function patchContext(self: FSM, context: {any}): ()
for k, v in context do
self._context[k] = v
end
end
local function setDefaultState(self: FSM, state: string): ()
if self.currentState == nil then
self.currentState = state
end
end
local function update(self: FSM, deltaTime: number): ()
local from = self.currentState
local context = self._context
local states = self._states
local state = states[from]
for to, condition in self._transitions[from] do
if condition(context) then
local newState = states[to]
local onExit = (state.parent or state).onExit
local onEnter = (newState.parent or newState).onEnter
self.currentState = to
state = newState
onExit(context)
onEnter(context)
end
end
if state.parent then
state.parent.onUpdate(context, deltaTime)
state:update()
else
state.onUpdate(context, deltaTime)
end
end
local function create(parentState: State, config: Config): FSM
local fsm = {
_transitions = {},
_context = {},
_states = config,
currentState = nil,
parent = parentState
}
fsm.addTransition = addTransition
fsm.addTwoWayTransition = addTwoWayTransition
fsm.getChildOf = getChildOf
fsm.setDefaultState = setDefaultState
fsm.setContext = setContext
fsm.patchContext = patchContext
fsm.update = update
return fsm
end
local function construct(name)
return {
onUpdate = function()
print("updated", name)
end,
onEnter = function()
print("entered", name)
end,
onExit = function()
print("exited", name)
end
}
end
local physics = construct("Physics")
local grounded = construct("Grounded")
local idle = construct("Idle")
local walk = construct("Walk")
local fsm = create(nil, {
Physics = create(physics, {
Walk = walk,
Idle = idle
}),
Grounded = grounded
})
fsm:setDefaultState("Grounded")
fsm:addTransition("Grounded", "Physics", function()
return true
end)
fsm:addTransition("Physics", "Gronded", function()
return false
end)
local physicsFSM = fsm:getChildOf("Physics")
physicsFSM:setContext({xVelocity = 0})
physicsFSM:setDefaultState("Walk")
physicsFSM:addTwoWayTransition("Walk", "Idle", function(context)
return context.xVelocity <= 0
end)
fsm:update() --[[output
exited Grounded
entered Physics
updated Physics
exited Walk
entered Idle
updated Idle
]]
fsm:update() --[[output
updated Physics
updated Idle
]]