2024-12-09 04:37:40 +00:00
|
|
|
structure Player =
|
|
|
|
|
struct
|
2024-12-15 09:10:19 +00:00
|
|
|
open GameType
|
2024-12-09 04:37:40 +00:00
|
|
|
|
|
|
|
|
(* width/height *)
|
|
|
|
|
val size = 35
|
2024-12-13 22:48:34 +00:00
|
|
|
val realSize = 35.0
|
2024-12-09 04:37:40 +00:00
|
|
|
|
|
|
|
|
val moveBy = 5
|
2024-12-13 22:48:34 +00:00
|
|
|
val jumpLimit = 150
|
2024-12-14 18:11:59 +00:00
|
|
|
val floatLimit = 3
|
2024-12-09 04:37:40 +00:00
|
|
|
|
2024-12-14 21:05:51 +00:00
|
|
|
fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed) =
|
|
|
|
|
{ yAxis = yAxis
|
|
|
|
|
, xAxis = xAxis
|
|
|
|
|
, health = health
|
|
|
|
|
, x = x
|
|
|
|
|
, y = y
|
|
|
|
|
, jumpPressed = jumpPressed
|
|
|
|
|
}
|
2024-12-09 04:37:40 +00:00
|
|
|
|
2024-12-15 09:10:19 +00:00
|
|
|
fun checkWalls (yAxis, xAxis, x, y, health, jumpPressed, lst, game: game_type) =
|
2024-12-09 04:37:40 +00:00
|
|
|
let
|
2024-12-13 22:48:34 +00:00
|
|
|
open QuadTree
|
|
|
|
|
in
|
|
|
|
|
case lst of
|
|
|
|
|
(QUERY_ON_LEFT_SIDE, wallID) :: tl =>
|
|
|
|
|
let
|
2024-12-15 09:10:19 +00:00
|
|
|
val {walls, ...} = game
|
|
|
|
|
val {x = wallX, width = wallWidth, ...} =
|
|
|
|
|
Vector.sub (walls, wallID - 1)
|
|
|
|
|
|
2024-12-13 22:48:34 +00:00
|
|
|
val newX = wallX + wallWidth
|
|
|
|
|
in
|
2024-12-15 09:10:19 +00:00
|
|
|
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, tl, game)
|
2024-12-13 22:48:34 +00:00
|
|
|
end
|
|
|
|
|
| (QUERY_ON_RIGHT_SIDE, wallID) :: tl =>
|
|
|
|
|
let
|
2024-12-15 09:10:19 +00:00
|
|
|
val {walls, ...} = game
|
|
|
|
|
val {x = wallX, width = wallWidth, ...} =
|
|
|
|
|
Vector.sub (walls, wallID - 1)
|
|
|
|
|
|
2024-12-13 22:48:34 +00:00
|
|
|
val newX = wallX - size
|
|
|
|
|
in
|
2024-12-15 09:10:19 +00:00
|
|
|
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, tl, game)
|
2024-12-13 22:48:34 +00:00
|
|
|
end
|
|
|
|
|
| (QUERY_ON_BOTTOM_SIDE, wallID) :: tl =>
|
|
|
|
|
let
|
2024-12-15 09:10:19 +00:00
|
|
|
val {walls, ...} = game
|
|
|
|
|
val {y = wallY, ...} = Vector.sub (walls, wallID - 1)
|
|
|
|
|
|
2024-12-13 22:48:34 +00:00
|
|
|
val newY = wallY - size
|
|
|
|
|
in
|
2024-12-15 09:10:19 +00:00
|
|
|
checkWalls
|
|
|
|
|
(ON_GROUND, xAxis, x, newY, health, jumpPressed, tl, game)
|
2024-12-13 22:48:34 +00:00
|
|
|
end
|
|
|
|
|
| (QUERY_ON_TOP_SIDE, wallID) :: tl =>
|
2024-12-15 09:10:19 +00:00
|
|
|
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, tl, game)
|
2024-12-17 22:45:58 +00:00
|
|
|
| [] =>
|
|
|
|
|
mkPlayer (health, xAxis, yAxis, x, y, jumpPressed)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun helpCheckPlatforms
|
|
|
|
|
( yAxis, xAxis, x, y, health
|
|
|
|
|
, jumpPressed, platList, wallList, game
|
|
|
|
|
) =
|
|
|
|
|
let
|
|
|
|
|
open QuadTree
|
|
|
|
|
in
|
|
|
|
|
case platList of
|
|
|
|
|
platID :: tl =>
|
|
|
|
|
(case yAxis of
|
|
|
|
|
DROP_BELOW_PLATFORM =>
|
|
|
|
|
helpCheckPlatforms
|
|
|
|
|
(yAxis, xAxis, x, y, health, jumpPressed, tl, wallList, game)
|
|
|
|
|
| _ =>
|
|
|
|
|
let
|
|
|
|
|
val {platforms, ...} = game
|
|
|
|
|
val {y = platY, ...} = Vector.sub (platforms, platID - 1)
|
|
|
|
|
|
|
|
|
|
val newY = platY - size
|
|
|
|
|
in
|
|
|
|
|
helpCheckPlatforms
|
|
|
|
|
(ON_GROUND, xAxis, x, newY, health, jumpPressed, tl, wallList, game)
|
|
|
|
|
end)
|
|
|
|
|
| [] =>
|
|
|
|
|
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, wallList, game)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
(*** MLTON STRANGE TYPES ERROR:
|
|
|
|
|
*** Trigger by deleting the longer `checkPlatforms` function
|
|
|
|
|
*** and uncommenting the function of the same name that raises Match.
|
|
|
|
|
|
|
|
|
|
fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, game) =
|
|
|
|
|
raise Match
|
|
|
|
|
|
|
|
|
|
*** *)
|
|
|
|
|
|
|
|
|
|
fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, game) =
|
|
|
|
|
let
|
|
|
|
|
val {wallTree, platformTree, ...} = game
|
|
|
|
|
val platCollisions = QuadTree.getCollisionsBelow
|
|
|
|
|
(y, y, size, size, 0, 0, 1920, 1080, 0, wallTree)
|
|
|
|
|
|
|
|
|
|
val wallCollisions = QuadTree.getCollisionSides
|
|
|
|
|
(y, y, size, size, 0, 0, 1920, 1080, 0, wallTree)
|
|
|
|
|
in
|
|
|
|
|
helpCheckPlatforms
|
|
|
|
|
( yAxis, xAxis, x, y, health, jumpPressed
|
|
|
|
|
, platCollisions, wallCollisions, game
|
|
|
|
|
)
|
2024-12-13 22:48:34 +00:00
|
|
|
end
|
2024-12-09 04:37:40 +00:00
|
|
|
|
2024-12-15 09:10:19 +00:00
|
|
|
fun helpMove (x, y, xAxis, yAxis, health, jumpPressed, game: game_type) =
|
2024-12-13 22:48:34 +00:00
|
|
|
let
|
|
|
|
|
(* check against wall quad tree *)
|
|
|
|
|
val desiredX =
|
2024-12-09 04:37:40 +00:00
|
|
|
case xAxis of
|
2024-12-13 22:48:34 +00:00
|
|
|
STAY_STILL => x
|
|
|
|
|
| MOVE_LEFT => x - moveBy
|
|
|
|
|
| MOVE_RIGHT => x + moveBy
|
2024-12-09 04:37:40 +00:00
|
|
|
in
|
|
|
|
|
case yAxis of
|
2024-12-13 22:48:34 +00:00
|
|
|
ON_GROUND =>
|
2024-12-17 22:45:58 +00:00
|
|
|
checkPlatforms
|
|
|
|
|
(yAxis, xAxis, desiredX, y, health, jumpPressed, game)
|
2024-12-14 18:02:57 +00:00
|
|
|
| FLOATING floated =>
|
|
|
|
|
let
|
|
|
|
|
val yAxis =
|
|
|
|
|
if floated = floatLimit then FALLING else FLOATING (floated + 1)
|
|
|
|
|
in
|
2024-12-17 22:45:58 +00:00
|
|
|
checkPlatforms
|
|
|
|
|
(yAxis, xAxis, desiredX, y, health, jumpPressed, game)
|
2024-12-14 18:02:57 +00:00
|
|
|
end
|
2024-12-13 22:48:34 +00:00
|
|
|
| FALLING =>
|
|
|
|
|
let
|
|
|
|
|
val desiredY = y + moveBy
|
|
|
|
|
in
|
2024-12-17 22:45:58 +00:00
|
|
|
checkPlatforms
|
|
|
|
|
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, game)
|
|
|
|
|
end
|
|
|
|
|
| DROP_BELOW_PLATFORM =>
|
|
|
|
|
let
|
|
|
|
|
val desiredY = y + moveBy
|
|
|
|
|
in
|
|
|
|
|
checkPlatforms
|
|
|
|
|
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, game)
|
2024-12-13 22:48:34 +00:00
|
|
|
end
|
|
|
|
|
| JUMPING jumped =>
|
|
|
|
|
if jumped + moveBy > jumpLimit then
|
|
|
|
|
(* if we are above the jump limit, trigger a fall *)
|
2024-12-09 04:37:40 +00:00
|
|
|
let
|
2024-12-17 22:45:58 +00:00
|
|
|
val newYAxis = FLOATING 0
|
2024-12-09 04:37:40 +00:00
|
|
|
in
|
2024-12-17 22:45:58 +00:00
|
|
|
checkPlatforms
|
|
|
|
|
(newYAxis, xAxis, desiredX, y, health, jumpPressed, game)
|
2024-12-09 04:37:40 +00:00
|
|
|
end
|
|
|
|
|
else
|
2024-12-13 22:48:34 +00:00
|
|
|
(* jump *)
|
|
|
|
|
let
|
|
|
|
|
val newJumped = jumped + moveBy
|
2024-12-17 22:45:58 +00:00
|
|
|
val newYAxis = JUMPING newJumped
|
2024-12-13 22:48:34 +00:00
|
|
|
val desiredY = y - moveBy
|
|
|
|
|
in
|
2024-12-17 22:45:58 +00:00
|
|
|
checkPlatforms
|
|
|
|
|
(newYAxis, xAxis, desiredX, desiredY, health, jumpPressed, game)
|
2024-12-13 22:48:34 +00:00
|
|
|
end
|
2024-12-09 04:37:40 +00:00
|
|
|
end
|
2024-12-14 07:59:43 +00:00
|
|
|
|
|
|
|
|
fun getXAxis (lh, rh) =
|
|
|
|
|
case (lh, rh) of
|
|
|
|
|
(false, false) => STAY_STILL
|
|
|
|
|
| (false, true) => MOVE_RIGHT
|
|
|
|
|
| (true, false) => MOVE_LEFT
|
|
|
|
|
| (true, true) => STAY_STILL
|
|
|
|
|
|
2024-12-14 18:02:57 +00:00
|
|
|
(* function returns default yAxis when neither up/down are pressed
|
|
|
|
|
* or both are pressed.
|
|
|
|
|
*
|
|
|
|
|
* In the case where the user was previously jumping,
|
|
|
|
|
* we enter the floating stage, because it's normal for games
|
|
|
|
|
* to have a very brief floating/gliding period before applying gravity.
|
|
|
|
|
*
|
|
|
|
|
* In the case where the user was previously floating, we want the player to
|
|
|
|
|
* keep floating at this point (another function will apply gravity if we
|
|
|
|
|
* floated enough).
|
|
|
|
|
*
|
|
|
|
|
* In every other case, we return the FALLING variant,
|
|
|
|
|
* which has the same effect as returning the ON_GROUND variant,
|
|
|
|
|
* except that it means gravity is applied if we walk off a platform.
|
|
|
|
|
* *)
|
|
|
|
|
fun defaultYAxis prevAxis =
|
|
|
|
|
case prevAxis of
|
|
|
|
|
JUMPING _ => FLOATING 0
|
|
|
|
|
| FLOATING _ => prevAxis
|
|
|
|
|
| _ => FALLING
|
|
|
|
|
|
|
|
|
|
(* We want to prevent a double jump
|
|
|
|
|
* or jumping while the player is falling
|
|
|
|
|
* so we only switch to the JUMPING case if the player
|
|
|
|
|
* is on the ground. *)
|
2024-12-14 21:05:51 +00:00
|
|
|
fun onJumpPressed (prevAxis, jumpPressed) =
|
2024-12-14 18:02:57 +00:00
|
|
|
case prevAxis of
|
2024-12-14 21:05:51 +00:00
|
|
|
ON_GROUND => if jumpPressed then prevAxis else JUMPING 0
|
2024-12-14 18:02:57 +00:00
|
|
|
| _ => prevAxis
|
|
|
|
|
|
2024-12-15 09:10:19 +00:00
|
|
|
fun move (game: game_type, input) =
|
2024-12-14 07:59:43 +00:00
|
|
|
let
|
2024-12-15 09:10:19 +00:00
|
|
|
val {x, y, yAxis, health, jumpPressed, ...} = #player game
|
2024-12-14 21:05:51 +00:00
|
|
|
val {leftHeld, rightHeld, upHeld, downHeld} = input
|
2024-12-15 09:10:19 +00:00
|
|
|
|
2024-12-14 07:59:43 +00:00
|
|
|
val xAxis = getXAxis (leftHeld, rightHeld)
|
|
|
|
|
in
|
2024-12-14 21:05:51 +00:00
|
|
|
case (upHeld, downHeld) of
|
|
|
|
|
(false, false) =>
|
|
|
|
|
let
|
|
|
|
|
val yAxis = defaultYAxis yAxis
|
|
|
|
|
val jumpPressed = false
|
|
|
|
|
in
|
2024-12-15 09:10:19 +00:00
|
|
|
helpMove (x, y, xAxis, yAxis, health, jumpPressed, game)
|
2024-12-14 21:05:51 +00:00
|
|
|
end
|
|
|
|
|
| (true, true) =>
|
2024-12-15 09:10:19 +00:00
|
|
|
let val yAxis = defaultYAxis yAxis
|
|
|
|
|
in helpMove (x, y, xAxis, yAxis, health, jumpPressed, game)
|
2024-12-14 21:05:51 +00:00
|
|
|
end
|
|
|
|
|
| (true, false) =>
|
|
|
|
|
let
|
|
|
|
|
val yAxis = onJumpPressed (yAxis, jumpPressed)
|
|
|
|
|
val jumpPressed = true
|
|
|
|
|
in
|
2024-12-15 09:10:19 +00:00
|
|
|
helpMove (x, y, xAxis, yAxis, health, jumpPressed, game)
|
2024-12-14 21:05:51 +00:00
|
|
|
end
|
|
|
|
|
| (false, true) =>
|
|
|
|
|
(* todo: should move down if on platform *)
|
2024-12-15 09:10:19 +00:00
|
|
|
let val jumpPressed = false
|
|
|
|
|
in helpMove (x, y, xAxis, yAxis, health, jumpPressed, game)
|
2024-12-14 21:05:51 +00:00
|
|
|
end
|
2024-12-14 07:59:43 +00:00
|
|
|
end
|
2024-12-15 09:10:19 +00:00
|
|
|
|
2024-12-16 00:58:59 +00:00
|
|
|
(* block is placeholder asset *)
|
|
|
|
|
fun getDrawVec ({x, y, ...}: player, width, height) =
|
2024-12-17 09:16:22 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
val x = Real32.fromInt x * wratio
|
|
|
|
|
val y = Real32.fromInt y * wratio + yOffset
|
|
|
|
|
|
|
|
|
|
val realSize = realSize * wratio
|
|
|
|
|
in
|
|
|
|
|
Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 0.5)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
val x = Real32.fromInt x * hratio + xOffset
|
|
|
|
|
val y = Real32.fromInt y * hratio
|
|
|
|
|
|
|
|
|
|
val realSize = realSize * hratio
|
|
|
|
|
in
|
|
|
|
|
Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 0.5)
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-12-09 04:37:40 +00:00
|
|
|
end
|