Chopper Command: Adding the Bombs

Time to give the baddies some teeth. I had to change a few things to support the bombs. I made the bomb class hold the reference to two objects, which are points basically, rendered to screen with the same skin.

I’ll use these same points later when running collision checks.

bomb.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
 
local Bomb = {}
 
 
 
local GameSprite = require("gamesprite")
 
local constants = require ("constants")
 
local Rectangle = require ("rectangle")
 
local Point = require ("point")
 
 
 
 
 
local RANGE_Y = 80
 
local RANGE_X = 150
 
 
 
function Bomb.new ( gameModel, gameTile )	
 
 
 
	local sprite = {}
 
 
 
	GameSprite.decorate(sprite, gameModel, gameTile)
 
	sprite.active = false
 
	sprite.squaredRadius = 16
 
    sprite.skin = love.graphics.newImage ( "sprites/bomb.png" )
 
    sprite.width = sprite.skin:getWidth()
 
	sprite.height = sprite.skin:getHeight()
 
 
 
	sprite._timeForBombBreak = 0
 
	sprite._intervalForBombBreak = 10 + math.floor( math.random() * 30 )
 
	sprite._vector = Point.new(0,0)
 
	sprite._speed = 4
 
	sprite._spritesPositions = {Point.new(0,0), Point.new(0,0)}
 
	sprite._spritesVisible = { false, false } 
 
 
 
    function sprite:reset (type)
 
 
 
		self.visible = false
 
		self.active = false
 
		self._timeForBombBreak = 0
 
		self._intervalForBombBreak = 10 + math.floor( math.random() * 30 )
 
	end
 
 
 
 
 
	function sprite:update (dt)
 
 
 
		if self.active == true then
 
 
 
			if self._timeForBombBreak >= 0 then 
 
				self._spritesVisible[1] = true
 
				self._timeForBombBreak = self._timeForBombBreak + 1
 
			end
 
 
 
			if self._timeForBombBreak > self._intervalForBombBreak then
 
 
 
				self._timeForBombBreak = -1
 
				self._spritesVisible[2] = true
 
				self._vector.x = 0
 
				self._vector.y = self._speed * 2
 
 
 
			elseif self._timeForBombBreak > 0 then
 
 
 
				self._spritesPositions[2].x = self._spritesPositions[1].x
 
				self._spritesPositions[2].y = self._spritesPositions[1].y
 
 
 
			end
 
 
 
			self._spritesPositions[1].x = self._spritesPositions[1].x + self._vector.x
 
			self._spritesPositions[1].y = self._spritesPositions[1].y + self._vector.y
 
 
 
 
 
			if self._spritesVisible[2] == true then
 
				self._spritesPositions[2].x = self._spritesPositions[1].x
 
				self._spritesPositions[2].y = self._spritesPositions[2].y - 1 * self._vector.y
 
			end
 
 
 
 
 
			if self._spritesPositions[1].x > constants.GAME_TILE_WIDTH then  
 
				self._spritesVisible[1] = false 
 
			end
 
 
 
			if self._spritesPositions[1].y > constants.GAME_TILE_TOTAL_HEIGHT then 
 
				self._spritesVisible[1] = false 
 
			end
 
 
 
			if self._spritesPositions[1].y < 0 then 
 
				self._spritesVisible[1] = false 
 
			end
 
 
 
			if self._spritesPositions[1].x < 0 then 
 
				self._spritesVisible[1] = false 
 
			end
 
 
 
			if self._spritesPositions[2].x > constants.GAME_TILE_WIDTH then 
 
				self._spritesVisible[2] = false 
 
			end
 
 
 
			if self._spritesPositions[2].y > constants.GAME_TILE_TOTAL_HEIGHT then 
 
				self._spritesVisible[2] = false 
 
			end
 
 
 
			if self._spritesPositions[2].y < 0 then 
 
			self._spritesVisible[2] = false 
 
			end
 
 
 
			if self._spritesPositions[2].x < 0 then 
 
				self._spritesVisible[2] = false 
 
			end
 
 
 
			if self._spritesVisible[1] == false and self._spritesVisible[2] == false then self.active = false end
 
 
 
		end	
 
	end
 
 
 
	function sprite:hit ()
 
 
 
		self.active = false
 
 
 
	end
 
 
 
	function sprite:drop ( shooter )
 
 
 
		if self.active == true then return end
 
 
 
		self._intervalForBombBreak = 10 + math.floor( math.random() * 30 )
 
		self._timeForBombBreak = 0
 
 
 
		local sign = shooter.scaleX * -1
 
 
 
		self._spritesVisible[1] = true
 
		self._spritesPositions[1].x = shooter.x + shooter.width * 0.5 * sign
 
		self._spritesPositions[1].y = shooter.y
 
 
 
		self._spritesPositions[2].x = shooter.x + shooter.width * 0.5 * sign
 
		self._spritesPositions[2].y = shooter.y
 
 
 
 
 
		local targetPosition = Point.new (0, 0)
 
		self._vector = Point.new (0, 0)
 
 
 
		if math.random() > 0.6 then
 
 
 
			targetPosition = Point.new (shooter.x + sign * math.random() * RANGE_X,
 
										shooter.y + math.random() * RANGE_Y)
 
			local angle = math.atan2(targetPosition.y - shooter.y,
 
										 targetPosition.x - shooter.x)
 
			self._vector.x = math.cos(angle) * self._speed
 
			self._vector.y = math.sin(angle) * self._speed
 
 
 
		end
 
 
 
		self.active = true
 
	end
 
 
 
 
 
 
 
	function sprite:draw() 
 
 
 
		if self.active == false then return end
 
 
 
 
 
		local sprite1Visible = self._spritesVisible[1]
 
		local sprite2Visible = self._spritesVisible[2]
 
 
 
		if sprite1Visible == false and sprite2Visible == false then
 
			return 
 
		end
 
 
 
		local sprite1Pos = self._spritesPositions[1]
 
		local sprite2Pos = self._spritesPositions[2]
 
 
 
		love.graphics.draw(self.skin, sprite1Pos.x, sprite1Pos.y, self.rotation, self.scaleX, self.scaleY, self.width * 0.5, self.height * 0.5)
 
		love.graphics.draw(self.skin, sprite2Pos.x, sprite2Pos.y, self.rotation, self.scaleX, self.scaleY, self.width * 0.5, self.height * 0.5)
 
 
 
	end
 
 
 
	sprite:reset(constants.RESET_HARD)
 
 
 
	return sprite
 
end
 
 
 
return Bomb

I overrode the draw method here, so as to draw two sprites.

And I added an extraDraw method to gamesprite.lua that looks like this:

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
 
function sprite:extraDraw()
 
end
 
 
 
function sprite:draw() 
 
 
 
	if self.visible == false or self.alpha == 0 then
 
		return 
 
	end
 
 
 
	if self.alpha < 1 then
 
		love.graphics.setColor(255,255,255, self.alpha * 255)
 
	end
 
 
 
	love.graphics.draw(self.skin, self.x, self.y, self.rotation, self.scaleX, self.scaleY, self.width * 0.5, self.height * 0.5)
 
 
 
	if self.alpha < 1 then
 
		love.graphics.setColor(255,255,255,255)
 
	end
 
 
 
	self.extraDraw()
 
 
 
end

Now, whenever I have to draw something else, which breaks the default draw method, I can transfer that extra bit of drawing to this method. So now I made enemy.lua implement that, calling the draw method inside the bomb.

1
2
3
4
5
6
 
function sprite:extraDraw ()
 
		sprite._bomb:draw()
 
end

The new enemy.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
 
local Enemy = {}
 
 
 
local GameSprite = require("gamesprite")
 
local constants = require ("constants")
 
local Rectangle = require ("rectangle")
 
local Point = require ("point")
 
local Bomb = require ("bomb")
 
 
 
 
 
function Enemy.new (boundaries, gameModel, gameTile)
 
 
 
	local sprite = {}
 
 
 
	GameSprite.decorate(sprite, gameModel)
 
 
 
	sprite._tile = gameTile
 
	sprite._nextPosition = Point.new(0,0)
 
	sprite._targetPosition = Point.new(0,0)
 
	sprite._boundaries = boundaries
 
	sprite._type = 0
 
	sprite._bomb = Bomb.new ( gameModel, gameTile )
 
	sprite._timeBetweenBombs = 0
 
	sprite._intervalBetweenBombs = 0
 
	sprite._timerBetweenShifts = 0
 
	sprite._intervalBetweenShifts = 0
 
	sprite._boom = love.graphics.newImage ( "sprites/boom1.png" )
 
	sprite._dying = false
 
 
 
 
 
	sprite.halfWidth = sprite.width * 0.5
 
	sprite.halfHeight = sprite.height * 0.5
 
	sprite.squaredRadius = sprite.halfWidth * sprite.halfWidth
 
 
 
 
 
 
 
	function sprite:reset (type)
 
 
 
		if type == constants.RESET_HARD then
 
 
 
			self.alpha = 1
 
			self.visible = true
 
			self.active = true
 
			self._dying = false
 
 
 
			constants.ENEMY_TYPES_INDEX = constants.ENEMY_TYPES_INDEX % #constants.ENEMY_TYPES
 
			constants.ENEMY_TYPES_INDEX = constants.ENEMY_TYPES_INDEX + 1
 
 
 
			self._type = constants.ENEMY_TYPES[constants.ENEMY_TYPES_INDEX]
 
 
 
			if self._type == constants.ENEMY_TYPE_AIRPLANE then
 
				self.skin = love.graphics.newImage ( "sprites/jet.png" )
 
			else
 
				local frame1 = love.graphics.newImage ( "sprites/heli1.png" )
 
				local frame2 = love.graphics.newImage ( "sprites/heli2.png" )
 
 
 
			    self.animationFrames = { frame1, frame2 }
 
			    self.skin = sprite.animationFrames[1]
 
			end
 
 
 
			self.width = self.skin:getWidth()
 
			self.height = self.skin:getHeight()
 
 
 
			self.x = self._boundaries.x + math.random() * self._boundaries.width
 
			self.y = self._boundaries.y + math.random() * self._boundaries.height
 
 
 
			self._nextPosition =  Point.new(self.x, self.y)
 
 
 
			self._timeBetweenBombs = 50 + math.random() * 100
 
			self._intervalBetweenBombs = 50 + math.random() * 200
 
 
 
			self._intervalBetweenShifts = 100 + math.random() * 100
 
			self._timerBetweenShifts = 0
 
 
 
			self:pickTargetPosition()
 
 
 
		end
 
 
 
		self._bomb:reset(type)
 
 
 
	end
 
 
 
	function sprite:pickTargetPosition ()
 
 
 
		if self.x < self._boundaries.x + self._boundaries.width * 0.5 then
 
			self._targetPosition.x = self._boundaries.x + self._boundaries.width
 
		else
 
			self._targetPosition.x = self._boundaries.x
 
		end
 
 
 
		self._targetPosition.y = self._boundaries.y + math.random() * self._boundaries.height
 
 
 
		if self._targetPosition.x < self._nextPosition.x then
 
			self.scaleX = -1
 
		elseif self._targetPosition.x > self._nextPosition.x then
 
			self.scaleX = 1
 
		end
 
 
 
	end
 
 
 
	function sprite:hit ()
 
 
 
		self.active = false
 
		self.skin = self._boom
 
		self._dying = true
 
 
 
	end
 
 
 
	function sprite:update (dt)
 
 
 
		self._bomb:update (dt)
 
 
 
		if self.visible == false then return end
 
 
 
		if self.active == false then
 
			self.alpha = self.alpha - 0.05
 
			if self.alpha  <= 0 then
 
				self.visible = false
 
			end
 
			return
 
		end
 
 
 
		self:animate(dt)
 
 
 
		self._timerBetweenShifts = self._timerBetweenShifts + 1
 
 
 
		if self._timerBetweenShifts > self._intervalBetweenShifts then
 
 
 
			self._timerBetweenShifts = 0
 
			self._intervalBetweenShifts = 100 + math.random() * 100
 
			self:pickTargetPosition ()
 
		end
 
 
 
		if self._type == constants.ENEMY_TYPE_HELICOPTER then
 
			if math.random() < 0.02 then
 
				self._targetPosition.y = self._boundaries.y + math.random() * self._boundaries.height
 
			end
 
		else
 
			if math.random() < 0.01 then
 
				self._targetPosition.y = self._boundaries.y + math.random() * self._boundaries.height
 
			end
 
		end
 
 
 
		if self._nextPosition.x + self.gameModel.enemy_speed < self._targetPosition.x then
 
			self._nextPosition.x =  self._nextPosition.x + self.gameModel.enemy_speed
 
		end
 
 
 
		if self._nextPosition.x - self.gameModel.enemy_speed > self._targetPosition.x then
 
			self._nextPosition.x = self._nextPosition.x - self.gameModel.enemy_speed
 
		end
 
 
 
		if self._nextPosition.y + self.gameModel.enemy_speed < self._targetPosition.y then
 
			self._nextPosition.y = self._nextPosition.y + self.gameModel.enemy_speed
 
		end
 
 
 
		if self._nextPosition.y - self.gameModel.enemy_speed > self._targetPosition.y then
 
			self._nextPosition.y =  self._nextPosition.y - self.gameModel.enemy_speed
 
		end
 
 
 
		if self._nextPosition.y < self._boundaries.top then
 
			self._nextPosition.y = self._boundaries.top
 
		end
 
		if self._nextPosition.y > self._boundaries.bottom then
 
			self._nextPosition.y = self._boundaries.bottom
 
		end
 
		if self._nextPosition.x - self.halfWidth < self._boundaries.left then
 
			self._nextPosition.x = self._boundaries.left + self.halfWidth
 
		end
 
		if self._nextPosition.x + self.halfWidth > self._boundaries.right then
 
			self._nextPosition.x = self._boundaries.right - self.halfWidth
 
		end
 
 
 
 
 
		if math.pow(self._nextPosition.x - self._targetPosition.x, 2) + math.pow (self._nextPosition.y - self._targetPosition.y, 2) <=
 
		self.squaredRadius then
 
			self:pickTargetPosition()
 
		end
 
 
 
		self.x = self._nextPosition.x
 
		self.y = self._nextPosition.y
 
 
 
		if self._bomb.active == false then
 
 
 
			self._timeBetweenBombs = self._timeBetweenBombs + 1
 
 
 
			if self._timeBetweenBombs > self._intervalBetweenBombs then
 
 
 
				self._intervalBetweenBombs = 50 + math.floor(math.random () * 200)
 
				self._bomb:drop( self )
 
 
 
			end
 
 
 
		end
 
 
 
	end
 
 
 
	function sprite:extraDraw ()
 
 
 
		sprite._bomb:draw()
 
	end
 
 
 
	sprite:reset (constants.RESET_HARD)
 
 
 
	return sprite
 
end
 
 
 
return Enemy

Nothing else needs to be added to main.lua