-
Notifications
You must be signed in to change notification settings - Fork 1
/
collisions.lua
91 lines (89 loc) · 3.47 KB
/
collisions.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
local collisions = {}
function collisions.check_rectangles_overlap( a, b )
local overlap = false
if not( a.x + a.width < b.x or b.x + b.width < a.x or
a.y + a.height < b.y or b.y + b.height < a.y ) then
overlap = true
end
return overlap
end
function collisions.invaders_walls_collision( invaders, walls )
local overlap, wall
if invaders.current_speed_x > 0 then
wall, wall_type = walls.current_level_walls['right'], 'right'
else
wall, wall_type = walls.current_level_walls['left'], 'left'
end
local a = { x = wall.position_x,
y = wall.position_y,
width = wall.width,
height = wall.height }
for _, invader_row in pairs( invaders.current_level_invaders ) do
for _, invader in pairs( invader_row ) do
local b = { x = invader.position_x,
y = invader.position_y,
width = invader.width,
height = invader.height }
overlap = collisions.check_rectangles_overlap( a, b )
if overlap then
if wall_type == invaders.allow_overlap_direction then
invaders.current_speed_x = -invaders.current_speed_x
if invaders.allow_overlap_direction == 'right' then
invaders.allow_overlap_direction = 'left'
else
invaders.allow_overlap_direction = 'right'
end
invaders.descend_by_row()
end
end
end
end
end
function collisions.resolve_collisions( invaders, walls )
collisions.invaders_walls_collision( invaders, walls )
end
function collisions.invaders_bullets_collision( invaders, bullets )
local overlap
for b_i, bullet in pairs( bullets.current_level_bullets) do
local a = { x = bullet.position_x,
y = bullet.position_y,
width = bullet.width,
height = bullet.height }
for i_i, invader_row in pairs( invaders.current_level_invaders ) do
for i_j, invader in pairs( invader_row ) do
local b = { x = invader.position_x,
y = invader.position_y,
width = invader.width,
height = invader.height }
overlap = collisions.check_rectangles_overlap( a, b )
if overlap then
invaders.destroy_invader( i_i, i_j )
bullets.destroy_bullet( b_i )
end
end
end
end
end
function collisions.bullets_walls_collision( bullets, walls )
local overlap
local wall = walls.current_level_walls['top']
local a = { x = wall.position_x,
y = wall.position_y,
width = wall.width,
height = wall.height }
for b_i, bullet in pairs( bullets.current_level_bullets) do
local b = { x = bullet.position_x,
y = bullet.position_y,
width = bullet.width,
height = bullet.height }
overlap = collisions.check_rectangles_overlap( a, b )
if overlap then
bullets.destroy_bullet( b_i )
end
end
end
function collisions.resolve_collisions( invaders, walls, bullets )
collisions.invaders_bullets_collision( invaders, bullets )
collisions.bullets_walls_collision( bullets, walls )
end
return collisions