add platform vector and platform quadtree to game type

This commit is contained in:
2024-12-17 21:42:06 +00:00
parent 507c1c331c
commit ddd58fd534
4 changed files with 132 additions and 7 deletions

View File

@@ -2,6 +2,8 @@ signature GAME_TYPE =
sig
type wall = {id: int, x: int, y: int, width: int, height: int}
type platform = {id: int, x: int, y: int, width: int}
datatype player_y_axis =
ON_GROUND
| FALLING
@@ -19,7 +21,13 @@ sig
, jumpPressed: bool
}
type game_type = {player: player, walls: wall vector, wallTree: QuadTree.t}
type game_type =
{ player: player
, walls: wall vector
, wallTree: QuadTree.t
, platforms: platform vector
, platformTree: QuadTree.t
}
val initial: game_type
end
@@ -28,6 +36,9 @@ structure GameType :> GAME_TYPE =
struct
type wall = {id: int, x: int, y: int, width: int, height: int}
(* all platforms have a fixed visual height and a fixed collision height *)
type platform = {id: int, x: int, y: int, width: int}
datatype player_y_axis =
ON_GROUND
| FALLING
@@ -45,7 +56,13 @@ struct
, jumpPressed: bool
}
type game_type = {player: player, walls: wall vector, wallTree: QuadTree.t}
type game_type =
{ player: player
, walls: wall vector
, wallTree: QuadTree.t
, platforms: platform vector
, platformTree: QuadTree.t
}
val initial: game_type =
let
@@ -61,10 +78,18 @@ struct
val wall1 = {id = 1, x = 0, y = 0, width = 100, height = 1080}
val wall2 = {id = 2, x = 1820, y = 0, width = 100, height = 1080}
val wall3 = {id = 3, x = 0, y = 980, width = 1920, height = 108}
val wall4 = {id = 4, x = 155, y = 911, width = 155, height = 55}
val walls = Vector.fromList [wall1, wall2, wall3, wall4]
val walls = Vector.fromList [wall1, wall2, wall3]
val wallTree = Wall.generateTree walls
val plat1 = {id = 1, x = 155, y = 911, width = 155}
val platforms = Vector.fromList [plat1]
val platformTree = Platform.generateTree platforms
in
{player = player, walls = walls, wallTree = wallTree}
{ player = player
, walls = walls
, wallTree = wallTree
, platforms = platforms
, platformTree = platformTree
}
end
end

View File

@@ -2,9 +2,14 @@ structure GameUpdate =
struct
fun update (game, input) =
let
val {player, walls, wallTree} = game
val {player, walls, wallTree, platforms, platformTree} = game
val player = Player.move (game, input)
in
{player = player, walls = walls, wallTree = wallTree}
{ player = player
, walls = walls
, wallTree = wallTree
, platforms = platforms
, platformTree = platformTree
}
end
end

94
fcore/platform.sml Normal file
View File

@@ -0,0 +1,94 @@
structure Platform =
struct
(* collision height of a platform.
* Visual height may (and probably will) be different. *)
val platHeight = 3
val rPlatHeight = 3.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, 0, 0, 1920, 1080, id, acc)
in
helpGenerateTree (pos + 1, platVec, acc)
end
fun generateTree platVec = helpGenerateTree (0, platVec, QuadTree.empty)
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

View File

@@ -10,6 +10,7 @@ in
end
fcore/wall.sml
fcore/platform.sml
fcore/game-type.sml
fcore/player.sml