-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.lua
More file actions
208 lines (164 loc) · 6.58 KB
/
Copy pathstate.lua
File metadata and controls
208 lines (164 loc) · 6.58 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
local args = {...}
local import_prefix = args[1]
if import_prefix then import_prefix = import_prefix:match("^(.-)[^%.]+$") end
if not import_prefix then import_prefix = "" end
local errormodule = require(import_prefix .. "error")
local utilmodule = load_module(import_prefix .. "util", true)
local consolemodule = load_module(import_prefix .. "console", true)
local classmodule = load_module(import_prefix .. "class", true)
local basestatemodule = load_module(import_prefix .. "states.base", true)
local mainmenustatemodule = load_module(import_prefix .. "states.mainMenu", true)
local pregamestatemodule = load_module(import_prefix .. "states.pregame", true)
local gamestatemodule = load_module(import_prefix .. "states.game", true)
local optionsstatemodule = load_module(import_prefix .. "states.options", true)
--[[ StateManager - the state manager class [singleton]
Holds a state stack
main_state - the initial main state
]]
local StateManager = class(function(self, main_state)
self.__states_dict = {}
self:reset(main_state)
end)
function StateManager:mustExit() return self.__exit end
function StateManager:getStatesStack() return self.__states[self.__main_count] end
function StateManager:getMainStatesStack() return self.__main_states end
function StateManager:getStatesCount() return self.__count end
function StateManager:getMainStatesCount() return self.__main_count end
-- getState - returns the current state (as a class), or the main state if there is none registered
function StateManager:getState()
local state = self:getMainStatesStack()[self:getMainStatesCount()]
if self.__states_dict[state] then return self.__states_dict[state]
--else return state end
else return nil end
end
-- pushMainState - store the old main state and reset the stack to put the new main_state on top
function StateManager:pushMainState(main_state)
if main_state == nil then return end
self.__main_states[self.__main_count + 1] = main_state
self.__main_count = self.__main_count + 1
table.insert(self.__states, {self.__main_states[self.__main_count]})
local state = self:getState()
if state then state:onPush()
else console:print("Pushed an unknown state: " .. main_state .. "\n", LogLevel.WARNING, "state.lua/StateManager:pushMainState") end
end
-- popMainState - restore the last stored main state
function StateManager:popMainState()
if self.__main_count == 1 then self.__exit = true
elseif self.__main_count == 0 then return nil end
while self:getStatesStack()[2] do self:popState() end
local main_state = self.__main_states[self.__main_count]
local state = self:getState()
if state then state:onPop()
else console:print("Popped an unknown state: " .. main_state .. "\n", LogLevel.WARNING, "state.lua/StateManager:pushMainState") end
self.__main_states[self.__main_count] = nil
self.__main_count = self.__main_count - 1
table.remove(self.__states)
if self.__main_count > 0 then
-- Also reset substates count
self.__count = #self.__states[#self.__states]
else
self.__count = 0
end
local topState = self:getState()
if topState then topState:onPoppedUpper(main_state, state) end
return main_state
end
function StateManager:pushState(state)
if state == nil then return end
self:getStatesStack()[self.__count + 1] = state
self.__count = self.__count + 1
end
function StateManager:popState()
if self.__count <= 1 then return nil end
local state = self:getStatesStack()[self.__count]
self:getStatesStack()[self.__count] = nil
self.__count = self.__count - 1
return state
end
-- registerState - register a state as stateName
function StateManager:registerState(state, stateName)
if state and self.__states_dict[stateName] and self.__states_dict[stateName].pseudostate then self.__states_dict[stateName] = nil end
if state and self.__states_dict[stateName] then console:print("Trying to re-register a state: " .. stateName .. "\n", LogLevel.WARNING_DEV, "state.lua/StateManager:registerState")
elseif not state and not self.__states_dict[stateName] then console:print("Trying to remove inexistent state: " .. stateName .. "\n", LogLevel.WARNING_DEV, "state.lua/StateManager:registerState")
elseif not state.isinstance or not state:isinstance(BaseState) then console:print("Trying to register a non-state object as state " .. stateName .. "\n", LogLevel.WARNING_DEV, "state.lua/StateManager:registerState")
else
self.__states_dict[stateName] = state
end
end
--[[ registerPseudostate
Register a pseudostate as name.
Pseudostates are states that cannot be executed (they are not real states).
]]
function StateManager:registerPseudostate(name)
local pseudostate = class(function(self) end, BaseState)
pseudostate.pseudostate = true
pseudostate:__implementAbstract("runIteration", abst_method("pseudostate " .. name .. " cannot be run"))
self:registerState(pseudostate(), name)
end
--[[ runIteration
Run a game/menu/... iteration.
]]
function StateManager:runIteration()
local state = self:getState()
if not state then return false end
if state.pseudostate then return false end
return state:runIteration()
end
--[[ runLoop
Run the iterations loop
]]
function StateManager:runLoop()
local catch = function(e)
self.__exit = true
self:crash("An iteration of the game loop crashed (" .. tostring(e) .. ")")
return true
end
while not self:mustExit() do
if not ({
try(differ(self.runIteration, self)):catch(any_error, catch)("state.lua/StateManager:runLoop")
})[1][2] then
self:popMainState()
end
end
end
--[[ reset
Reset the state manager to an empty and valid state
]]
function StateManager:reset(main_state)
self.__exit = false
if main_state then
self.__main_states, self.__main_count = {main_state}, 1
self.__states, self.__count = {{main_state}}, 1
local state = self:getState()
if state then state:onPush() end
else
self.__main_states, self.__main_count = {}, 0
self.__states, self.__count = {}, 0
end
-- Also gc just to be safe.
collectgarbage()
end
function StateManager:crash(msg)
while self.__main_states[1] do
self:popMainState()
end
error(msg or "Crash requested")
end
function StateManager:fastcrash()
self.__states_dict = {}
self.__main_states = {}
self.__main_count = 0
self.__states = {}
self.__count = 0
self.__exit = true
error("Fast crash requested")
return false
end
-- stateManager - the state manager singleton
stateManager = StateManager("mm")
stateManager:registerState(mainMenuState, "mm")
stateManager:registerState(optionsState, "options")
stateManager:registerState(preGameState, "gameWrapper")
stateManager:registerState(gameState, "game")
-- Register the in-game pseudostate
stateManager:registerPseudostate("ig")