forked from SimonLarsen/mrrescue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
299 lines (254 loc) · 6.74 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
295
296
297
298
299
require("config")
require("resources")
require("util")
require("map")
require("player")
require("human")
require("enemy")
require("boss")
require("magmahulk")
require("gasleak")
require("gasghost")
require("charcoal")
require("coalball")
require("door")
require("item")
require("fire")
require("particles")
-- gamestates
require("splash")
require("mainmenu")
require("ingame")
require("ingame_menu")
require("options")
require("keyboard")
require("joystick")
require("levelselection")
require("summary")
require("highscore_entry")
require("highscore_list")
require("howto")
require("history")
-- 3rd party libraries
require("AnAL")
require("slam")
require("TSerial")
WIDTH = 256
HEIGHT = 200
MAPW = 41*16
MAPH = 16*16
show_debug = false
local MAX_FRAMETIME = 1/20
local MIN_FRAMETIME = 1/60
local AXIS_COOLDOWN = 0.2
local xacc = 0
local yacc = 0
local xacccool = 0
local yacccool = 0
STATE_SPLASH, STATE_INGAME, STATE_MAINMENU, STATE_LEVELSELECTION, STATE_OPTIONS, STATE_KEYBOARD, STATE_JOYSTICK,
STATE_HOWTO, STATE_HIGHSCORE_LIST, STATE_HIGHSCORE_ENTRY, STATE_INGAME_MENU, STATE_HISTORY, STATE_SUMMARY = 0,1,2,3,4,5,6,7,8,9,10,11,12
gamestates = {[0]=splash, [1]=ingame, [2]=mainmenu, [3]=levelselection, [4]=options, [5]=keyboard,
[6]=joystick, [7]=howto, [8]=highscore_list, [9]=highscore_entry, [10]=ingame_menu, [11]=history, [12]=summary}
function love.load()
loadConfig()
loadHighscores()
loadStats()
love.graphics.setBackgroundColor(0,0,0)
love.graphics.setDefaultImageFilter("nearest","nearest")
loadResources()
setMode()
splash.enter()
end
function love.update(dt)
if xacccool > 0 then
xacccool = xacccool - dt
end
if yacccool > 0 then
yacccool = yacccool - dt
end
gamestates[state].update(dt)
end
function love.draw()
-- Draw border and enable scissoring for fullscreen
lg.push()
setZoom()
gamestates[state].draw()
lg.pop()
lg.setScissor()
if state == STATE_INGAME and map.type == MT_NORMAL then
updateLightmap()
end
end
function setZoom()
if config.fullscreen == 1 then
local sw = love.graphics.getWidth()/WIDTH/config.scale
local sh = love.graphics.getHeight()/HEIGHT/config.scale
lg.scale(sw,sh)
elseif config.fullscreen == 2 then
local sw = love.graphics.getWidth()/WIDTH/config.scale
local sh = love.graphics.getHeight()/HEIGHT/config.scale
local tx = (love.graphics.getWidth() - WIDTH*config.scale*sh)/2
lg.translate(tx, 0)
lg.scale(sh, sh)
lg.setScissor(tx, 0, WIDTH*config.scale*sh, love.graphics.getHeight())
elseif config.fullscreen == 3 then
lg.translate(fs_translatex,fs_translatey)
lg.setScissor(fs_translatex, fs_translatey, WIDTH*config.scale, HEIGHT*config.scale)
end
end
function love.keypressed(k, uni)
gamestates[state].keypressed(k, uni)
end
function love.joystickpressed(joy, k)
if joy == config.joystick then
if gamestates[state].joystickpressed then
gamestates[state].joystickpressed(joy, k)
else
for a, key in pairs(config.joykeys) do
if k == key then
gamestates[state].action(a)
end
end
end
end
end
--- Updates keystates of ingame keys.
-- Should only be called when ingame, as it
-- makes call to Player
function updateKeys()
-- Check keyboard keys
for action, key in pairs(config.keys) do
if love.keyboard.isDown(key) then
keystate[action] = true
else
keystate[action] = false
end
end
-- Check joystick axes
local axis1, axis2 = love.joystick.getAxes(config.joystick)
if axis1 and axis2 then
if axis1 < -0.5 then
keystate.left = true
elseif axis1 > 0.5 then
keystate.right = true
end
if axis2 < -0.5 then
keystate.up = true
elseif axis2 > 0.5 then
keystate.down = true
end
-- Check sudden movements in axes
-- (for ladders and menus)
xacc = xacc*0.50 + axis1*0.50
yacc = yacc*0.50 + axis2*0.50
if math.abs(axis1) < 0.1 then
xacccool = 0
end
if math.abs(axis2) < 0.1 then
yacccool = 0
end
if xacccool <= 0 then
if axis1 < -0.90 then
gamestates[state].action("left")
xacccool = AXIS_COOLDOWN
elseif axis1 > 0.90 then
gamestates[state].action("right")
xacccool = AXIS_COOLDOWN
end
end
if yacccool <= 0 then
if axis2 < -0.90 then
gamestates[state].action("up")
yacccool = AXIS_COOLDOWN
elseif axis2 > 0.90 then
gamestates[state].action("down")
yacccool = AXIS_COOLDOWN
end
end
end
-- Check joystick keys
for action, key in pairs(config.joykeys) do
if love.joystick.isDown(config.joystick, key) then
keystate[action] = true
end
end
end
function love.run()
math.randomseed(os.time())
math.random() math.random()
if love.load then love.load(arg) end
local dt = 0
local acc = 0
-- Main loop time.
while true do
local frame_start = love.timer.getMicroTime()
-- Process events.
if love.event then
love.event.pump()
for e,a,b,c,d in love.event.poll() do
if e == "quit" then
if not love.quit or not love.quit() then
if love.audio then
love.audio.stop()
end
return
end
end
love.handlers[e](a,b,c,d)
end
end
love.timer.step()
dt = love.timer.getDelta()
dt = math.min(dt, MAX_FRAMETIME)
acc = acc + dt
while acc >= MIN_FRAMETIME do
love.update(MIN_FRAMETIME)
acc = acc - MIN_FRAMETIME
end
-- Update screen
love.graphics.clear()
love.draw()
love.graphics.present()
love.timer.sleep(0.001)
end
end
function love.quit()
saveConfig()
saveHighscores()
saveStats()
end
function love.releaseerrhand(msg)
print("An error has occured, the game has been stopped.")
if not love.graphics or not love.event or not love.graphics.isCreated() then
return
end
love.graphics.setCanvas()
love.graphics.setPixelEffect()
-- Load.
if love.audio then love.audio.stop() end
love.graphics.reset()
love.graphics.setBackgroundColor(89, 157, 220)
local font = love.graphics.newFont(14)
love.graphics.setFont(font)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.clear()
local err = {}
p = string.format("An error has occured that caused %s to stop.\nYou can notify %s about this%s.\n\nError: %s", love._release.title or "this game", love._release.author or "the author", love._release.url and " at " .. love._release.url or "", msg)
local function draw()
love.graphics.clear()
love.graphics.printf(p, 70, 70, love.graphics.getWidth() - 70)
love.graphics.present()
end
draw()
local e, a, b, c
while true do
e, a, b, c = love.event.wait()
if e == "quit" then
return
end
if e == "keypressed" and a == "escape" then
return
end
draw()
end
end