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
|
function is_colliding(a, b)
return (
a.top_left.x < b.bottom_right.x and a.bottom_right.x > b.top_left.x
and a.top_left.y < b.bottom_right.y and a.bottom_right.y > b.top_left.y
)
end
_hurts = {
vec2(Entities.Bullet, Entities.Player),
vec2(Entities.Sword, Entities.Enemy),
}
function hurts(a, b)
if b:is_in_iframe() then
return false
end
if not (a.damage and b.health) then
return false
end
return some(_hurts, function (v) return a.entity_type == v.x and b.entity_type == v.y end)
end
function handle_collision(a, b)
if a.entity_type == Entities.Sword and b.entity_type == Entities.Player and b.equipped[a.id] == nil then
b:equip(a)
return
end
if hurts(a, b) then
b:take_damage(a.line_of_sight, a.damage)
end
end
function run_collisions()
collidable = filter(_World, function(e) return e.collision end)
for _ai, a in pairs(collidable) do
for _bi, b in pairs(collidable) do
if a.id == b.id then
goto continue
end
if is_colliding(
{top_left = (a.collision_bounds.top_left + a.position), bottom_right = (a.collision_bounds.bottom_right + a.position) },
{top_left = (b.collision_bounds.top_left + b.position), bottom_right = (b.collision_bounds.bottom_right + b.position) }
) then
handle_collision(a, b)
end
::continue::
end
end
end
|