-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
363 lines (300 loc) · 8.72 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
function love.load()
-- Version number
version = 'V1.0'
-- Timer variable
dtotal = 0
-- Set screen attributes
screen = {
width = 320,
height = 240
}
-- Set player attributes
player = {
x = 0,
y = 0,
speed = 200,
gravity = 100,
width = 24,
height = 24
}
-- Set bullet attributes
bullet = {
x = 0,
y = 0,
speed = 300,
width = 15,
height = 10,
img = love.graphics.newImage('assets/bullet.png')
}
-- Set game state
gamestate = 'title'
-- Set window attributes
love.window.setTitle('Moonman')
love.window.setMode(screen.width * 2, screen.height * 2)
-- Set default scaling filter
love.graphics.setDefaultFilter('nearest', 'nearest')
-- Create new canvas
canvas = love.graphics.newCanvas(screen.width, screen.height)
-- Create new animation
animPlayer = newAnimation(love.graphics.newImage('assets/moonman.png'), 24, 24, 0.5)
animEnemy = newAnimation(love.graphics.newImage('assets/alien.png'), 24, 24, 1)
-- Load sprites
logo = love.graphics.newImage('assets/logo.png')
background = love.graphics.newImage('assets/background.png')
-- Load font
font = love.graphics.newImageFont('assets/font.png', ' !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~')
love.graphics.setFont(font)
-- Load music
musicTrack = love.audio.newSource('assets/music.wav', 'static')
musicTrack:setLooping(true)
-- Load sound effects
shootSfx = love.audio.newSource('assets/laser.wav', 'static')
shootSfx:setVolume(0.5)
playerDestroySfx = love.audio.newSource('assets/death.wav', 'static')
playerDestroySfx:setVolume(0.5)
enemyDestroySfx = love.audio.newSource('assets/explosion.wav', 'static')
enemyDestroySfx:setVolume(0.75)
end
function love.update(dt)
if gamestate == 'play' then
-- Left and right movement key binds
if love.keyboard.isDown('a', 'left') then
if player.x > 0 then
player.x = player.x - (player.speed * dt)
end
elseif love.keyboard.isDown('d', 'right') then
if player.x < (screen.width - player.width) then
player.x = player.x + (player.speed * dt)
end
end
-- Jetpack key bind
if love.keyboard.isDown('w', 'up') then
if player.y > 20 then
player.y = player.y - ((player.speed + player.gravity) * dt)
end
end
-- Continuously apply gravity to player
if player.y < screen.height then
player.y = player.y + (player.gravity * dt)
end
-- Game over if touching bottom of screen
if player.y > (screen.height - player.height) then
endGame()
end
-- Update animations
animPlayer.currentTime = animPlayer.currentTime + dt
if animPlayer.currentTime >= animPlayer.duration then
animPlayer.currentTime = animPlayer.currentTime - animPlayer.duration
end
animEnemy.currentTime = animEnemy.currentTime + dt
if animEnemy.currentTime >= animEnemy.duration then
animEnemy.currentTime = animEnemy.currentTime - animEnemy.duration
end
-- Move projectile
bullet.x = bullet.x + (bullet.speed * dt)
if bullet.x > screen.width then
bullet.x = 1000
end
-- Update enemies
for i=#enemies, 1, -1 do
local enemy = enemies[i]
if not enemy.removed then
enemy.x = enemy.x - (enemy.speed * dt)
-- Remove enemy when off screen
if enemy.x < (0 - enemy.width) then
enemy.removed = true
end
-- Collision checking (projectile)
if checkCollision(enemy.x,enemy.y,enemy.width,enemy.height,bullet.x,bullet.y,bullet.width,bullet.height) then
enemy.removed = true
bullet.x = 1000
score = score + 100
playSound(enemyDestroySfx)
local explosion = getExplosion(getBlast(50))
explosion:setPosition(enemy.x + enemy.width/2, enemy.y + enemy.height/2)
explosion:emit(5)
table.insert(explosions, explosion)
end
-- Collision checking (player)
if checkCollision(enemy.x,enemy.y,enemy.width,enemy.height,player.x,player.y,player.width,player.height) then
endGame()
end
else table.remove(enemies, i) end
end
-- Update explosions
for i=#explosions, 1, -1 do
local explosion = explosions[i]
explosion:update(dt)
if explosion:getCount() == 0 then
table.remove(explosions, i)
end
end
-- Timer (1s)
dtotal = dtotal + dt
if dtotal >= 1 then
dtotal = dtotal - 1
-- Spawn an enemy
newEnemy(500, love.math.random(20, 216))
-- Add to score
score = score + 10
end
end
end
function love.keypressed(key)
-- Quit when escape is pressed
if key == 'escape' then
love.event.push('quit')
end
-- Space to start game or shoot
if key == 'space' then
if gamestate == 'title' then
gamestate = 'play'
startGame()
elseif gamestate == 'play' then
shootGun()
elseif gamestate == 'dead' then
gamestate = 'title'
end
end
end
function love.draw(dt)
if gamestate == 'title' then
-- Draw title screen
love.graphics.clear()
love.graphics.draw(logo, 58, 92, 0, 2, 2)
love.graphics.printf('PRESS SPACE TO START', 0, 300, 320, 'center', 0, 2, 2)
love.graphics.printf('A GAME BY ALEX ABBATIELLO'..string.char(10)..'@SYNTHIC 2019', 0, 410, 320, 'center', 0, 2, 2)
love.graphics.print(version)
else
-- Set drawing target to canvas
love.graphics.setCanvas(canvas)
-- Clear previous drawing
love.graphics.clear()
-- Draw background
love.graphics.draw(background, 0, 0)
-- Draw score
love.graphics.print('SCORE'..string.char(10)..math.floor(score))
-- Animate player sprite
local spriteNum = math.floor(animPlayer.currentTime / animPlayer.duration * #animPlayer.quads) + 1
love.graphics.draw(animPlayer.spriteSheet, animPlayer.quads[spriteNum], player.x, player.y)
-- Animate enemies
for i=#enemies, 1, -1 do
local enemy = enemies[i]
local spriteNum = math.floor(animEnemy.currentTime / animEnemy.duration * #animEnemy.quads) + 1
love.graphics.draw(animEnemy.spriteSheet, animEnemy.quads[spriteNum], enemy.x, enemy.y)
end
-- Draw projectile
love.graphics.draw(bullet.img, bullet.x, bullet.y)
-- Draw explosions
for i=#explosions, 1, -1 do
local explosion = explosions[i]
love.graphics.draw(explosion, 0, 0)
end
-- Set drawing target to window
love.graphics.setCanvas()
-- Draw full canvas and scale
love.graphics.draw(canvas, 0, 0, 0, 2, 2)
-- Draw game over screen
if gamestate == 'dead' then
love.graphics.setCanvas(canvas)
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle('fill', 75, 85, 170, 55)
love.graphics.setColor(255, 255, 255)
love.graphics.printf('GAME OVER', 0, 100, 160, 'center', 0, 2, 2)
love.graphics.setCanvas()
love.graphics.draw(canvas, 0, 0, 0, 2, 2)
end
end
end
function startGame()
-- Reset positions
player.x = 40
player.y = 40
bullet.x = 1000
-- Reset score
score = 0
-- Reset entities
enemies = {}
explosions = {}
-- Start music
musicTrack:play()
-- Spawn enemies
for i=1,2 do
newEnemy(500, love.math.random(20, 216))
end
end
function endGame()
-- Stop music
musicTrack:stop()
-- Play sound
playSound(playerDestroySfx)
-- Game over
gamestate = 'dead'
end
function shootGun()
-- Shoot if no projectile on screen
if bullet.x > screen.width then
bullet.x = player.x + player.width - 2
bullet.y = player.y + ((player.height / 2) - (bullet.height / 2))
playSound(shootSfx)
end
end
function newEnemy(x,y)
local enemy = {}
enemy.x = x
enemy.y = y
enemy.width = 24
enemy.height = 24
enemy.speed = love.math.random(100, 300)
enemy.removed = false
table.insert(enemies, enemy)
end
function playSound(sound)
sound:stop()
pitchMod = 0.8 + love.math.random(0, 10) / 25
sound:setPitch(pitchMod)
sound:play()
end
function getBlast(size)
local blast = love.graphics.newCanvas(size, size)
love.graphics.setCanvas(blast)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.circle('fill', size/2, size/2, size/2)
love.graphics.setCanvas()
return blast
end
function getExplosion(image)
pSystem = love.graphics.newParticleSystem(image, 30)
pSystem:setParticleLifetime(0.5, 0.5)
pSystem:setLinearAcceleration(-100, -100, 100, 100)
pSystem:setColors(255, 255, 0, 255, 255, 153, 51, 255, 64, 64, 64, 0)
pSystem:setSizes(0.5, 0.5)
return pSystem
end
--[[
Create animation from sprite sheet
https://love2d.org/wiki/Tutorial:Animation
--]]
function newAnimation(image, width, height, duration)
local animation = {}
animation.spriteSheet = image
animation.quads = {}
for y = 0, image:getHeight() - height, height do
for x = 0, image:getWidth() - width, width do
table.insert(animation.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
animation.duration = duration or 1
animation.currentTime = 0
return animation
end
--[[
Collision detection function
https://love2d.org/wiki/BoundingBox.lua
--]]
function checkCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end