diff options
| author | Elizabeth Hunt <me@liz.coffee> | 2026-05-17 11:53:35 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <me@liz.coffee> | 2026-05-17 11:54:05 -0700 |
| commit | 661156ce988131eb4896a7af4d00a42b520305e7 (patch) | |
| tree | 23b64b8f224f25aeb9c789a54da13f29c22b91c5 /collisions.lua | |
| parent | 7d415c4958f584c031922ed77a3559d15bf2dc1b (diff) | |
| download | dyl8-661156ce988131eb4896a7af4d00a42b520305e7.tar.gz dyl8-661156ce988131eb4896a7af4d00a42b520305e7.zip | |
This is REAL Artificial Intelligence and definitely REALLY COMPLICATED
Diffstat (limited to 'collisions.lua')
| -rw-r--r-- | collisions.lua | 34 |
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:: |
