cache 'jumpPressed' state inside player type. We use this boolean to check if the player has jumped before. If they have jumped before, then holding the up button does not cause another jump when falling down (unlike before this commit)

This commit is contained in:
2024-12-14 21:05:51 +00:00
parent 8b8d6f07bc
commit 1017bf1b7a

View File

@@ -11,20 +11,39 @@ struct
val jumpLimit = 150
val floatLimit = 3
type t = {yAxis: y_axis, xAxis: x_axis, health: int, x: int, y: int}
type t =
{ yAxis: y_axis
, xAxis: x_axis
, health: int
, x: int
, y: int
, jumpPressed: bool
}
(* placeholder *)
val initial: t =
{yAxis = JUMPING 0, xAxis = STAY_STILL, health = 3, x = 500, y = 500}
{ yAxis = JUMPING 0
, xAxis = STAY_STILL
, health = 3
, x = 500
, y = 500
, jumpPressed = false
}
(* placeholder *)
fun getVec ({x, y, ...}: t) =
Block.lerp (x, y, realSize, realSize, 1920.0, 1080.0, 0.5, 0.5, 0.5)
fun mkPlayer (health, xAxis, yAxis, x, y) =
{yAxis = yAxis, xAxis = xAxis, health = health, x = x, y = y}
fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed) =
{ yAxis = yAxis
, xAxis = xAxis
, health = health
, x = x
, y = y
, jumpPressed = jumpPressed
}
fun checkWalls (yAxis, xAxis, x, y, health, lst) =
fun checkWalls (yAxis, xAxis, x, y, health, jumpPressed, lst) =
let
open QuadTree
in
@@ -34,28 +53,28 @@ struct
val {x = wallX, width = wallWidth, ...} = Wall.getID wallID
val newX = wallX + wallWidth
in
checkWalls (yAxis, xAxis, newX, y, health, tl)
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, tl)
end
| (QUERY_ON_RIGHT_SIDE, wallID) :: tl =>
let
val {x = wallX, width = wallWidth, ...} = Wall.getID wallID
val newX = wallX - size
in
checkWalls (yAxis, xAxis, newX, y, health, tl)
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, tl)
end
| (QUERY_ON_BOTTOM_SIDE, wallID) :: tl =>
let
val {y = wallY, ...} = Wall.getID wallID
val newY = wallY - size
in
checkWalls (ON_GROUND, xAxis, x, newY, health, tl)
checkWalls (ON_GROUND, xAxis, x, newY, health, jumpPressed, tl)
end
| (QUERY_ON_TOP_SIDE, wallID) :: tl =>
checkWalls (yAxis, xAxis, x, y, health, tl)
| [] => mkPlayer (health, xAxis, yAxis, x, y)
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, tl)
| [] => mkPlayer (health, xAxis, yAxis, x, y, jumpPressed)
end
fun helpMove (x, y, xAxis, yAxis, health) =
fun helpMove (x, y, xAxis, yAxis, health, jumpPressed) =
let
(* check against wall quad tree *)
val desiredX =
@@ -70,7 +89,8 @@ struct
val collisions = QuadTree.getCollisionSides
(desiredX, y, size, size, 0, 0, 1920, 1080, 0, Wall.tree)
in
checkWalls (yAxis, xAxis, desiredX, y, health, collisions)
checkWalls
(yAxis, xAxis, desiredX, y, health, jumpPressed, collisions)
end
| FLOATING floated =>
let
@@ -80,7 +100,8 @@ struct
val yAxis =
if floated = floatLimit then FALLING else FLOATING (floated + 1)
in
checkWalls (yAxis, xAxis, desiredX, y, health, collisions)
checkWalls
(yAxis, xAxis, desiredX, y, health, jumpPressed, collisions)
end
| FALLING =>
let
@@ -88,7 +109,15 @@ struct
val collisions = QuadTree.getCollisionSides
(desiredX, desiredY, size, size, 0, 0, 1920, 1080, 0, Wall.tree)
in
checkWalls (yAxis, xAxis, desiredX, desiredY, health, collisions)
checkWalls
( yAxis
, xAxis
, desiredX
, desiredY
, health
, jumpPressed
, collisions
)
end
| JUMPING jumped =>
if jumped + moveBy > jumpLimit then
@@ -97,7 +126,15 @@ struct
val collisions = QuadTree.getCollisionSides
(desiredX, y, size, size, 0, 0, 1920, 1080, 0, Wall.tree)
in
checkWalls (FLOATING 0, xAxis, desiredX, y, health, collisions)
checkWalls
( FLOATING 0
, xAxis
, desiredX
, y
, health
, jumpPressed
, collisions
)
end
else
(* jump *)
@@ -109,7 +146,15 @@ struct
val collisions = QuadTree.getCollisionSides
(desiredX, desiredY, size, size, 0, 0, 1920, 1080, 0, Wall.tree)
in
checkWalls (yAxis, xAxis, desiredX, desiredY, health, collisions)
checkWalls
( yAxis
, xAxis
, desiredX
, desiredY
, health
, jumpPressed
, collisions
)
end
end
@@ -145,26 +190,43 @@ struct
* or jumping while the player is falling
* so we only switch to the JUMPING case if the player
* is on the ground. *)
fun onJumpPressed prevAxis =
fun onJumpPressed (prevAxis, jumpPressed) =
case prevAxis of
ON_GROUND => JUMPING 0
ON_GROUND => if jumpPressed then prevAxis else JUMPING 0
| _ => prevAxis
fun getYAxis (uh, dh, prevAxis) =
case (uh, dh) of
(false, false) => defaultYAxis prevAxis
| (true, true) => defaultYAxis prevAxis
| (true, false) => onJumpPressed prevAxis
| (false, true) =>
(* todo: should move down if on platform *)
prevAxis
fun move
({x, y, yAxis, health, ...}: t, {leftHeld, rightHeld, upHeld, downHeld}) =
fun move ({x, y, yAxis, health, jumpPressed, ...}: t, input) =
let
val {leftHeld, rightHeld, upHeld, downHeld} = input
val xAxis = getXAxis (leftHeld, rightHeld)
val yAxis = getYAxis (upHeld, downHeld, yAxis)
in
helpMove (x, y, xAxis, yAxis, health)
case (upHeld, downHeld) of
(false, false) =>
let
val yAxis = defaultYAxis yAxis
val jumpPressed = false
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed)
end
| (true, true) =>
let
val yAxis = defaultYAxis yAxis
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed)
end
| (true, false) =>
let
val yAxis = onJumpPressed (yAxis, jumpPressed)
val jumpPressed = true
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed)
end
| (false, true) =>
(* todo: should move down if on platform *)
let
val jumpPressed = false
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed)
end
end
end