-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstateMachine.lua
More file actions
62 lines (53 loc) · 1.66 KB
/
Copy pathstateMachine.lua
File metadata and controls
62 lines (53 loc) · 1.66 KB
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
-- ##################################################################
-- # Package: State Machine
-- # This package provides state machine code that can be used by
-- # other packages
-- ##################################################################
local stateMachine = {}
-- ##################################################################
-- # Class: State
-- # Each instance of State can have enter, exit, update and draw
-- # methods. These should be implemented by the caller.
-- ##################################################################
local State = {}
function State:enter()
end
function State:exit()
end
function State:update()
end
function State:draw()
end
function State:new(enter, exit, update, draw)
local o = {enter = enter, exit = exit, update = update, draw = draw}
setmetatable(o, self)
self.__index = self
return o
end
function stateMachine.newState(enter, exit, update, draw)
return State:new(enter, exit, update, draw)
end
-- ##################################################################
-- # Class: StateMachine
-- # A basic state machine that can switch between State objects
-- # The current state's exit() method and the new states enter()
-- # method are called on switching.
-- ##################################################################
local StateMachine = {}
function StateMachine:setState(state)
if self.state then
self.state:exit()
end
self.state = state
self.state:enter()
end
function StateMachine:new()
local o = {}
setmetatable(o, self)
self.__index = self
return o
end
function stateMachine.newStateMachine()
return StateMachine:new()
end
return stateMachine