-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
169 lines (145 loc) · 4.14 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
-- Dependencies
local Assets = require("engine.Assets")
local Audio = require("engine.Audio")
local Config = require("engine.Config")
local Controller = require("engine.Controller")
local Debug = require("engine.Debug")
local FrameRate = require("engine.FrameRate")
local Function = require("engine.Function")
local Log = require("engine.Log")
local Music = require("engine.Music")
local State = require("engine.State")
local Reporter = require("engine.Reporter")
local Transition = require("engine.Transition")
local Video = require("engine.Video")
local Notify = require("game.Notify")
-- Variables
local busy = false
-- Setups screen with 'loading...' title
local function setupLoadingScreen()
local screen = love.graphics.newImage("graphics/loading.png")
love.graphics.draw(screen)
love.graphics.present()
end
-- Hooks some modules
local function installHooks()
State.update = Function.around(State.update, function(update, delta)
delta = 0.5 * delta * Config.baseSpeed
-- We run the update twice with 1/2 delta for more precise simulation
update(delta)
update(delta)
if Config.turboMode then
update(delta)
update(delta)
end
end)
Controller.keyPressed = Function.before(Controller.keyPressed, function(...)
Debug.keyPressed(...)
State.keyPressed(...)
end)
love.busy = function()
busy = true
end
end
-- Loads levels metadata
local function loadMetadata()
-- Must be required after Assets module is loaded
require("game.states.Tower").load()
require("game.ui.LevelsMenu").load()
end
-- Makes screenshot of each level
local function makeScreenshots()
local Room = require("game.Room")
local Tower = require("game.states.Tower")
local screenshotsDir = "screenshots"
love.filesystem.createDirectory(screenshotsDir)
for floor = 1, Tower.getMaxFloor() do
for room = 1, 4 do
local name = string.format("%02d-%02d", floor, room);
Log.info("Making screenshot of level " .. name)
Room.create("levels/" .. name .. ".lua")
Room.skipMessage()
love.graphics.reset()
love.graphics.clear()
Video.beginDrawing()
Room.draw()
Video.endDrawing()
local screenshot = love.graphics.newScreenshot()
screenshot:encode("png", screenshotsDir .. "/" .. name .. ".png");
end
end
end
-- Initializes game
function love.load()
-- Begin initialization
Log.info("Loading...")
setupLoadingScreen()
-- Install hooks
installHooks()
Audio.hook()
Controller.hook()
-- Load modules data
Config.load()
Reporter.init(Config)
Assets.load(Config)
loadMetadata()
-- Initialize rest of the modules
Audio.init(Config)
Controller.init(Config)
Debug.init(Config)
FrameRate.init(Config)
Music.init(Config)
State.init(Config)
Transition.init(Config)
Notify.init(Config)
Video.init(Config)
-- Finish initialization
Log.info("Loading finished")
-- Make screenshots if set
if Config.makeScreenshots then
makeScreenshots()
love.event.quit()
end
-- Play testing level if set
if Config.testingLevel then
State.set("Game", "testing", Config.testingLevel)
end
end
-- Processes change of window visibility
function love.visible(visible)
Log.info("Window visible: %s", visible)
if visible then
Music.resume()
else
Music.pause()
end
end
-- Updates game
function love.update(delta)
if busy or not love.window.isVisible() then
busy = false
return
end
FrameRate.beginCounting()
Music.update(delta)
Notify.update(delta)
State.update(delta)
Transition.update(delta)
Controller.update(delta)
end
-- Draws game
function love.draw()
Video.beginDrawing()
State.draw()
Transition.draw()
Notify.draw()
Debug.draw()
Video.endDrawing()
FrameRate.endCounting()
end
-- Quits game
function love.quit()
Log.info("Quitting...")
State.quit()
Config.save()
end