-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.lua
More file actions
374 lines (307 loc) · 8.62 KB
/
main.lua
File metadata and controls
374 lines (307 loc) · 8.62 KB
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
require "gob"
require "sprite"
require "weapons"
local mathx = require "mathx"
function initStars(width, height)
local stars = {}
for i = 1, 100 do
table.insert(stars, {
x = math.random(width),
y = math.random(height),
z = (math.random() ^ 4) * 0.9 + 0.1,
})
end
return stars
end
function initEnemies(count)
local enemies = {}
for i = 1, count do
-- randomize start frame
enemy = Gob:new(
{
sprite = sprites.candy,
time = math.random(100) / 100 + 100,
hit = 0
})
table.insert(enemies, enemy)
end
return enemies
end
function scatterEnemies()
for i, e in ipairs(enemies) do
e.x = math.random(200) + 200
e.y = math.random(200)
e.fps = math.random(8, 14)
end
end
function spawnCollectible(x, y)
local c = Gob:new(
{
-- Gob
x = x,
y = y,
sprite = sprites.thing,
-- Collectible
speed = -100 * math.random(75, 125) / 100,
score = 100,
})
table.insert(collectibles, c)
end
function loadImage(name)
image = love.graphics.newImage(name)
image:setFilter("linear", "linear")
return image
end
function setTint(enable, amount)
if (enable) then
tintEffect:send("tint", amount)
love.graphics.setShader(tintEffect)
else
love.graphics.setShader()
end
end
function love.load()
EPSILON = 1E-3
time = 0
score = math.floor(math.random() * -1000000)
-- establish scale factor
scale = 3
screenWidth = love.graphics.getWidth() / scale
screenHeight = love.graphics.getHeight() / scale
-- create offscreen render target
fb = love.graphics.newCanvas(screenWidth, screenHeight)
fb:setFilter("nearest", "nearest")
-- input state table
input = {}
controls = {
[" "] = "fire",
up = "up",
down = "down",
left = "left",
right = "right",
}
images = {
star = loadImage("images/star.png"),
}
sprites = {
bg = Sprite:new("images/bg.png"),
carrot = Sprite:newAnim("images/carrot.png", 5, {
center = { x = 14, y = 6 },
hitbox = { x = 9, y = 5, w = 12, h = 3 },
}),
candy = Sprite:newAnim("images/candy.png", 6, {
center = { x = 8, y = 8 },
--hitbox = { x = 0, y = 0, w = 16, h = 16 },
hitbox = { x = 6, y = 2, w = 4, h = 11 },
}),
thing = Sprite:newAnim("images/thing.png", 6, {
center = { x = 5, y = 5 },
hitbox = { x = 0, y = 0, w = 11, h = 11 }
}),
}
bg = Gob:new(
{
x = 0,
y = 0,
sprite = sprites.bg,
})
stars = initStars(screenWidth, screenHeight)
starSpeed = screenWidth * 1.5
starWidth = images.star:getWidth()
player = Gob:new(
{
x = 20,
y = 100,
sprite = sprites.carrot,
})
avgDeltaX = 0
avgDeltaY = 0
enableBulletAnim = true
collectibles = {}
-- init enemies
enemies = initEnemies(15)
scatterEnemies()
-- set font
font = love.graphics.newFont("fonts/easta_seven_condensed.ttf", 8)
love.graphics.setFont(font)
-- init pixel effects (shaders)
tintEffect = love.graphics.newShader("shaders/tint.fs")
blurEffect = love.graphics.newShader("shaders/blur.fs")
warpEffect = love.graphics.newShader("shaders/warp.fs")
-- XXX hack
blur = false
blurEffect:send("radius", 25)
blurEffect:send("angle", 0)
warp = false
-- misc setup
love.graphics.setBackgroundColor(0, 0, 0)
-- player input
love.mouse.setVisible(false)
love.mouse.setGrabbed(true)
love.mouse.setPosition(screenWidth / 2, screenHeight / 2)
startX, startY = love.mouse.getPosition()
end
function getMouseDeltas()
local startMouseX = screenWidth / 2 * scale
local startMouseY = screenHeight / 2 * scale
local currX, currY = love.mouse.getPosition()
local deltaX = (currX - startMouseX) / scale
local deltaY = (currY - startMouseY) / scale
-- reset to middle of window
love.mouse.setPosition(startMouseX, startMouseY)
return deltaX, deltaY
end
function love.draw()
love.graphics.push()
love.graphics.setCanvas(fb)
-- draw background
-- love.graphics.setColor(255, 255, 255, 255)
-- love.graphics.draw(images.bg, 0, 0)
bg:draw()
-- draw stars
for i, s in ipairs(stars) do
local starScale = math.max(1/starWidth, s.z)
local x = math.floor(s.x + 0.5)
local alpha = math.max(0.2, s.z) * 0.75
if slow then
starScale = starScale / 4
end
love.graphics.setColor(255, 255, 255, 255 * alpha)
love.graphics.draw(images.star, s.x, s.y, 0, starScale, 1.0)
end
-- restore color
love.graphics.setColor(255, 255, 255, 255)
-- draw weapon
drawWeapons()
-- draw collectibles
for _, c in pairs(collectibles) do
c:draw()
end
-- draw ship
player:draw()
-- draw candy
for i, e in pairs(enemies) do
if (e.hit > 0) then
setTint(true, 0.7)
end
e:draw()
if (e.hit > 0) then
e.hit = e.hit - 1
end
setTint(false, 0.0)
end
-- blit framebuffer to screen
love.graphics.pop()
love.graphics.setCanvas()
if (blur) then
love.graphics.setShader(blurEffect)
end
if (warp) then
warpEffect:send("time", time)
love.graphics.setShader(warpEffect)
end
love.graphics.draw(fb, 0, 0, 0, scale, scale)
love.graphics.setShader()
-- draw meaningless gibberish
love.graphics.print("SCORE: " .. score, 10, 10, 0, scale, scale)
-- love.graphics.print("Seiji Ozawa quit, vexing rabid symphonic folk!", 10, 25)
end
function love.update(dt)
-- if paused, bail
if paused then
return
end
handleInput()
-- compute time scale
local t = time
if slow then
dt = dt / 4
end
time = time + dt
-- update player
local deltaX, deltaY = getMouseDeltas()
player.currentFrame = 3
if avgDeltaY < -EPSILON then
player.currentFrame = 1
-- detect case when player changing directions
elseif mathx.sgn(avgDeltaY) ~= mathx.sgn(deltaY) then
player.currentFrame = 3
elseif avgDeltaY > EPSILON then
player.currentFrame = 5
end
-- keep 5 frame average of movement to figure out which way to turn the ship
avgDeltaY = (avgDeltaY + deltaY) / 2
player.x = player.x + deltaX
player.y = player.y + deltaY
player.x = math.max(math.min(player.x, screenWidth), 0)
player.y = math.max(math.min(player.y, screenHeight), 0)
if input.up then
player.currentFrame = 1
player.y = player.y - 150 * dt
elseif input.down then
player.currentFrame = 5
player.y = player.y + 150 * dt
end
if input.left then
player.x = player.x - 150 * dt
elseif input.right then
player.x = player.x + 150 * dt
end
-- update stars
for i, s in pairs(stars) do
s.x = s.x - s.z * starSpeed * dt
if s.x < -starWidth then
s.x = screenWidth + starWidth
s.y = math.random(screenHeight)
end
end
-- update collectibles
for c_i, c in pairs(collectibles) do
c:update(dt)
local dv = c.speed * dt
local dx = dv
local dy = 0
c.x = c.x + dx
c.y = c.y - dy
-- XXX: need to account for bullet size!
if c.x < 0 or c.x > screenWidth or c.y < 0 or c.y > screenHeight then
table.remove(collectibles, c_i)
end
if (player:hitGob(c)) then
score = score + c.score
table.remove(collectibles, c_i)
end
end
-- update enemies
for s_i, s in pairs(enemies) do
s:update(dt)
end
-- update weapon
updateWeapons(dt, input.fire, player.x, player.y)
end
function love.keypressed(key, unicode)
if key == "p" then
paused = not paused
elseif key == "s" then
slow = not slow
elseif key == "d" then
enableBulletAnim = not enableBulletAnim
elseif key == "r" then
scatterEnemies()
elseif key == "b" then
blur = not blur
elseif key == "w" then
warp = not warp
elseif key == "escape" then
love.event.quit()
elseif key == "return" then
warpEffect = love.graphics.newShader("warp.fs")
elseif key == "tab" then
switchWeapon()
end
end
function handleInput()
for key, state in pairs(controls) do
input[state] = love.keyboard.isDown(key)
end
end