-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamestates.lua
104 lines (91 loc) · 3 KB
/
gamestates.lua
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
--
--> This is a manager for game states. Really useful
--
local gamestates = {};
gamestates.__gamestates_path = "gamestates/"
gamestates._toload = {
playground = "playground.lua",
menu = "menu.lua",
}
gamestates.gamestates = {}
gamestates.current_game_state = nil;
function gamestates:initialize(currgamestate)
print("Gamestates manager :")
for k,v in pairs(self._toload) do
self.gamestates[k] = love.filesystem.load(self.__gamestates_path..v)();
print("\t loaded the "..k)
end
if not currgamestate then
if self.gamestates[0] then
self.current_game_state = 0;
elseif self.gamestates["start"] then
self.current_game_state = "start"
else
for k,v in pairs(self.gamestates) do
self.current_game_state = k;
break;
end
end
else
if self.gamestates[currgamestate] then
self.current_game_state = currgamestate;
else
error("Gamestate "..currgamestate.." does not exist or is not loaded.", 2)
end
end
end
function gamestates:set_game_state(game_state_name, ...)
if self.gamestates[game_state_name] then
if self.gamestates[self.current_game_state].exit then
self.gamestates[self.current_game_state]:exit()
end
self.current_game_state = game_state_name;
if self.gamestates[self.current_game_state].switched_to then
self.gamestates[self.current_game_state]:switched_to(...)
end
else
error("Gamestate "..currgamestate.." does not exist or is not loaded.", 2)
end
end
function gamestates:draw() -- All game states need a draw and an update function. I will not use if statements to tire this and make this run slower
self.gamestates[self.current_game_state]:draw()
end
function gamestates:update(dt)
self.gamestates[self.current_game_state]:update(dt)
end
function gamestates:keypressed(const, scancode, isrepeat)
if self.gamestates[self.current_game_state].keypressed then
self.gamestates[self.current_game_state]:keypressed(const, scancode, isrepeat)
end
end
function gamestates:keyreleased(const, scancode, isrepeat)
if self.gamestates[self.current_game_state].keyreleased then
self.gamestates[self.current_game_state]:keyreleased(const, scancode, isrepeat)
end
end
function gamestates:mousepressed(x, y, button, istouch)
if self.gamestates[self.current_game_state].mousepressed then
self.gamestates[self.current_game_state]:mousepressed(x, y, button, istouch)
end
end
function gamestates:mousereleased(x, y, button, istouch)
if self.gamestates[self.current_game_state].mousereleased then
self.gamestates[self.current_game_state]:mousereleased(x, y, button, istouch)
end
end
function gamestates:textinput( text )
if self.gamestates[self.current_game_state].textinput then
self.gamestates[self.current_game_state]:textinput( text )
end
end
function gamestates:filedropped(file)
if self.gamestates[self.current_game_state].filedropped then
self.gamestates[self.current_game_state]:filedropped( file )
end
end
function gamestates:focus(f)
if self.gamestates[self.current_game_state].focus then
self.gamestates[self.current_game_state]:focus( f )
end
end
return gamestates;