-
Notifications
You must be signed in to change notification settings - Fork 10
/
game.lua
305 lines (200 loc) · 7.98 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
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
game = class('game')
function game:initialize()
self.screen = {}
self.screen.width = 800
self.screen.height = 600
self.audio = audio:new()
self.missiles = {}
self.bombs = {}
self.explosions = {}
self.cursor = cursor:new(350,350)
self.bombtower = {}
self.bombtower.x = 400
self.bombtower.y = 500
self.current_level = 1
self.level = level:new(self.current_level)
self.score = score:new()
self.cities = self:buildCities()
self.audio:play('start_level')
self.game_over = false
cron = require 'cron'
cron.every(self.level.missile_interval,launchMissiles)
imgfont = love.graphics.newImageFont('gfx/imgfont2.png'," ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
love.graphics.setFont(imgfont)
end
function game:update(dt)
if self.level.num_missiles == self.level.launched_missiles and table.getn(self.missiles) == 0 and self.level.num_missiles > 0 and not self.game_over then
self.score:add(self.level.num_bombs * 5) -- extra bomb bonus
if table.getn(self.cities) > 0 and self.current_level < 9 then
self:advanceLevel()
else
self:gameOver()
end
end
cron.update(dt)
for k,e in pairs(self.explosions) do
-- check for exploded missiles
for k,missile in pairs(self.missiles) do
if e.shape:testPoint(0,0,0,missile.body:getX(),missile.body:getY()) then
local exp = explosion:new(world,missile.body:getX(),missile.body:getY() - 15)
table.insert(self.explosions,exp)
missile.body:destroy()
self.audio:play('boom')
table.remove(self.missiles,k)
self.level.destroyed_missiles = self.level.destroyed_missiles + 1
self.score:add(25)
end
end
if not e:update() then
table.remove(self.explosions,k)
end
end
for ck,city in pairs(self.cities) do
for k,missile in pairs(self.missiles) do
if city.shape:testPoint(0,0,0,missile.body:getX(),missile.body:getY()) then
local e = explosion:new(world,missile.body:getX(),missile.body:getY() - 10)
table.insert(self.explosions,e)
missile.body:destroy()
self.audio:play('boom')
table.remove(self.missiles,k)
self.level.destroyed_missiles = self.level.destroyed_missiles + 1
city.body:destroy()
table.remove(self.cities,ck)
break
end
end
end
for k,missile in pairs(self.missiles) do
if missile.body:getY() > 525 then
missile.body:destroy()
table.remove(self.missiles,k)
self.level.destroyed_missiles = self.level.destroyed_missiles + 1
end
end
for k,b in pairs(self.bombs) do
if self:testCollision(b.body,b.xtarget,b.ytarget) then
local e = explosion:new(world,b.xtarget,b.ytarget)
table.insert(self.explosions,e)
b.body:destroy()
table.remove(self.bombs,k)
end
end
if love.keyboard.isDown('up') and self.cursor.y > 0 then
self.cursor.y = self.cursor.y - 8
elseif self.cursor.y < 0 then
self.cursor.y = 0
end
if love.keyboard.isDown('right') and self.cursor.x < self.screen.width then
self.cursor.x = self.cursor.x + 8
elseif self.cursor.x > self.screen.width then
self.cursor.x = self.screen.width
end
if love.keyboard.isDown('down') and self.cursor.y < (self.bombtower.y - self.cursor.height) then
self.cursor.y = self.cursor.y + self.cursor.height
elseif self.cursor.y > self.bombtower.y then
self.cursor.y = self.bombtower.y
end
if love.keyboard.isDown('left') and self.cursor.x > 0 then
self.cursor.x = self.cursor.x - 8
elseif self.cursor.x < 0 then
self.cursor.x = 0
end
end
function game:draw()
love.graphics.setBackgroundColor(self.level.background_color)
for k,b in pairs(self.bombs) do
b:draw()
end
for k,m in pairs(self.missiles) do
m:draw(self.level.missile_tail_color,self.level.missile_color)
end
for k,c in pairs(self.cities) do
c:draw(self.level.city_color)
end
for k,e in pairs(self.explosions) do
e:draw()
end
self.cursor:draw()
self.score:draw()
self:drawInanimateObjects()
end
function game:launchMissile()
if self.level.num_missiles > self.level.launched_missiles and not self.audio.start:isPlaying() then
local xcoords = {112,115,110,120,105,700,176,247,554,625,696,400,683}
local index = math.random(1,13)
local xcoord = xcoords[index]
local ycoord = 35
local m = missile:new(world,xcoord,ycoord,self.level.missile_speed)
table.insert(self.missiles,m)
self.level.launched_missiles = self.level.launched_missiles + 1
end
end
function game:shoot()
if self.level.num_bombs > 0 then
local b = bomb:new(world,self.cursor.x,self.cursor.y)
table.insert(self.bombs,b)
self.level.num_bombs = self.level.num_bombs - 1
self.audio:play('launch')
else
self.audio:play('no_ammo')
end
end
function game:testCollision(body,x,y)
local vx1 = body:getX() - game.bombtower.x
local vy1 = body:getY() - game.bombtower.y
local vx2 = x - game.bombtower.x
local vy2 = y - game.bombtower.y
if vx1 - vx2 < 4 and vy1 - vy2 < 4 then
return true
end
return false
end
function game:gameOver()
self.game_over = true
self.audio:play('game_over')
end
function game:startOver()
self.audio:play('start_level')
self.current_level = 1
self.cities = self:buildCities()
self.game_over = false
self.score.total = 0
self.level = level:new(self.current_level)
cron.reset()
cron.every(self.level.missile_interval,launchMissiles)
end
function game:advanceLevel()
if self.current_level < 9 then
self.audio:play('start_level')
self.current_level = self.current_level + 1
self.level = level:new(self.current_level)
cron.reset()
cron.every(self.level.missile_interval,launchMissiles)
else
self:gameOver()
end
end
function game:drawInanimateObjects()
-- ground
love.graphics.setColor(self.level.ground_color)
love.graphics.rectangle('fill',0 ,525,self.screen.width,75)
love.graphics.rectangle('fill',0,500,50,25)
love.graphics.rectangle('fill',750,500,50,25)
love.graphics.rectangle('fill',300,515,200,10)
love.graphics.rectangle('fill',325,505,150,10)
love.graphics.rectangle('fill',350,495,100,10)
-- bomb
if self.level.num_bombs > 0 then
love.graphics.setColor(math.random(0,255),math.random(0,255),math.random(0,255))
love.graphics.rectangle('fill',self.bombtower.x,self.bombtower.y - 8,8,4)
end
end
function game:buildCities()
return {city:new(105,513),city:new(176,513),city:new(247,513),city:new(554,513),city:new(625,513),city:new(696,513)}
end
function launchMissiles()
local num_missiles = math.random(1,game.level.max_concurrent_missiles)
for i=1, num_missiles do
game:launchMissile()
end
end