-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
277 lines (228 loc) · 7.54 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
love.errorhandler = require("utils.errorhandler")
local argparse = require("lib.argparse")
local ScreenManager = require("ui.ScreenManager")
local joystickManager = require("utils.joystickManager")
local console = require("utils.console")
local mouseUtils = require("utils.mouse")
local musicManager = require("utils.musicManager")
local scheduler = require("utils.scheduler")
local settings = require("utils.settings")
local languageUtils = require("utils.language")
local Steam = require("luasteam")
local storage = require("utils.storage")
local achievements = require("utils.achievements")
local isInitialized = false
local debugUpdateDelay = 0
local steamUpdateInterval = 0.1
local steamLastUpdatedAt = love.timer.getTime()
local screenManager
---------------------
-- Local functions --
---------------------
local function completeInitialization()
if isInitialized then
return
end
local steamUserId = "local"
-- Initialize Steam
local success = pcall(function ()
if GameEnv.disableSteam then
error("Steam disabled")
end
local steamInitStartAt = love.timer.getTime()
if Steam.init() then
steamUserId = tostring(Steam.user.getSteamID())
elseif not GameEnv.disableSteam then
error("Steam must be running")
end
print("Steam initalization completed in", math.floor((love.timer.getTime() - steamInitStartAt) * 1000) / 1000, "s")
end)
if not success then
if GameEnv.disableSteam then
print("Failed to connect to steam")
else
love.window.showMessageBox("Steam initialization error", "Failed to connect to Steam. Make sure that Steam client is running.", "error")
-- love.event.quit()
end
end
GameEnv.isSteamInitialized = success
-- Load game saves
love.filesystem.createDirectory(steamUserId)
storage.load(steamUserId.."/user_progress.bin")
-- Init achievements
if success then
Steam.userStats.requestCurrentStats()
achievements.init()
end
screenManager:emit("handleInitializationFinish")
isInitialized = true
end
----------------------
-- Love2d callbacks --
----------------------
function love.load(arg)
console.overridePrint()
print("Game started")
-- Parse command-line arguments
local parser = argparse()
if GameEnv.enableCommandLineArgs then
parser:option("--level", "Force load game level")
parser:option("--screen", "Force display screen")
parser:flag("--maximize", "Force maximize window")
parser:flag("--nosplash", "Skip splash screen")
end
local args = parser:parse(arg)
-- Lua initialization
math.randomseed(os.time())
-- Love2D initalization
love.window.setIcon(love.image.newImageData("assets/window_icon.png"))
settings.load()
if not settings.get("language") then
settings.set("language", languageUtils.getSystemLanguage())
end
-- Initalize UI and show first screen
screenManager = ScreenManager:new()
if args.level then
completeInitialization()
print("Force load level", args.level)
screenManager:transition("GameScreen", args.level)
elseif args.screen then
print("Force load screen", args.screen)
completeInitialization()
screenManager:transition(args.screen)
elseif args.nosplash then
completeInitialization()
screenManager:transition("MainMenuScreen")
else
screenManager:transition("SplashScreen")
end
end
function love.quit()
local x, y, display = love.window.getPosition()
settings.set("window_x", math.max(1, x))
settings.set("window_y", math.max(1, y))
settings.set("window_display", display)
Steam.shutdown()
end
function love.update(deltaTime)
if not isInitialized and screenManager.fadeProgress < 0.01 then
completeInitialization()
end
-- Update Steam integration
if isInitialized and love.timer.getTime() - steamLastUpdatedAt > steamUpdateInterval then
steamLastUpdatedAt = love.timer.getTime()
Steam.runCallbacks()
achievements.update()
end
-- Don't update game if it's frozen
if deltaTime > 0.5 then
return
end
scheduler.update(deltaTime)
mouseUtils.update()
screenManager:update(deltaTime)
musicManager:update(deltaTime)
if GameEnv.simulateFrameRate then
debugUpdateDelay = debugUpdateDelay + deltaTime
if debugUpdateDelay > 1 / GameEnv.simulateFrameRate then
screenManager:emit("update", debugUpdateDelay)
debugUpdateDelay = 0
end
else
screenManager:emit("update", deltaTime)
end
end
function love.draw()
screenManager:emit("draw")
screenManager:draw()
console.draw()
end
function love.keypressed(key, ...)
screenManager:emit("handleKeyPress", key, ...)
if key == "`" and GameEnv.enableGameConsole then
console.toggle()
elseif key == "f11" then
if settings.get("window_mode") == "windowed" then
settings.set("window_mode", "fullscreen")
else
settings.set("window_mode", "windowed")
end
end
if GameEnv.enableDebugMode then
if key == "r" and love.keyboard.isDown("lctrl") and love.keyboard.isDown("lshift") then
love.event.quit("restart")
elseif key == "k" and love.keyboard.isDown("lctrl") and love.keyboard.isDown("lshift") then
Steam.userStats.resetAllStats(true)
Steam.userStats.storeStats()
print("Achievements reset")
end
end
end
function love.keyreleased(...)
screenManager:emit("handleKeyRelease", ...)
end
function love.mousepressed(...)
screenManager:emit("handleMousePress", ...)
end
function love.focus(isFocused)
screenManager:emit("handleWindowFocus", isFocused)
end
function love.joystickadded(joystick)
joystickManager.add(joystick)
end
function love.joystickremoved(joystick)
joystickManager.remove(joystick)
end
function love.resize(width, height)
if settings.get("window_mode") == "windowed" then
settings.set("window_width", width)
settings.set("window_height", height)
end
screenManager:emit("handleWindowResize", width, height)
end
---------------------
-- Steam callbacks --
---------------------
function Steam.friends.onGameOverlayActivated(data)
screenManager:emit("handleWindowFocus", not data.active)
end
function Steam.userStats.onUserStatsReceived(data)
print("Steam achievements loaded")
end
-----------------------
-- Settings handlers --
-----------------------
settings.addHandler("window_mode", function (value)
if not isInitialized then
return
end
if value == "windowed" then
love.window.updateMode(settings.get("window_width"), settings.get("window_height"), {
fullscreen = false,
})
elseif value == "borderless" then
love.window.updateMode(0, 0, {
fullscreen = true,
fullscreentype = "desktop",
})
else
love.window.updateMode(0, 0, {
fullscreen = true,
fullscreentype = "exclusive",
})
end
screenManager:emit("handleWindowResize", love.graphics.getWidth(), love.graphics.getHeight())
end)
settings.addHandler("vsync", function (value)
if value then
love.window.setVSync(-1)
else
love.window.setVSync(0)
end
end)
settings.addHandler("master_volume", function (value)
love.audio.setVolume(value)
end)
settings.addHandler("language", function (value)
languageUtils.loadLanguage(value)
end)