-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
366 lines (306 loc) · 8.18 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
-- common utilities used throughout the program
require 'wyx.util'
require 'random'
local versionFile = love.filesystem.read('VERSION')
VERSION = string.match(versionFile, '.*VERSION=([%d%.]+)') or "UNKNOWN"
GAMENAME = 'Wyx'
LOAD_DELAY = 0.025
--[[--
DEBUG/PROFILING
--]]--
--debug = nil
debugGameEvents = debug and nil
debugCommandEvents = debug and nil
debugInputEvents = debug and nil
debugTooltips = debug and true
doProfile = false
local doGlobalProfile = doProfile and false
--[[ Profiler Setup ]]--
local globalProfiler
if doGlobalProfile then
require 'lib.profiler'
local _profiler = newProfiler()
globalProfiler = {
start = function() _profiler:start() end,
stop = function()
_profiler:stop()
local filename = 'pepperfish.out'
local outfile = io.open(filename, 'w+')
_profiler:report(outfile)
outfile:close()
print('profile written to '..filename)
end,
}
end
if doProfile then
require 'profiler'
print('analyze profile with '
..'"lua lib/summary.lua lprof_tmp.0.<stuff>.out"')
else
local dummy = function() end
profiler = {
start = dummy,
stop = dummy
}
end
--[[--
GLOBAL CLASSES
--]]--
RunState = require 'lib.hump.gamestate'
cron = require 'lib.cron'
tween = require 'lib.tween'
inspect = require 'lib.inspect'
--[[--
<3 LÖVE <3
--]]--
local function _makeADir(dir)
if not love.filesystem.mkdir(dir) then
local savedir = love.filesystem.getSaveDirectory()
error('Could not create directory: '..savedir..'/'..tostring(dir))
end
end
local function _makeSaveDirectories()
for _,dir in ipairs({
--[[
'font',
'music',
'image',
'sound',
'map',
'entity/item',
'entity/enemy',
'entity/hero',
'skill',
]]--
'save',
'morgue',
}) do _makeADir(dir) end
end
local function _setIcon()
local icon = love.graphics.newImage('icon.png')
love.graphics.setIcon(icon)
end
function love.load()
-- create fake Console to store Console:print and :log lines until actual
-- console is created
Console = {
print = function(self, ...)
self._output = self._output or {}
self._output[#self._output+1] = {print = {...}}
end,
log = function(self, ...)
self._output = self._output or {}
self._output[#self._output+1] = {log = {...}}
end,
}
Console:print(colors.GREEN, '%s v%s', GAMENAME, VERSION)
-- start the profiler
if doGlobalProfile then globalProfiler.start() end
-- set graphics mode
resizeScreen(1024, 768)
-- set the program icon
_setIcon()
-- set window title
love.graphics.setCaption(GAMENAME..' v'..VERSION)
-- set key repeat
love.keyboard.setKeyRepeat(100, 200)
-- save number of music and sound files as global
NUM_MUSIC = 8
NUM_SOUNDS = 11
-- global tile width and height
TILEW, TILEH = 32, 32
-- define game fonts
GameFont = {
bighuge = love.graphics.newImageFont('font/lofi_bighuge.png',
'0123456789!@#$%()-=+,.":;/\\?\' ' ..
'abcdefghijklmnopqrstuvwxyz' ..
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
big = love.graphics.newImageFont('font/lofi_big.png',
'0123456789!@#$%()-=+,.":;/\\?\' ' ..
'abcdefghijklmnopqrstuvwxyz' ..
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
bigsmall = love.graphics.newImageFont('font/lofi_bigsmall.png',
'0123456789!@#$%()-=+,.":;/\\?\' ' ..
'abcdefghijklmnopqrstuvwxyz' ..
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
small = love.graphics.newImageFont('font/lofi_small.png',
'0123456789!@#$%^&*()-=+[]{}:;\'"<>,.?/\\ ' ..
'abcdefghijklmnopqrstuvwxyz' ..
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
verysmall = love.graphics.newImageFont('font/lofi_verysmall.png',
'0123456789!@#$%^&*()-=+[]{}:;\'"<>,.?/\\ ' ..
'abcdefghijklmnopqrstuvwxyz' ..
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
console = love.graphics.newImageFont('font/grafx2.png',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ..
'abcdefghijklmnopqrstuvwxyz' ..
'0123456789`~!@#$%^&*()_+-={}[]\\/|<>,.;:\'" '),
}
-- register all love events with run state
RunState.registerEvents()
-- create global event managers (event "channels")
local EventManager = getClass 'wyx.event.EventManager'
GameEvents = EventManager('GameEvents')
InputEvents = EventManager('InputEvents')
CommandEvents = EventManager('CommandEvents')
if debugGameEvents then GameEvents:debug(debugGameEvents) end
if debugInputEvents then InputEvents:debug(debugInputEvents) end
if debugCommandEvents then CommandEvents:debug(debugCommandEvents) end
-- create global ui system
UISystem = getClass('wyx.system.UISystem')()
-- create global console
Console = getClass('wyx.ui.Console')(Console._output)
-- make sure the save directories are created
_makeSaveDirectories()
-----------------------------------
-- "The real Wyx starts here..." --
-----------------------------------
RunState.switch(State.intro)
end
function love.update(dt)
cron.update(dt)
tween.update(dt)
GameEvents:flush()
InputEvents:flush()
CommandEvents:flush()
love.audio.update()
UISystem:update(dt)
end
local _postdraw = function() UISystem:draw() end
-- table for quick lookup of specific modifiers to generic modifiers
local _modLookup = {
numlock = 'numlock',
capslock = 'capslock',
scrollock = 'scrollock',
rshift = 'shift',
lshift = 'shift',
rctrl = 'ctrl',
lctrl = 'ctrl',
ralt = 'alt',
lalt = 'alt',
rmeta = 'meta',
lmeta = 'meta',
lsuper = 'super',
rsuper = 'super',
mode = 'mode',
compose = 'compose',
}
local function _getModifiers()
local mods = {}
for k,v in pairs(_modLookup) do
if love.keyboard.isDown(k) then
mods[k] = true
mods[v] = true
end
end
return mods
end
local KeyboardEvent = getClass 'wyx.event.KeyboardEvent'
function love.keypressed(key, unicode)
local mods = _getModifiers()
-- shift-F1 for debug mode
if debug and 'f1' == key and mods['shift'] then
debug.debug()
else
InputEvents:notify(KeyboardEvent(key, unicode, mods))
end
end
local MousePressedEvent = getClass 'wyx.event.MousePressedEvent'
local MouseReleasedEvent = getClass 'wyx.event.MouseReleasedEvent'
function love.mousepressed(x, y, button)
local mods = _getModifiers()
InputEvents:notify(MousePressedEvent(x, y, button,
love.mouse.isGrabbed(),
love.mouse.isVisible(),
mods))
end
function love.mousereleased(x, y, button)
local mods = _getModifiers()
InputEvents:notify(MouseReleasedEvent(x, y, button,
love.mouse.isGrabbed(),
love.mouse.isVisible(),
mods))
end
function love.quit()
tween.stopAll()
Console:destroy()
UISystem:destroy()
GameEvents:destroy()
InputEvents:destroy()
CommandEvents:destroy()
if doGlobalProfile then globalProfiler.stop() end
end
local collectgarbage = collectgarbage
local getMicroTime = love.timer.getMicroTime
local sleep = love.timer.sleep
local event = love.event
local poll = love.event.poll
local audio = love.audio
local audiostop = love.audio.stop
local handlers = love.handlers
local clear = love.graphics.clear
local present = love.graphics.present
local function rungb(maxTime)
local start = getMicroTime()
local time = 0
while time < maxTime do
collectgarbage('step', 0)
collectgarbage('stop')
time = getMicroTime() - start
end
end
function love.run()
if love.load then love.load(arg) end
local Hz60 = 1/60
local dt = Hz60/4
local currentTime = getMicroTime()
local accumulator = 0.0
local gbcount = 0
-- disable automatic garbage collector
collectgarbage('stop')
-- Main loop time.
while true do
-- Process events.
if event then
for e,a,b,c in poll() do
if e == "q" then
if not love.quit or not love.quit() then
-- restart garbage collector
collectgarbage('restart')
if audio then
audiostop()
end
return
end
end
handlers[e](a,b,c)
end
end
local newTime = getMicroTime()
local frameTime = newTime - currentTime
frameTime = frameTime > 0.25 and 0.25 or frameTime
currentTime = newTime
accumulator = accumulator + frameTime
if frameTime < Hz60 then
gbcount = gbcount + 1
local idletime = (Hz60 - frameTime) * 0.99
-- every once in a while, collect some garbage instead of sleeping
if gbcount > 10 then
rungb(idletime)
gbcount = 0
else
sleep(idletime*1000)
end
end
while accumulator >= dt do
if love.update then love.update(dt) end
accumulator = accumulator - dt
end
clear()
if love.draw then
love.draw()
_postdraw()
end
present()
end
end