extract a couple of collision functions to a separate module for reuse
This commit is contained in:
15
fcore/collision.sml
Normal file
15
fcore/collision.sml
Normal 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
|
||||||
@@ -1,20 +1,6 @@
|
|||||||
structure EnemyBehaviour =
|
structure EnemyBehaviour =
|
||||||
struct
|
struct
|
||||||
open GameType
|
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? *)
|
(* if player is attacking, does enemy collide with attack? *)
|
||||||
fun isCollidingWithPlayerAttack (player: player, enemy: enemy) =
|
fun isCollidingWithPlayerAttack (player: player, enemy: enemy) =
|
||||||
let
|
let
|
||||||
@@ -28,12 +14,18 @@ struct
|
|||||||
MAIN_ATTACKING {length, ...} =>
|
MAIN_ATTACKING {length, ...} =>
|
||||||
(case facing of
|
(case facing of
|
||||||
FACING_RIGHT =>
|
FACING_RIGHT =>
|
||||||
let val px = px + pSize
|
let
|
||||||
in isCollidingPlus (px, py, length, pSize, ex, ey, eSize, eSize)
|
val px = px + pSize
|
||||||
|
in
|
||||||
|
Collision.isCollidingPlus
|
||||||
|
(px, py, length, pSize, ex, ey, eSize, eSize)
|
||||||
end
|
end
|
||||||
| FACING_LEFT =>
|
| FACING_LEFT =>
|
||||||
let val px = px - length
|
let
|
||||||
in isCollidingPlus (px, py, length, pSize, ex, ey, eSize, eSize)
|
val px = px - length
|
||||||
|
in
|
||||||
|
Collision.isCollidingPlus
|
||||||
|
(px, py, length, pSize, ex, ey, eSize, eSize)
|
||||||
end)
|
end)
|
||||||
| _ => false
|
| _ => false
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user