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