-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
327 lines (270 loc) · 9.53 KB
/
main.lua
File metadata and controls
327 lines (270 loc) · 9.53 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
require 'Player'
require 'Background'
require 'UpperObstacles'
require 'LowerObstacles'
require 'Alien'
require 'Boss'
require 'Bonus'
require 'BackGroundObject'
require 'Menu'
require 'Home'
require 'GameOver'
--Screen size is 800x600
--Background size is 1600x600
aliens = {}
bossIsOn = false
currentDifficulty = 1
timeSinceLastAlien = 0
gameOnceLoaded = false
function game_load()
if gameOnceLoaded then
resetGame()
return
end
shootingSound = love.audio.newSource('lazer.mp3')
Player.xPos = love.graphics.getWidth() / 10
Player.yPos = love.graphics.getHeight() / 2
Player.image = love.graphics.newImage(Player.imagePath)
Player.height = Player.image:getHeight()
Player.width = Player.image:getWidth()
Background.image2 = love.graphics.newImage(Background.image2Path)
Background.image = love.graphics.newImage(Background.imagePath)
Background.initialize()
for i=1,10 do
Background.newBackgroundObject()
end
Boss.image = love.graphics.newImage(Boss.imagePath)
Bullet.image = love.graphics.newImage(Bullet.imagePath)
Alien.image = love.graphics.newImage(Alien.imagePath)
Alien.height = Alien.image:getHeight()
Alien.width = Alien.image:getWidth()
font = love.graphics.newFont('Xcelsion.ttf', 20)
gameOnceLoaded = true
end
function game_update(dt)
if love.keyboard.isDown('space') then
Player:fire()
end
if love.keyboard.isDown('down') then
Player:moveDown(dt)
end
if love.keyboard.isDown('up') then
Player:moveUp(dt)
end
Player.timeSinceLastBullet = Player.timeSinceLastBullet + dt
if bossIsOn then
Boss.shootCooldown = Boss.shootCooldown + dt
Boss:move()
Boss:fire() --doesnt actually fire each time; checks the cooldown first
end
if (timeSinceLastAlien >= 3) then
aliens[Alien.new(Player.speed * currentDifficulty * 20)] = true
timeSinceLastAlien = 0
else
timeSinceLastAlien = timeSinceLastAlien + dt
end
moveBullets(Player.bullets, dt)
moveBossBullets(Boss.bullets, dt)
Background:move(dt)
UpperObstacles:move(dt)
LowerObstacles:move(dt)
moveAliens(aliens, dt)
if not bossIsOn then
checkForShotAliens(aliens, Player.bullets)
else
checkForShotBoss(Player.bullets)
checkForShotPlayer(Boss.bullets)
end
end
function game_draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(Background.image, Background.xPos, Background.yPos)
love.graphics.draw(Background.image, Background.xPos2, Background.yPos)
Background.draw()
drawBullets(Player.bullets)
love.graphics.draw(Player.image, Player.xPos, Player.yPos)
if UpperObstacles:isPlayerOverLapping(Player) or LowerObstacles:isPlayerOverLapping(Player) then
--love.graphics.rectangle('fill', Player:getXPos(), Player:getYPos(), Player.width, Player.height)
Player:changeScore(-0.0050)
end
UpperObstacles:draw()
LowerObstacles:draw()
love.graphics.setFont(font)
--love.graphics.draw(love.graphics.newImage('background texture.png'), 0, 0)
love.graphics.draw(Background.image2, Background.img2xPos, Background.yPos)
love.graphics.draw(Background.image2, Background.img2xPos2, Background.yPos)
love.graphics.print('Score: ' .. math.floor(Player.score + 0.5), 10, 10)
love.graphics.print('Difficulty: ' .. currentDifficulty, 10, 575)
if not bossIsOn then
drawAliens(aliens)
else
Boss:draw()
love.graphics.print('Boss Health: ' .. Boss.health .. '/' .. Boss.maxHealth, 300, 10)
Boss:drawBullets()
end
end
function drawBullets(tableOfBullets)
if type(tableOfBullets) ~= 'table' then
return
end
for bul, _ in pairs(tableOfBullets) do
Bullet.draw(bul)
end
end
function drawAliens(tableOfAliens)
for value, key in pairs(tableOfAliens) do
--love.graphics.rectangle('fill', value.xpose, value.ypose, value.width, value.height)
love.graphics.draw(Alien.image, value.xpose, value.ypose)
end
end
function moveBullets(tableOfBullets , dt)
if type(tableOfBullets) ~= 'table' then
return
end
local toDelete={}
local width = love.graphics.getWidth()
for bull, _ in pairs(tableOfBullets) do
Bullet.move(bull,dt,1)
if bull.xPos > width or bull.xPos < 0 then
toDelete[bull]=true
end
end
for bull, _ in pairs(toDelete) do
tableOfBullets[bull]=nil
end
end
function moveBossBullets(tableOfBullets, dt)
local toDelete={}
local width = love.graphics.getWidth()
for bull, _ in pairs(tableOfBullets) do
Bullet.specialMove(bull,dt,1)
if bull.xPos > width or bull.xPos < 0 then
toDelete[bull]=true
end
end
for bull, _ in pairs(toDelete) do
tableOfBullets[bull]=nil
end
end
function moveAliens(tableOfAliens, dt)
local aliensToDelete = {}
for alien, _ in pairs(tableOfAliens) do
Alien.move(alien, dt)
--remove the alien form current aliens when
if alien.xpose < 0 then
aliensToDelete[alien] = true
end
end
for alien, _ in pairs(aliensToDelete) do
Player:changeScore(-20 * currentDifficulty)
tableOfAliens[alien] = nil
end
end
function checkForShotAliens (tableOfAliens, tableOfBullets)
local bullsToDelete = {}
local aliensToDelete = {}
for bul, _ in pairs(tableOfBullets) do
for ali, __ in pairs(tableOfAliens) do
if pointIsInRectangle(bul.xPos, bul.yPos, unpack(Alien.getRectangle(ali))) then
updatePointsOnAlienKill()
aliensToDelete[ali] = true
bullsToDelete[bul] = true
end
end
end
for bul, _ in pairs(bullsToDelete) do
tableOfBullets[bul] = nil
end
for ali, _ in pairs(aliensToDelete) do
tableOfAliens[ali] = nil
end
end
function checkForShotBoss (tableOfBullets)
local toBeDeleted = {}
for bul, _ in pairs(tableOfBullets) do
if pointIsInRectangle(bul.xPos, bul.yPos, unpack(Boss:getRectangle())) then
Boss:getHit()
toBeDeleted[bul] = true
end
end
for bul, _ in pairs(toBeDeleted) do
tableOfBullets[bul] = nil
end
end
function checkForShotPlayer (tableOfBullets)
local playerRectangle = Player:getRectangle()
for bul, _ in pairs(tableOfBullets) do
if bul ~= nil then --for some reason is still entered the for even when there were no elements
--unexplicable but i had no time
if rectanglesOverlap(Player.xPos, Player.yPos, Player.image:getWidth(), Player.image:getHeight(), bul.xPos, bul.yPos, bul.image:getWidth(), bul.image:getHeight()) then
gameOver()
end
end
end
end
function updatePointsOnAlienKill()
Player:changeScore(100)
Player.scoreSinceLastBoss = Player.scoreSinceLastBoss + 100
if Player.scoreSinceLastBoss >= 1200 then
Boss:spawn()
bossIsOn = true
end
end
function bossIsDead()
Player:changeScore(1000 * currentDifficulty)
bossIsOn = false
Player.scoreSinceLastBoss = 0
--Player.speed = 100 + currentDifficulty*10
currentDifficulty = currentDifficulty + 1
end
--returns true is the first a point with the coordinates of the first two arguments
--is in the rectangle constructed by the origin of the rectangle and its width and height
function pointIsInRectangle(pointX, pointY, rectX, rectY, width, height)
if rectX < rectX + width then
leftSide = rectX
rightSide = rectX + width
else
rigthSide = rectX
leftSide = rectX + width
end
if rectY < rectY + height then
topSide = rectY
bottomSide = rectY + height
else
bottomSide = rectY
topSide = rectY + height
end
return (pointX >= leftSide) and (pointX <= rightSide) and (pointY >= topSide) and (pointY <= bottomSide)
end
--receives two rectangles as arguments; the starting points of the rectangles (x,y)
--returns true only if a point from one is INSIDE the other; i.e. return false
--if they have a common edge; return false if they are separate as well
function rectanglesOverlap(rect1X, rect1Y, rect1Width, rect1Height, rect2X, rect2Y, rect2Width, rect2Height)
--just checking if each of the edges of one of the rects is inside the other
--with the pointIsInRectangle function
local topEdge1X, topEdge1Y = rect1X, rect1Y
local topEdge2X, topEdge2Y = rect1X + rect1Width, rect1Y
local bottomEdge1X, bottomEdge1Y = rect1X, rect1Y + rect1Height
local bottomEdge2X, bottomEdge2Y = rect1X + rect1Width, rect1Y + rect1Height
local point1Inside = pointIsInRectangle(topEdge1X, topEdge1Y, rect2X, rect2Y, rect2Width, rect2Height)
local point2Inside = pointIsInRectangle(topEdge2X, topEdge2Y, rect2X, rect2Y, rect2Width, rect2Height)
local point3Inside = pointIsInRectangle(bottomEdge1X, bottomEdge1Y, rect2X, rect2Y, rect2Width, rect2Height)
local point4Inside = pointIsInRectangle(bottomEdge2X, bottomEdge2Y, rect2X, rect2Y, rect2Width, rect2Height)
return point1Inside or point2Inside or point3Inside or point4Inside
end
function gameOver ()
gamestate = 'Game Over'
gameover_load()
end
function resetGame()
Player.score = 0
Player.scoreSinceLastBoss = 0
Player.bullets = {}
Player.timeSinceLastBullet = 0
Player.xPos = love.graphics.getWidth() / 10
Player.yPos = love.graphics.getHeight() / 2
aliens = {}
timeSinceLastAlien = 0
bossIsOn = false
currentDifficulty = 1
end