2025-01-29 07:34:48 +00:00
|
|
|
signature QUAD_FOLDER =
|
|
|
|
|
sig
|
|
|
|
|
type env
|
|
|
|
|
type state
|
2025-01-29 07:07:54 +00:00
|
|
|
|
2025-01-29 07:34:48 +00:00
|
|
|
val fold: int * env * state -> state
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
signature MAKE_QUAD_TREE_FOLD =
|
|
|
|
|
sig
|
|
|
|
|
structure Fn: QUAD_FOLDER
|
|
|
|
|
|
|
|
|
|
val foldRegion: int * int * int * int *
|
|
|
|
|
Fn.env * Fn.state * QuadTreeType.t
|
|
|
|
|
-> Fn.state
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
functor MakeQuadTreeFold(Fn: QUAD_FOLDER): MAKE_QUAD_TREE_FOLD =
|
|
|
|
|
struct
|
|
|
|
|
structure Fn = Fn
|
|
|
|
|
|
|
|
|
|
open QuadTreeType
|
|
|
|
|
|
|
|
|
|
fun foldRegionVec (rx, ry, rw, rh, env, state, pos, elements) =
|
|
|
|
|
if pos = Vector.length elements then
|
|
|
|
|
state
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val item = Vector.sub (elements, pos)
|
|
|
|
|
val state =
|
|
|
|
|
if isCollidingItem (rx, ry, rw, rh, ~1, item) then
|
|
|
|
|
Fn.fold (#itemID item, env, state)
|
|
|
|
|
else
|
|
|
|
|
state
|
|
|
|
|
in
|
2025-01-29 19:12:24 +00:00
|
|
|
foldRegionVec (rx, ry, rw, rh, env, state, pos + 1, elements)
|
2025-01-29 07:34:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun foldRegion (rx, ry, rw, rh, env, state, tree) =
|
|
|
|
|
case tree of
|
2025-01-31 06:24:51 +00:00
|
|
|
NODE {nodes, x, y, w, h} =>
|
2025-01-29 07:34:48 +00:00
|
|
|
if isCollidingPlus (rx, ry, rw, rh, x, y, w, h) then
|
|
|
|
|
let
|
2025-01-31 06:24:51 +00:00
|
|
|
val state =
|
|
|
|
|
foldRegion (rx, ry, rw, rh, env, state, Vector.sub (nodes, tlIdx))
|
|
|
|
|
val state =
|
|
|
|
|
foldRegion (rx, ry, rw, rh, env, state, Vector.sub (nodes, trIdx))
|
|
|
|
|
val state =
|
|
|
|
|
foldRegion (rx, ry, rw, rh, env, state, Vector.sub (nodes, blIdx))
|
2025-01-29 07:34:48 +00:00
|
|
|
in
|
2025-01-31 06:24:51 +00:00
|
|
|
foldRegion (rx, ry, rw, rh, env, state, Vector.sub (nodes, brIdx))
|
2025-01-29 07:34:48 +00:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
state
|
|
|
|
|
| LEAF {items, x, y, w, h} =>
|
|
|
|
|
if isCollidingPlus (rx, ry, rw, rh, x, y, w, h) then
|
|
|
|
|
foldRegionVec (rx, ry, rw, rh, env, state, 0, items)
|
|
|
|
|
else
|
|
|
|
|
state
|
|
|
|
|
end
|