summaryrefslogtreecommitdiff
path: root/collisions.lua
diff options
context:
space:
mode:
Diffstat (limited to 'collisions.lua')
-rw-r--r--collisions.lua34
1 files changed, 23 insertions, 11 deletions
diff --git a/collisions.lua b/collisions.lua
index f57554e..3d3ba46 100644
--- a/collisions.lua
+++ b/collisions.lua
@@ -1,10 +1,22 @@
function is_colliding(a, b)
- ax1, bx1 = a.position.x, b.position.x
- ax2, bx2 = ax1 + SPRITE_DIMS.x, bx1 + SPRITE_DIMS.y
- ay1, by1 = a.position.y, b.position.y
- ay2, by2 = ay1 + SPRITE_DIMS.y, by1 + SPRITE_DIMS.x
- return (ax1 < bx2 and ax2 > bx1
- and ay1 < by2 and ay2 > by1)
+ 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)
@@ -12,10 +24,7 @@ function handle_collision(a, b)
b:equip(a)
return
end
- if a.damage and b.health then
- if b:is_in_iframe() then
- return
- end
+ if hurts(a, b) then
b:take_damage(a.line_of_sight, a.damage)
end
end
@@ -27,7 +36,10 @@ function run_collisions()
if a.id == b.id then
goto continue
end
- if is_colliding(a, b) then
+ 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::