-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
294 lines (241 loc) · 9.35 KB
/
main.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
local ScreenManager = require('lib.screenmanager.ScreenManager')
local Log = require( 'src.util.Log' )
local DebugGrid = require( 'src.ui.overlays.DebugGrid' )
local Letterbox = require( 'src.ui.overlays.Letterbox' )
local DataHandler = require( 'src.DataHandler' )
-- ------------------------------------------------
-- Local Variables
-- ------------------------------------------------
local debugGrid
local letterbox
local mousepressed = false
local mousedragged = false
-- ------------------------------------------------
-- Constants
-- ------------------------------------------------
local DEBUG_OUTPUT_FLAG = '-d'
local DEBUG_GRID_FLAG = '-g'
local DEBUG_FULLSCREEN_FLAG = '-f'
local DEBUG_WINDOWED_FLAG = '-w'
local SCREENS = {
bootloading = require( 'src.ui.screens.BootLoadingScreen' ),
mainmenu = require( 'src.ui.screens.MainMenu' ),
ingamemenu = require( 'src.ui.screens.IngameCombatMenu' ),
options = require( 'src.ui.screens.OptionsScreen' ),
changelog = require( 'src.ui.screens.ChangelogScreen' ),
combat = require( 'src.ui.screens.CombatScreen' ),
inventory = require( 'src.ui.screens.InventoryScreen' ),
help = require( 'src.ui.screens.HelpScreen' ),
gamescreen = require( 'src.ui.screens.GameScreen' ),
gameover = require( 'src.ui.screens.GameOverScreen' ),
loadgame = require( 'src.ui.screens.SavegameScreen' ),
confirm = require( 'src.ui.screens.ConfirmationModal' ),
information = require( 'src.ui.screens.InformationModal' ),
inputdialog = require( 'src.ui.screens.InputDialog' ),
maptest = require( 'src.ui.screens.MapTest' ),
mapeditor = require( 'src.map.editor.MapEditor' ),
mapeditormenu = require( 'src.map.editor.MapEditorMenu' ),
prefabeditor = require( 'src.map.editor.PrefabEditor' ),
prefabeditormenu = require( 'src.map.editor.PrefabEditorMenu' ),
editorloading = require( 'src.map.editor.EditorLoadingScreen' ),
keybindingeditor = require( 'src.ui.screens.KeybindingScreen' ),
keybindingmodal = require( 'src.ui.screens.KeybindingModal' ),
playerInfo = require( 'src.ui.screens.PlayerInfo' ),
base = require( 'src.base.BaseScreen' ),
basemenu = require( 'src.base.BaseScreenMenu' ),
recruitment = require( 'src.base.RecruitmentScreen' ),
shop = require( 'src.base.ShopScreen' ),
}
-- ------------------------------------------------
-- Local Functions
-- ------------------------------------------------
---
-- Iterates over any provided command line arguments and activates the proper
-- mechanics.
--
local function handleCommandLineArguments( args )
for _, arg in pairs( args ) do
if arg == DEBUG_OUTPUT_FLAG then
Log.setDebugActive( true )
elseif arg == DEBUG_GRID_FLAG then
debugGrid = true
elseif arg == DEBUG_FULLSCREEN_FLAG then
love.window.setFullscreen( true )
elseif arg == DEBUG_WINDOWED_FLAG then
love.window.setFullscreen( false )
end
end
end
---
-- Prints some information about the game and the player's system.
--
local function printGameInfo()
local info = {}
info[#info + 1] = "==================="
info[#info + 1] = string.format( "Title: '%s'", getTitle() )
info[#info + 1] = string.format( "Version: %s", getVersion() )
info[#info + 1] = string.format( "LOVE Version: %d.%d.%d (%s)", love.getVersion() )
info[#info + 1] = string.format( "OS: %s", love.system.getOS() )
info[#info + 1] = string.format( "Resolution: %dx%d\n", love.graphics.getDimensions() )
info[#info + 1] = "---- RENDERER ---- "
local name, version, vendor, device = love.graphics.getRendererInfo()
info[#info + 1] = string.format( "Name: %s \n Version: %s \n Vendor: %s \n Device: %s\n", name, version, vendor, device )
info[#info + 1] = "-------------------"
info[#info + 1] = os.date( "%d.%m.%Y %H:%M:%S", os.time() )
info[#info + 1] = "===================\n"
for _, line in ipairs( info ) do
Log.print( line )
end
end
-- ------------------------------------------------
-- Callbacks
-- ------------------------------------------------
function love.load( args )
Log.init()
handleCommandLineArguments( args )
printGameInfo()
ScreenManager.init( SCREENS, 'bootloading' )
letterbox = Letterbox()
-- Create the actual debug grid, if the debug grid flag was set via command
-- line arguments.
if debugGrid then
debugGrid = DebugGrid()
end
end
function love.draw()
ScreenManager.draw()
if debugGrid then
debugGrid:draw()
end
letterbox.draw()
end
function love.update(dt)
ScreenManager.update(dt)
end
function love.quit(q)
Log.info( 'Shutting down...', 'Main' )
ScreenManager.quit(q)
DataHandler.removeTemporaryFiles()
Log.info( 'Thank you for playing "On The Roadside"!', 'Main' )
end
function love.keypressed( key, scancode, isrepeat )
ScreenManager.keypressed( key, scancode, isrepeat )
end
function love.keyreleased( key, scancode )
ScreenManager.keyreleased( key, scancode )
end
function love.resize( w, h )
ScreenManager.resize( w, h )
end
function love.textinput( text )
ScreenManager.textinput( text )
end
function love.mousepressed( mx, my, button, isTouch, presses )
mousepressed = true
ScreenManager.mousepressed( mx, my, button, isTouch, presses )
end
function love.mousereleased( mx, my, button, isTouch, presses )
mousepressed = false
if mousedragged then
mousedragged = false
ScreenManager.mousedragstopped()
return
end
ScreenManager.mousereleased( mx, my, button, isTouch, presses )
end
function love.mousefocus( f )
ScreenManager.mousefocus( f )
end
function love.mousemoved( x, y, dx, dy, isTouch )
if mousepressed then
mousedragged = true
mousepressed = false
ScreenManager.mousedragstarted()
end
ScreenManager.mousemoved( x, y, dx, dy, isTouch )
end
function love.wheelmoved( dx, dy )
ScreenManager.wheelmoved( dx, dy )
end
function love.errhand( msg )
msg = tostring( msg )
Log.error(( debug.traceback( tostring( msg ), 3 ):gsub( "\n[^\n]+$", "" )))
if not love.window or not love.graphics or not love.event then
return
end
if not love.graphics.isCreated() or not love.window.isOpen() then
local success, status = pcall(love.window.setMode, 800, 600)
if not success or not status then
return
end
end
-- Reset state.
if love.mouse then
love.mouse.setVisible(true)
love.mouse.setGrabbed(false)
love.mouse.setRelativeMode(false)
end
if love.joystick then
-- Stop all joystick vibrations.
for _,v in ipairs(love.joystick.getJoysticks()) do
v:setVibration()
end
end
if love.audio then love.audio.stop() end
love.graphics.reset()
love.graphics.setNewFont(math.floor(love.window.toPixels(14)))
love.graphics.setBackgroundColor( 0, 0, 0, 0 )
love.graphics.setColor( 255, 255, 255, 255 )
local trace = debug.traceback()
love.graphics.clear(love.graphics.getBackgroundColor())
love.graphics.origin()
local err = {}
table.insert(err, "\n\nError\n")
table.insert(err, msg.."\n\n")
for l in string.gmatch(trace, "(.-)\n") do
if not string.match(l, "boot.lua") then
l = string.gsub(l, "stack traceback:", "Traceback\n")
table.insert(err, l)
end
end
table.insert(err, '\n\nYou can find the error in the latest.log file in your save directory.' )
table.insert(err, 'Press <return> to open the directoy. Press <escape> to close the game.' )
local p = table.concat(err, "\n")
p = string.gsub(p, "\t", "")
p = string.gsub(p, "%[string \"(.-)\"%]", "%1")
-- Save a copy of the log to the crash dump folder.
Log.saveCrashDump()
local function draw()
local pos = love.window.toPixels(70)
love.graphics.clear(love.graphics.getBackgroundColor())
love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos)
love.graphics.present()
end
while true do
love.event.pump()
for e, a, _, _ in love.event.poll() do
if e == "quit" then
return
elseif e == "keypressed" then
if a == "return" then
love.system.openURL( 'file://' .. love.filesystem.getSaveDirectory() )
return
elseif a == "escape" then
return
end
elseif e == "touchpressed" then
local name = love.window.getTitle()
if #name == 0 or name == "Untitled" then name = "Game" end
local buttons = {"OK", "Cancel"}
local pressed = love.window.showMessageBox("Quit "..name.."?", "", buttons)
if pressed == 1 then
return
end
end
end
draw()
if love.timer then
love.timer.sleep(0.1)
end
end
end