-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenManager.lua
129 lines (118 loc) · 3.43 KB
/
ScreenManager.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
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
--[[
CLASS SCREEN MANAGER
Welcome to the main gamestate logic! Be sure that in main.lua you are ALWAYS calling the below methods, as was intended.
How do you call them?
Simple! All you have to do is do "require(<directory to this module>)" in main.lua, and call each of these methods according to their love class equivalent.
For details check out main.lua in this repo: https://github.com/thenerdie/Yonder
]]--
local verbose = true
local very_verbose = false
local function puts(str, reqVeryVerbosePerms)
if reqVeryVerbosePerms then
if verbose and very_verbose then
print(str)
end
else
if verbose then
print(str)
end
end
end
local gameStates = { -- this is where you set the directories of your screens
["landing"] = require("Screens/LandingScreen");
["songs"] = require("Screens/SongSelect")
}
local currState = nil
local self = {}
-----------------------
function self:SwitchStates(newState, supressLoad)
if not gameStates[newState] then
currState = nil
error("Error SM106: newState not found in gameStates")
else
currState = newState
if not supressLoad then
puts("Loaded screen \"" .. newState .. "\"; :Load() suppressed = " .. tostring(not not supressLoad))
self:Load()
end
end
end
--// Common LOVE2D functions, wrapped in ScreenManager
function self:Draw(dt) -- In your screen, if you want to switch states from that screen, you need to return a string in either the :Draw() or the :Update() methods indentifying what screen you want to switch to.
if currState ~= nil then
if gameStates[currState].Draw then
gameStates[currState]:Draw(dt)
end
end
end
function self:Update(dt)
if currState ~= nil then
if gameStates[currState].Update then
gameStates[currState]:Update(dt)
puts(":Update called; DT = " .. dt .. "; FPS = " .. 1/dt, true)
else
puts(":Update called but unimplemented; DT = " .. dt .. "; FPS = " .. 1/dt, true)
end
end
end
function self:Quit()
if currState ~= nil then
if gameStates[currState].Quit then
gameStates[currState]:Quit()
puts("Exiting game...")
end
end
end
function self:MousePressed(x, y, button)
if currState ~= nil then
if gameStates[currState].MousePressed then
gameStates[currState]:MousePressed(x, y, button)
puts("Mouse pressed; x = " .. x .. "; y = " .. y)
end
end
end
function self:MouseReleased(x, y, button)
if currState ~= nil then
if gameStates[currState].MouseReleased then
gameStates[currState]:MouseReleased(x, y, button)
puts("Mouse released; x = " .. x .. "; y = " .. y)
end
end
end
function self:KeyPressed(key)
if currState ~= nil then
if gameStates[currState].KeyPressed then
gameStates[currState]:KeyPressed(key)
puts("Key pressed; key = " .. key)
end
end
end
function self:KeyReleased(key)
if currState ~= nil then
if gameStates[currState].KeyReleased then
gameStates[currState]:KeyReleased(key)
puts("Key released; key = " .. key)
end
end
end
function self:Load()
if currState ~= nil then
if gameStates[currState].Load then
gameStates[currState]:Load(self)
puts("Loaded screen \"" .. currState .. "\" successfully!")
else
puts(":Load() unimplemented in Screen")
end
end
end
function self:MouseMoved(x, y, dx, dy, istouch)
if currState ~= nil then
if gameStates[currState].MouseMoved then
gameStates[currState]:MouseMoved(x, y, dx, dy, istouch)
puts("Mouse moved; x = " .. x .. "; y = " .. y, true)
end
end
end
puts("ScreenManager class initialized successfully!")
-----------------------
return self