diff --git a/fcore/game-type.sml b/fcore/game-type.sml index 0cf9032..f250866 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -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 diff --git a/fcore/game-update.sml b/fcore/game-update.sml index 70f786e..a9ccb75 100644 --- a/fcore/game-update.sml +++ b/fcore/game-update.sml @@ -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 diff --git a/fcore/platform.sml b/fcore/platform.sml new file mode 100644 index 0000000..b70717b --- /dev/null +++ b/fcore/platform.sml @@ -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 diff --git a/oms.mlb b/oms.mlb index acd9f56..100947f 100644 --- a/oms.mlb +++ b/oms.mlb @@ -10,6 +10,7 @@ in end fcore/wall.sml +fcore/platform.sml fcore/game-type.sml fcore/player.sml