extract a couple of collision functions to a separate module for reuse

This commit is contained in:
2025-02-08 08:39:04 +00:00
parent f9fe009d59
commit 1c105193e2
3 changed files with 26 additions and 18 deletions

15
fcore/collision.sml Normal file
View File

@@ -0,0 +1,15 @@
structure Collision =
struct
fun isColliding (ix, iy, ifx, ify, cx, cy, cfx, cfy) =
ix < cfx andalso ifx > cx andalso iy < cfy andalso ify > cy
fun isCollidingPlus (ix, iy, iw, ih, cx, cy, cw, ch) =
let
val ifx = ix + iw
val ify = iy + ih
val cfx = cx + cw
val cfy = cy + ch
in
isColliding (ix, iy, ifx, ify, cx, cy, cfx, cfy)
end
end

View File

@@ -1,20 +1,6 @@
structure EnemyBehaviour =
struct
open GameType
fun isColliding (ix, iy, ifx, ify, cx, cy, cfx, cfy) =
ix < cfx andalso ifx > cx andalso iy < cfy andalso ify > cy
fun isCollidingPlus (ix, iy, iw, ih, cx, cy, cw, ch) =
let
val ifx = ix + iw
val ify = iy + ih
val cfx = cx + cw
val cfy = cy + ch
in
isColliding (ix, iy, ifx, ify, cx, cy, cfx, cfy)
end
(* if player is attacking, does enemy collide with attack? *)
fun isCollidingWithPlayerAttack (player: player, enemy: enemy) =
let
@@ -28,12 +14,18 @@ struct
MAIN_ATTACKING {length, ...} =>
(case facing of
FACING_RIGHT =>
let val px = px + pSize
in isCollidingPlus (px, py, length, pSize, ex, ey, eSize, eSize)
let
val px = px + pSize
in
Collision.isCollidingPlus
(px, py, length, pSize, ex, ey, eSize, eSize)
end
| FACING_LEFT =>
let val px = px - length
in isCollidingPlus (px, py, length, pSize, ex, ey, eSize, eSize)
let
val px = px - length
in
Collision.isCollidingPlus
(px, py, length, pSize, ex, ey, eSize, eSize)
end)
| _ => false
end

View File

@@ -2,6 +2,7 @@ $(SML_LIB)/basis/basis.mlb
(* fcore *)
fcore/constants.sml
fcore/collision.sml
fcore/quad-tree-type.sml
fcore/quad-tree-fold.sml