-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
280 lines (243 loc) · 9.77 KB
/
game.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
-- Copyright (C) 2023 Alessandro Amatucci Girlanda
-- This file is part of Invaders in Space.
-- Invaders in Space is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- Invaders in Space is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with Invaders in Space. If not, see <https://www.gnu.org/licenses/>.
Game = Object:extend()
function Game:new()
-- Check if player lost
self.game_over = false
self.player_dead = false
-- Create Objects/Entities
self.player = Player(player_pic, WINDOW_WIDTH_CENTER - PLAYER_WIDTH / 2, WINDOW_HEIGHT - PLAYER_HEIGHT * 2)
-- Create Projectiles
self.projectiles_timer = PROJECTILES_WAIT
self.projectiles = {}
-- Create Enemies
self.enemies_adjust = false
local enemies_x_start = (WINDOW_WIDTH - (ENEMIES_COLS * ENEMY_WIDTH + (ENEMIES_COLS - 1) * ENEMIES_GAP)) / 2
local enemies_y_start = 30
self.enemies_direction = 1
-- Every half number of enemy rows (round up) gains one additional health
local enemies_rows = {}
local enemies_to_insert = ENEMIES_ROWS
local health = 1
while(enemies_to_insert > 0) do
local rows = math.ceil(enemies_to_insert / 2)
for i=1,rows do
table.insert(enemies_rows, health)
end
enemies_to_insert = enemies_to_insert - rows
health = health + 1
end
self.enemies = {}
for r=1,#enemies_rows do
local tmp_enemies = {}
for c=1,ENEMIES_COLS do
local enemy = Enemy(enemies_pic, enemies_x_start + (ENEMY_WIDTH + ENEMIES_GAP) * (c - 1), enemies_y_start + (ENEMY_HEIGHT + ENEMIES_GAP) * (r - 1), enemies_rows[#enemies_rows - r + 1])
table.insert(tmp_enemies, enemy)
end
table.insert(self.enemies, tmp_enemies)
end
-- Enemies Projectiles
self.e_projectiles_wait = ENEMIES_PROJECTILES_WAIT
self.e_projectiles_dec = 0
self.e_projectiles_timer = 0
self.e_projectiles = {}
-- Explosions
self.explosions = {}
-- Game Over
self.game_over_timer = 0
self.game_over_wait = 2
end
function Game:update(dt)
if (self.game_over) then
self:gameOver(dt)
else
self:timersUpdate(dt)
self.player:update(dt)
self:projectilesUpdate(dt)
self:aliensUpdate(dt)
self:eProjectilesUpdate(dt)
end
self:explosionsUpdate(dt)
end
function Game:draw()
arrayDraw(self.explosions)
self:playerDraw()
arrayDraw(self.projectiles)
self:enemiesDraw()
arrayDraw(self.e_projectiles)
end
-- UPDATE FUNCTIONS --
function Game:timersUpdate(dt)
-- Shoot no more than the maximum number of player's projectiles every "projectiles_wait" seconds
if self.projectiles_timer < PROJECTILES_WAIT then
self.projectiles_timer = self.projectiles_timer + dt
end
-- Shoot enemies' projectiles every "e_projectiles_wait" seconds
if self.e_projectiles_timer < ENEMIES_PROJECTILES_WAIT then
self.e_projectiles_timer = self.e_projectiles_timer + dt
end
end
function Game:projectilesUpdate(dt)
-- Shoot Player's projectile
if love.keyboard.isDown("space") and #self.projectiles < PROJECTILES_MAX and self.projectiles_timer >= PROJECTILES_WAIT then
self.projectiles_timer = self.projectiles_timer - PROJECTILES_WAIT
local projectile = Projectile(projectile_pic, projectile_sound, self.player.x + self.player.width / 2 - PROJECTILES_WIDTH / 2, self.player.y)
table.insert(self.projectiles, projectile)
playSound(projectile.sound)
end
-- Move Player's projectiles, if any
for i,v in ipairs(self.projectiles) do
v:update(dt, -1)
end
-- Clear player's projectiles (avoiding memory leaks)
for i=#self.projectiles,1,-1 do
if self.projectiles[i].y + self.projectiles[i].height <= 0 then
table.remove(self.projectiles, i)
end
end
end
function Game:aliensUpdate(dt)
-- Aliens movement
for i,v in ipairs(self.enemies) do
for j,enemy in ipairs(v) do
if (enemy ~= 0) then
enemy:update(dt, self.enemies_direction)
if (self.enemies_direction == 1 and enemy.x + enemy.width > WINDOW_WIDTH) or (self.enemies_direction == -1 and enemy.x < 0) then
self.enemies_adjust = true
self.enemies_direction = self.enemies_direction * -1
end
-- Check for collision with projectiles, if any
-- Remove projectile and reset projectile timer
-- Remove enemy (set it to zero)
for p,projectile in ipairs(self.projectiles) do
if (projectile:collision(enemy, -1)) then
self.projectiles_timer = PROJECTILES_WAIT
table.remove(self.projectiles, p)
self.enemies[i][j]:updateHealth(-1)
if (self.enemies[i][j].health == 0) then
-- Explosion animation
self:generateExplosion(self.enemies[i][j].x, self.enemies[i][j].y)
self.enemies[i][j] = 0
-- Check if the whole column is zero
if (j == 1 or j == #v) then
self.enemies = removeColumn(self.enemies, j)
end
-- Chech if all enemies are dead
if (#self.enemies[1] <= 0) then
gameover:resultUpdate()
current_page = "Game Over"
else
for i,v in ipairs(self.enemies) do
for j, enemy in ipairs(v) do
if (enemy ~= 0) then
enemy:updateSpeed(ENEMIES_SPEED_INC)
end
end
end
end
-- Increase projectiles spawning time
self.e_projectiles_dec = self.e_projectiles_dec + ENEMIES_PROJECTILES_DEC
end
break
end
end
end
end
end
-- Adjust the enemies in the screen
if (self.enemies_adjust) then
self.enemies = adjustEnemies(self.enemies, self.enemies_direction * -1)
self.enemies_adjust = false
end
-- Check Enemies colliding with the player
self.game_over = enemiesCollision(self.enemies, self.player)
end
function Game:eProjectilesUpdate(dt)
-- Create Enemies projectiles
if (self.e_projectiles_timer >= self.e_projectiles_wait) then
self.e_projectiles_timer = self.e_projectiles_timer - self.e_projectiles_wait
::random_enemy::
local e_row = math.random(1, #self.enemies)
local e_col = math.random(1, #self.enemies[1])
if (self.enemies[e_row][e_col] == 0) then
goto random_enemy
elseif (self.enemies[e_row][e_col]) then
local enemy = self.enemies[e_row][e_col]
local e_projectile = Projectile(e_projectile_pic, e_projectile_sound, enemy.x + enemy.width / 2 - PROJECTILES_WIDTH / 2, enemy.y + enemy.height - PROJECTILES_HEIGHT)
e_projectile.speed = ENEMIES_PROJECTILES_SPEED
table.insert(self.e_projectiles, e_projectile)
playSound(e_projectile_sound)
self.e_projectiles_wait = math.random(1, (3 - self.e_projectiles_dec))
end
end
-- Move enemies' projectiles, if any
for i,p in ipairs(self.e_projectiles) do
p:update(dt, 1)
-- Check if enemy's projectile collided with player
if (p:collision(self.player, 1)) then
table.remove(self.e_projectiles, i)
self.player:hit(-1)
self.game_over = self.player:checkDeath()
end
end
-- Clear enemies' projectiles (avoiding memory leaks)
for p=#self.e_projectiles,1,-1 do
if (self.e_projectiles[p].y > WINDOW_HEIGHT) then
table.remove(self.e_projectiles, p)
end
end
end
function Game:explosionsUpdate(dt)
for i=#self.explosions,1,-1 do
self.explosions[i]:update(dt)
if (self.explosions[i]:done()) then
table.remove(self.explosions, i)
end
end
end
-- DRAW FUNCTIONS --
function Game:playerDraw()
self.player:healthDraw()
self.player:draw()
end
function Game:enemiesDraw()
for r,v in ipairs(self.enemies) do
for c,enemy in ipairs(v) do
if (self.enemies[r][c] ~= 0) then
enemy:draw()
end
end
end
end
-- GENERATE ENTITIES --
function Game:generateExplosion(x, y)
table.insert(self.explosions, Explosion(explosion_pic, x, y))
playSound(explosion_sound)
end
-- GAME LOSS --
function Game:gameOver(dt)
if (gameover.status == "lose") then
if (not self.player_dead) then
self.player.visibility = false
self:generateExplosion(self.player.x, self.player.y)
self.player_dead = true
end
if (self.game_over_timer >= self.game_over_wait) then
current_page = "Game Over"
else
self.game_over_timer = self.game_over_timer + dt
end
else
current_page = "Game Over"
end
end