-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameStateManager.lua
More file actions
179 lines (155 loc) · 5.17 KB
/
Copy pathGameStateManager.lua
File metadata and controls
179 lines (155 loc) · 5.17 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
local GameStateManager = {
currentState = nil,
previousState = nil
}
local clickSound = love.audio.newSource("assets/pause.wav", "static")
clickSound:setVolume(.5)
local function assertState(state)
assert(state == nil or type(state) == "table", "State must be a table or nil")
end
local function assertFunction(state, funcName)
if state and state[funcName] then
assert(type(state[funcName]) == "function", funcName .. " must be a function")
end
end
function GameStateManager:getPreviousState()
return self.previousState
end
function GameStateManager:getState()
return self.currentState
end
function GameStateManager:setState(newState)
assertState(newState)
if self.currentState == newState then return end
self.previousState = self.currentState
self.currentState = newState
love.audio.stop(clickSound)
love.audio.play(clickSound)
if self.currentState then
assertFunction(self.currentState, "enter")
if self.currentState.enter then
self.currentState:enter()
end
end
end
function GameStateManager:reloadState()
if self.currentState then
assertFunction(self.currentState, "enter")
if self.currentState.enter then
self.currentState:enter()
end
end
end
function GameStateManager:revertState()
if self.previousState then
self:setState(self.previousState)
end
end
function GameStateManager:mousemoved(x, y, ...)
assert(type(x) == "number", "x must be a number")
assert(type(y) == "number", "y must be a number")
if self.currentState then
assertFunction(self.currentState, "mousemoved")
if self.currentState.mousemoved then
self.currentState:mousemoved(x, y, ...)
end
end
end
function GameStateManager:wheelmoved(x, y)
assert(type(x) == "number", "x must be a number")
assert(type(y) == "number", "y must be a number")
if self.currentState then
assertFunction(self.currentState, "wheelmoved")
if self.currentState.wheelmoved then
self.currentState:wheelmoved(x, y)
end
end
end
function GameStateManager:mousepressed(x, y, button)
assert(type(x) == "number", "x must be a number")
assert(type(y) == "number", "y must be a number")
assert(type(button) == "number", "button must be a number")
if self.currentState then
assertFunction(self.currentState, "mousepressed")
if self.currentState.mousepressed then
self.currentState:mousepressed(x, y, button)
end
end
end
function GameStateManager:mousereleased(x, y, button)
assert(type(x) == "number", "x must be a number")
assert(type(y) == "number", "y must be a number")
assert(type(button) == "number", "button must be a number")
if self.currentState then
assertFunction(self.currentState, "mousereleased")
if self.currentState.mousereleased then
self.currentState:mousereleased(x, y, button)
end
end
end
function GameStateManager:keypressed(key, scancode, isrepeat)
assert(type(key) == "string", "key must be a string")
assert(type(scancode) == "string", "scancode must be a string")
assert(type(isrepeat) == "boolean", "isrepeat must be a boolean")
if self.currentState then
assertFunction(self.currentState, "keypressed")
if self.currentState.keypressed then
self.currentState:keypressed(key, scancode, isrepeat)
end
end
end
function GameStateManager:keyreleased(key, scancode)
assert(type(key) == "string", "key must be a string")
assert(type(scancode) == "string", "scancode must be a string")
if self.currentState then
assertFunction(self.currentState, "keyreleased")
if self.currentState.keyreleased then
self.currentState:keyreleased(key, scancode)
end
end
end
function GameStateManager:textinput(text)
assert(type(text) == "string", "text must be a string")
if self.currentState then
assertFunction(self.currentState, "textinput")
if self.currentState.textinput then
self.currentState:textinput(text)
end
end
end
function GameStateManager:update(dt)
assert(type(dt) == "number", "dt must be a number")
if self.currentState then
assertFunction(self.currentState, "update")
if self.currentState.update then
self.currentState:update(dt)
end
end
end
function GameStateManager:quit()
if self.currentState then
assertFunction(self.currentState, "quit")
if self.currentState.quit then
self.currentState:quit()
end
end
end
function GameStateManager:draw()
if self.currentState then
assertFunction(self.currentState, "draw")
if self.currentState.draw then
self.currentState:draw()
end
end
end
function GameStateManager:resize(w, h)
assert(type(w) == "number", "w must be a number")
assert(type(h) == "number", "h must be a number")
if self.currentState then
assertFunction(self.currentState, "resize")
if self.currentState.resize then
self.currentState:resize(w, h)
end
end
end
return GameStateManager