-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
371 lines (318 loc) · 10.2 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
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 960
-- 628
-- 1200
ASTR_MAX = 5
math.randomseed(os.time())
asteroids = {}
astrDataPools = {}
astrDataPoolsCtr = 1
isAlive = nil
isPaused = nil
canShoot = nil
boomIdx = -1
boomProgress = 0
shoot = 0
shotOffset = 0
score = 0
gameTime = 0
function love.load()
love.window.setTitle('Space Shooter 50')
-- love.graphics.setDefaultFilter('nearest', 'nearest')
smallFont = love.graphics.newFont('graphics/font.ttf', 15)
largeFont = love.graphics.newFont('graphics/font.ttf', 50)
love.graphics.setFont(smallFont)
love.window.setMode(WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
resizable = true,
})
-- player
player = {}
-- asteroid
color1 = hex2rgb('#FFC854')
color2 = hex2rgb('#FF960D')
color3 = hex2rgb('#F56600')
color4 = hex2rgb('#B03509')
color5 = hex2rgb('#FF1D00')
--asteroids = {}
asteroidsColor = { color1, color2, color3, color4, color5 }
skyShader = love.graphics.newShader('graphics/SkyShader.sh')
asteroidsShader = love.graphics.newShader('graphics/AsteroidsShader.sh')
shipShader = love.graphics.newShader('graphics/ShipShader.sh')
boomShader = love.graphics.newShader('graphics/VFXShader.sh')
soundtrack = love.audio.newSource('sounds/soundtrack.ogg', 'stream')
sounds = {
['boom'] = love.audio.newSource('sounds/boom.ogg', 'static'),
['shoot'] = love.audio.newSource('sounds/shoot.ogg', 'static'),
['rip'] = love.audio.newSource('sounds/rip.ogg', 'static')
}
gameState = 'start'
isPaused = false
soundtrack:setLooping(false) -- set to true later
-- love.audio.play(soundtrack)
end
function love.keypressed(key, u)
if key == 'escape' then
love.event.quit()
end
if key == "p" then
isPaused = not isPaused
end
if key == "space" then
canShoot = true
sounds['shoot']:play()
end
if gameState == 'done' or gameState == 'start' then
if key == 'enter' or key == 'return' then
gameState = 'play'
isAlive = true
initShip()
asteroids = {}
astrDataPools = getAstrDataPools()
for i = 1, ASTR_MAX do
local asteroid = initAstr()
table.insert(asteroids, asteroid)
end
end
end
end
function love.update(dt)
if isPaused then
return
end
gameTime = gameTime + dt
skyShader:send("time", gameTime)
if gameState == 'play' then
-- player
if love.keyboard.isDown('left', 'a') then
player.xSpeed = player.xSpeed - dt
end
if love.keyboard.isDown('right', 'd') then
player.xSpeed = player.xSpeed + dt
end
if player.xSpeed < 0 then
player.xSpeed = player.xSpeed + dt * .25
end
if player.xSpeed > 0 then
player.xSpeed = player.xSpeed - dt * .25
end
-- player doesn't go off screen
if player.x > 0.05 then
player.x = player.x + player.xSpeed * 0.01
else
player.x = 0.06
player.xSpeed = 0
end
if player.x < 0.95 then
player.x = player.x + player.xSpeed * 0.01
else
player.x = 0.94
player.xSpeed = 0
end
-- shoot lasers
shootStop = 0.8
if canShoot == true then
if shoot < shootStop then
shoot = shoot + dt * 2
else
shotOffset = shotOffset + dt * 6
shoot = shoot + dt * .3
end
if shotOffset > 2.4 then
shoot = 0
canShoot = false
shotOffset = 0
end
end
-- asteroid go brrr
if boomIdx >= 0 then
local boomXY = {asteroids[boomIdx].x, asteroids[boomIdx].y}
boomProgress = boomProgress + dt
if boomProgress > 0.5 then
-- asteroid destroyed
asteroids[boomIdx] = initAstr()
end
if boomProgress >= 1 then
boomProgress = 0
boomIdx = -1
end
shoot = 0
shotOffset = 0
canShoot = false
boomShader:send("seed", boomSeed)
boomShader:send("progress", boomProgress)
boomShader:send("position", boomXY)
end
-- asteroid
local astrXY = {}
local astRot = {}
local seeds = {}
local colors = {}
for i, astr in ipairs(asteroids) do
astr.y = astr.y + (astr.speed * dt)
table.insert(astrXY, {astr.x, astr.y})
table.insert(astRot, astr.rotation)
table.insert(seeds, astr.seed)
table.insert(colors, astr.color)
if astr.y > 1.2 then
-- circle collision stolen from https://sheepolution.com/learn/book/21
distance = magnitute(astr.x, astr.y, player.x, player.y)
if distance < astr.size + player.size then
sounds['rip']:play()
isAlive = false
asteroids[i] = initAstr()
end
end
if astr.y > 1.8 then
asteroids[i] = initAstr()
end
local shotDist = 0 -- from projectile to asteroid
local shotXY = {
x = player.x,
y = 2 - shotOffset
}
shotDist = magnitute(astr.x, astr.y, shotXY.x, shotXY.y)
if shotDist < astr.size then
sounds['boom']:play()
shoot = 0
canShoot = 0
shotOffset = 0
score = score + 5
boomIdx = i
boomSeed = math.random(1, 1024^2)
end
end
-- send all the shaders :3
local thrust = -math.max(-1, math.min(1, player.xSpeed * 3))
shipShader:send("time", gameTime)
shipShader:send("position", {player.x, player.y})
shipShader:send("thrust", thrust * .5 + .5)
shipShader:send("shoot", shoot)
shipShader:send("shotOffset", shotOffset)
asteroidsShader:send("time", gameTime)
asteroidsShader:send("coords", unpack(astrXY))
asteroidsShader:send("rotations", unpack(astRot))
asteroidsShader:send("seeds", unpack(seeds))
asteroidsShader:send("colors", unpack(colors))
-- reset game
if not isAlive then
gameState = 'done'
player.x = -1
player.y = -1
score = 0
end
end
end
function love.draw()
love.graphics.setShader(skyShader)
love.graphics.rectangle('fill', 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
love.graphics.setShader()
if gameState == 'start' then
love.graphics.setFont(largeFont)
love.graphics.printf("Space Shooter CS50!", 0, WINDOW_HEIGHT / 4, WINDOW_WIDTH, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf("Press Enter to begin!", 0, 550, WINDOW_WIDTH, 'center')
elseif gameState == 'play' then
love.graphics.setFont(smallFont)
love.graphics.print("Score: " .. tostring(score), 15, 10)
love.graphics.setShader(asteroidsShader)
love.graphics.rectangle('fill', 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
love.graphics.setShader()
love.graphics.setShader(shipShader)
love.graphics.rectangle('fill', 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
love.graphics.setShader()
if boomIdx >= 0 then
love.graphics.setShader(boomShader)
love.graphics.rectangle('fill', 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
love.graphics.setShader()
end
elseif gameState == 'done' then
love.graphics.printf("Press Enter to try again!", 0, 140, WINDOW_WIDTH, 'center')
end
end
-- https://gist.github.com/jasonbradley/4357406
function hex2rgb(hex)
hex = hex:gsub("#","")
return {tonumber(hex:sub(1,2), 17) / 255, tonumber(hex:sub(3,4), 17) / 255, tonumber(hex:sub(5,6), 17) / 255}
end
function initShip()
player = {
x = 0.5,
y = 1.4,
size = 0.06,
xSpeed = 0
}
end
function getAstrDataPools()
local result = {
Xs = {},
Ys = {},
cols = {},
rots = {}
}
local step = 1 / (ASTR_MAX + 1)
for i = 1, ASTR_MAX do
table.insert(result.Xs, i * step)
table.insert(result.Ys, -i * step * 2)
table.insert(result.cols, asteroidsColor[i])
table.insert(result.rots, math.floor(-ASTR_MAX * 0.5))
end
return result
end
function initAstr()
local poolSize = ASTR_MAX - astrDataPoolsCtr + 1;
local asteroid = {
x = astrDataPools.Xs[math.random(1, poolSize)],
y = astrDataPools.Ys[math.random(1, poolSize)],
speed = 0.2,
seed = math.random(1, 9000),
rotation = astrDataPools.rots[math.random(1, poolSize)],
color = astrDataPools.cols[math.random(1, poolSize)],
size = 0.08
}
removeTblValue(astrDataPools.Xs, asteroid.x)
removeTblValue(astrDataPools.Ys, asteroid.y)
removeTblValue(astrDataPools.rots, asteroid.rotation)
removeTblValue(astrDataPools.cols, asteroid.color)
astrDataPoolsCtr = astrDataPoolsCtr + 1
if astrDataPoolsCtr > ASTR_MAX then
astrDataPools = getAstrDataPools()
astrDataPoolsCtr = 1
end
return asteroid
end
function removeTblValue(tbl, value)
for i, v in ipairs(tbl) do
if v == value then
table.remove(tbl, i)
end
end
end
-- copy paste code from https://stackoverflow.com/a/11671820
function map(tbl, f)
local t = {}
for k,v in pairs(tbl) do
t[k] = f(v)
end
return t
end
-- ofc you have to write your own function for everything
-- https://stackoverflow.com/questions/2705793/how-to-get-number-of-entries-in-a-lua-table
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- clone table
-- https://stackoverflow.com/a/641993
function tableCopy(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
function magnitute(p1x, p1y, p2x, p2y)
dist = math.sqrt((p1x - p2x)^2 + (p1y - p2y)^2)
return dist
end