Files
sml-projects/game-sml/fcore/level/platform.sml
Humza Shahid da8790f0b6 Add 'game-sml/' from commit '113c3e67abe635f714f972a1e2ab0e4b24ff10f4'
git-subtree-dir: game-sml
git-subtree-mainline: aa5357714d
git-subtree-split: 113c3e67ab
2026-04-24 00:38:14 +01:00

119 lines
3.4 KiB
Standard ML

structure Platform =
struct
type t = {id: int, x: int, y: int, width: int}
(* collision height of a platform.
* Visual height may (and probably will) be different. *)
val platHeight = 5
val rPlatHeight = 5.0
fun helpGenerateTree (pos, platVec, acc) =
if pos = Vector.length platVec then
acc
else
let
val {id, x, y, width} = Vector.sub (platVec, pos)
val acc = QuadTree.insert (x, y, width, platHeight, id, acc)
in
helpGenerateTree (pos + 1, platVec, acc)
end
fun generateTree platVec =
helpGenerateTree
( 0
, platVec
, QuadTree.create (Constants.worldWidth, Constants.worldHeight)
)
fun helpFindPos (findNum, vec, low, high) =
let
val mid = low + ((high - low) div 2)
val platform = Vector.sub (vec, mid)
val {id = curNum, x = _, y = _, width = _} = platform
in
if curNum = findNum then mid
else if curNum < findNum then helpFindPos (findNum, vec, mid + 1, high)
else helpFindPos (findNum, vec, low, mid - 1)
end
fun findPos (findNum, vec) =
helpFindPos (findNum, vec, 0, Vector.length vec - 1)
fun find (findNum, vec) =
let val pos = findPos (findNum, vec)
in Vector.sub (vec, pos)
end
fun helpGetDrawVecWider
(pos, platVec, acc, winWidth, winHeight, ratio, yOffset) =
if pos = Vector.length platVec then
Vector.concat acc
else
let
val plat = Vector.sub (platVec, pos)
val {x, y, width, id = _} = plat
val x = Real32.fromInt x * ratio
val y = Real32.fromInt y * ratio + yOffset
val width = Real32.fromInt width * ratio
val height = rPlatHeight * ratio
val block = Block.lerp
(x, y, width, height, winWidth, winHeight, 0.0, 0.0, 0.0)
val acc = block :: acc
in
helpGetDrawVecWider
(pos + 1, platVec, acc, winWidth, winHeight, ratio, yOffset)
end
fun helpGetDrawVecTaller
(pos, platVec, acc, winWidth, winHeight, ratio, xOffset) =
if pos = Vector.length platVec then
Vector.concat acc
else
let
val plat = Vector.sub (platVec, pos)
val {x, y, width, id = _} = plat
val x = Real32.fromInt x * ratio + xOffset
val y = Real32.fromInt y * ratio
val width = Real32.fromInt width * ratio
val height = rPlatHeight * ratio
val block = Block.lerp
(x, y, width, height, winWidth, winHeight, 0.0, 0.0, 0.0)
val acc = block :: acc
in
helpGetDrawVecTaller
(pos + 1, platVec, acc, winWidth, winHeight, ratio, xOffset)
end
fun getDrawVec (platVec, width, height) =
let
val wratio = width / 1920.0
val hratio = height / 1080.0
in
if wratio < hratio then
let
val scale = 1080.0 * wratio
val yOffset =
if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0
else 0.0
in
helpGetDrawVecWider (0, platVec, [], width, height, wratio, yOffset)
end
else
let
val scale = 1920.0 * hratio
val xOffset =
if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0
else 0.0
in
helpGetDrawVecTaller (0, platVec, [], width, height, hratio, xOffset)
end
end
end