-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
385 lines (322 loc) · 10.3 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
dialog = love.filesystem.load('dialog.lua')()
say = dialog.say
choose = dialog.choose
choosecancelable = dialog.choosecancelable
rooms = love.filesystem.load('rooms.lua')()
function switchbgmusic(music)
if currentbgmusic ~= music then
if currentbgmusic then
currentbgmusic:stop()
end
currentbgmusic = music
currentbgmusic:play()
end
end
function love.load()
kleft = 'left'
kright = 'right'
kup = 'up'
kdown = 'down'
kact = 'space'
scalefactor = 4
realwidth = 640
realheight = 480
virtualwidth = realwidth / scalefactor
virtualheight = realheight / scalefactor
-- give us a retro look when scaling pixel art
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setMode(realwidth, realheight)
canvas = love.graphics.newCanvas(virtualwidth, virtualheight)
scancanvas = love.graphics.newCanvas(virtualwidth, virtualheight)
-- load music and sounds
music = love.audio.newSource('music/title.ogg')
music:setLooping(true)
shipmusic = love.audio.newSource('music/ship.ogg')
shipmusic:setLooping(true)
worldmapmusic = love.audio.newSource('music/city.ogg')
worldmapmusic:setLooping(true)
confirm = love.audio.newSource('sounds/confirm.wav', 'static')
bloop = love.audio.newSource('sounds/bloop.wav', 'static')
anchor = love.audio.newSource('sounds/anchor.wav', 'static')
dial = love.audio.newSource('sounds/dial.wav', 'static')
currentbgmusic = nil
-- debug font
debugfont = love.graphics.newFont(14)
-- load graphics
comm = love.graphics.newImage('images/comm.png')
logo = love.graphics.newImage('images/logo.png')
font = love.graphics.newImageFont('images/smallfont.png',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
.. 'abcdefghijklmnopqrstuvwxyz,.!?@:>\''
.. '|$*')
scanline = love.graphics.newImage('images/scanline.png')
jyesula = love.graphics.newImage('images/jyesula.png')
ship = love.graphics.newImage('images/ship.png')
city = love.graphics.newImage('images/city.png')
backdrop = nil
-- start game in the title mode
switchmode('title')
end
--- Copy (and decompress) an image and return the copy.
-- This function always returns an image with uncompressed image data.
-- @param img the image
-- @return the copy of the image
function copyimg(img)
local width = img:getWidth()
local height = img:getHeight()
local canvas = love.graphics.newCanvas(width, height)
love.graphics.setCanvas(canvas)
love.graphics.push()
love.graphics.origin()
love.graphics.clear(0, 0, 0, 0)
local origr, origg, origb, origa = love.graphics.getColor()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(img, 0, 0)
love.graphics.setColor(origr, origg, origb, origa)
love.graphics.pop()
love.graphics.setCanvas()
local data = canvas:newImageData()
return love.graphics.newImage(data)
end
--- Determine whether a pixel in an image is totally transparent.
-- @param img the image
-- @param mx the x position
-- @param my the y position
-- @return whether the pixel is transparent
function ispicked(img, x, y)
local width = img:getWidth()
local height = img:getHeight()
if x < 0 or x >= width or y < 0 or y >= height then
return false
end
local image = copyimg(img)
local data = img:getData()
local r, g, b, a = data:getPixel(x, y)
return a ~= 0
end
--- Create an "outline" image of an image.
-- The outline is a white, 1-pixel border around each non-transparent
-- pixel of the original image.
-- The returned image has a pixel-wide transparent border around it.
-- @param img the image
-- @return the outline image
function outlineimage(img)
local image = copyimg(img)
local width = img:getWidth()
local height = img:getHeight()
local data = img:getData()
local canvas = love.graphics.newCanvas(width + 2, height + 2)
local origcanvas = love.graphics.getCanvas()
local origr, origg, origb, origa = love.graphics.getColor()
love.graphics.setCanvas(canvas)
love.graphics.clear(0, 0, 0, 0)
love.graphics.push()
love.graphics.origin()
love.graphics.translate(1.5, 1.5)
love.graphics.setColor(255, 255, 255, 255)
for x = 0, width - 1 do
for y = 0, height - 1 do
local r, g, b, a = data:getPixel(x, y)
if a ~= 0 then
local offsets = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
for _, offset in ipairs(offsets) do
local nx = x + offset[1]
local ny = y + offset[2]
if nx >= -1 and nx < width + 1
and ny >= -1 and ny < height + 1
then
if nx < 0 or nx >= width
or ny < 0 or ny >= height
then
love.graphics.points(nx, ny)
else
local r, g, b, a = data:getPixel(nx, ny)
if a == 0 then
love.graphics.points(nx, ny)
end
end
end
end
end
end
end
love.graphics.setColor(origr, origg, origb, origa)
love.graphics.pop()
if origcanvas == nil then
love.graphics.setCanvas()
else
love.graphics.setCanvas(origcanvas)
end
local newdata = canvas:newImageData()
return love.graphics.newImage(newdata)
end
states = {
title = {},
intro = {},
play = {}
}
function switchmode(to)
if mode and states[mode].quit then
states[mode].quit()
end
mode = to
if states[mode].init then
states[mode].init()
end
end
function playconfirm()
confirm:play()
end
-- PLAY MODE
--- Prepare the outlines and missing fields of all objects.
-- This procedure fills out the fields of all existing objects
-- if those fields are not already defined, including the outline
-- images. You must run this after defining new objects.
-- This procedure mutates the global variable "objects".
function primeobjects()
for key, _ in pairs(objects) do
if objects[key].picked == nil then
objects[key].picked = false
end
if objects[key].outline == nil then
objects[key].outline = outlineimage(objects[key].image)
end
end
end
function states.play.init()
rooms.enter(rooms.intro)
end
function states.play.update()
if rooms.current.update then
rooms.current.update()
end
dialog.update()
end
function states.play.draw()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(backdrop, 0, 0)
if rooms.current.draw then
rooms.current.draw()
end
local picked = nil
-- draw all objects
for _, object in pairs(objects) do
love.graphics.draw(object.image, object.xoff, object.yoff)
end
-- identify the picked object
for _, object in pairs(objects) do
if object.picked then
picked = object
end
end
-- draw an outline around the picked object
if picked then
love.graphics.draw(picked.outline, picked.xoff - 1, picked.yoff - 1)
end
-- draw the active dialog if applicable
dialog.draw()
-- draw the name of the picked object
if picked then
dialog.showtext(picked.name)
end
end
function states.play.keypressed(key)
if dialog.isgrabbing() then
dialog.keypressed(key)
end
end
function states.play.mousemoved(x, y, dx, dy, istouch)
-- pick any object under the cursor
if dialog.isactive() then
dialog.mousemoved(x, y, dx, dy, istouch)
elseif y / scalefactor < virtualheight - 30 then
for _, object in pairs(objects) do
object.picked = ispicked(object.image,
x / scalefactor - object.xoff,
y / scalefactor - object.yoff)
picked = object.picked
end
else
for _, object in pairs(objects) do
object.picked = false
end
end
end
function states.play.mousepressed(x, y, button, istouch)
-- if a dialog is waiting for the user, let it process the press
if dialog.isgrabbing() then
dialog.mousepressed(x, y, button, istouch)
return
end
-- if a dialog is active, do nothing
if dialog.isactive() then
return
end
-- otherwise, if an object is picked, activate it
local done = false
for _, object in pairs(objects) do
if object.picked and not done then
object.picked = false
dialog.start(object.action)
done = true
end
end
end
-- title screen callbacks
function states.title.init()
fadeopacity = 0
switchbgmusic(music)
end
function states.title.update()
if fadeopacity < 255 then
fadeopacity = fadeopacity + 1
end
end
function states.title.draw()
love.graphics.setColor(255, 255, 255, fadeopacity)
love.graphics.draw(logo, 0, 0)
love.graphics.setColor(255, 255, 255, 255)
end
function startgame()
confirm:play()
music:stop()
switchmode('play')
end
function states.title.keypressed(key)
if key == kact and fadeopacity >= 100 then
startgame()
end
end
function states.title.mousepressed(x, y, dx, dy, istouch)
if fadeopacity >= 100 then
startgame()
end
end
-- main callbacks
function love.update()
if states[mode].update then
states[mode].update()
end
end
function love.draw()
love.graphics.clear()
love.graphics.scale(scalefactor, scalefactor)
if states[mode].draw then
states[mode].draw()
end
end
function love.keypressed(...)
if states[mode].keypressed then
states[mode].keypressed(...)
end
end
function love.mousemoved(...)
if states[mode].mousemoved then
states[mode].mousemoved(...)
end
end
function love.mousepressed(...)
if states[mode].mousepressed then
states[mode].mousepressed(...)
end
end