-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
239 lines (190 loc) · 5.39 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
-- main.lua: Main script, take care of scenes and initialisations.
game = {
-- The current level, if a level has been opened.
level = 1,
-- How many levels have been unlocked, loaded from savegame.
levelsUnlocked = 1,
-- The current scene that should be shown.
state = "mainmenu",
-- The current overlay that should be shown, if applicable.
overlay = false,
-- The amount of balls left. Stored globally to be accessible from level success and such.
ballsLeft = 0
}
-- Base internal resolution ("canvas" resolution)
base_resolution = {
x = 1280,
y = 720
}
-- Resolution that the window is resized to (scaled up from base_resolution)
resolution = {
x = base_resolution.x,
y = base_resolution.y
}
-- Canvas offset, if aspect ratio is different
offset = {
x = 0,
y = 0
}
scenes = {}
overlays = {}
oldmousedown = false
-- Keeps track of held down keys to prevent repeating actions.
sparsifier = {}
-- Dummy translation function
-- (Future proofing for when a translation system is implemented)
function S(text, ...)
return string.format(text, ...)
end
require("fonts")
require("util")
require("gtk")
require("savegame")
require("mainmenu")
require("game")
require("selectlevel")
require("final")
require("success")
require("pause")
dbg = require("dbg")
bf = require("lib.breezefield")
json = require("lib.json")
-- On load callback
function love.load()
-- resizable = true makes Android landscape.
love.window.setMode(resolution.x, resolution.y, { resizable = true })
love.window.setTitle("Box Smasher")
love.graphics.setDefaultFilter('nearest', 'nearest', 4)
-- Hide navigation bar and notification tray
if love.system.getOS() == 'Android' then
love.window.setFullscreen(true)
end
assets = {
back_btn = newImage("back_btn"),
lvlok = newImage("lvlok"),
lock = newImage("lock"),
menu = newImage("menu"),
tutorial = newImage("tutorial")
}
fonts = initFonts()
sounds = {
click = newSound("click"),
pop = newSound("pop"),
success = newSound("success")
}
local totalLevels = 0
while true do
if love.filesystem.getInfo("levels/"..(totalLevels+1)..".lua") then
totalLevels = totalLevels + 1
else
break
end
end
game.totalLevels = totalLevels
print(totalLevels)
savegame.load()
game.levelsUnlocked = savegame.get('levelsUnlocked') or 1
game.seenTutorial = savegame.get('seenTutorial') or false
math.randomseed(os.time())
-- Hardcode initial state init
if scenes[game.state].init ~= nil then
scenes[game.state].init()
end
end
function love.update(dt)
if scenes[game.state].update ~= nil then
scenes[game.state].update(dt)
end
if game.overlay then
if overlays[game.overlay].update ~= nil then
overlays[game.overlay].update()
end
end
oldmousedown = love.mouse.isDown(1)
-- Check for quit keybind (ctrl+q, hardcoded in Android too I think)
if love.keyboard.isDown('lctrl') and love.keyboard.isDown('q') then
love.event.quit()
end
-- Debug options (accessible with F3+<arbitrary>, see dbg.lua)
if love.keyboard.isDown('f3') then
for id, def in pairs(dbg) do
if love.keyboard.isDown(def.keybind) and not sparsifier[def.keybind] then
dbg[id].enabled = not dbg[id].enabled
end
sparsifier[def.keybind] = love.keyboard.isDown(def.keybind)
end
end
end
local trans_alpha = 0
-- On draw callback
function love.draw()
-- Offset canvas using 'offset', scale up canvas
-- using a factor of res / base_res
love.graphics.translate(offset.x, offset.y)
love.graphics.scale(
resolution.x / base_resolution.x,
resolution.y / base_resolution.y)
-- Default font & draw colour
love.graphics.setFont(fonts.sans.medium)
love.graphics.setColor(1,1,1)
-- Call scene's draw function
local bg
if scenes[game.state].draw ~= nil then
if scenes[game.state].background then
bg = scenes[game.state].background
drawBG(love.math.colorFromBytes(bg.r, bg.g, bg.b))
end
scenes[game.state].draw()
end
if game.overlay then
if overlays[game.overlay].draw ~= nil then
overlays[game.overlay].draw()
end
end
if scenes[game.state].background then
drawBGLetterbox(love.math.colorFromBytes(bg.r, bg.g, bg.b))
end
-- Call debug functionalities' draw functions
-- (if they're enabled)
for id, def in pairs(dbg) do
if def.enabled then
love.graphics.setColor(1,1,1)
love.graphics.setFont(fonts.sans.medium)
def.draw()
end
end
if game.trans then
if game.trans_step < 25 then
trans_alpha = trans_alpha + 10
else
trans_alpha = trans_alpha - 10
end
love.graphics.setColor(0,0,0,trans_alpha/255)
love.graphics.push()
love.graphics.origin()
love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.pop()
game.trans_step = game.trans_step + 1
if game.trans_step == 25 then
game.state = game.trans_to
if scenes[game.state].init ~= nil then
scenes[game.state].init()
end
elseif game.trans_step == 50 then
game.trans = false
end
end
end
function love.resize(w, h)
resolution.x = w
resolution.y = h
-- Keep aspect ratio of canvas, don't stretch it if aspect ratio is changed
if resolution.y / resolution.x > (base_resolution.y/base_resolution.x) then
resolution.y = math.ceil(resolution.x * (base_resolution.y/base_resolution.x))
else
resolution.x = math.ceil(resolution.y * (base_resolution.x/base_resolution.y))
end
-- Calculate offset (canvas should be in the middle, fill the edges with void)
offset.x = (w - resolution.x) / 2
offset.y = (h - resolution.y) / 2
end