separate RECOIL state to RECOIL_LEFT and RECOIL_RIGHT, telling us which direction player reccoils from

This commit is contained in:
2024-12-19 04:09:03 +00:00
parent 8dc5ad9359
commit 9ed32f38d2
2 changed files with 58 additions and 39 deletions

View File

@@ -13,12 +13,12 @@ sig
datatype player_x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT datatype player_x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT
datatype player_reaction = NO_REACTION | RECOIL of int datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int
type player = type player =
{ yAxis: player_y_axis { yAxis: player_y_axis
, xAxis: player_x_axis , xAxis: player_x_axis
, reaction: player_reaction , recoil: player_recoil
, health: int , health: int
, x: int , x: int
, y: int , y: int
@@ -56,12 +56,12 @@ struct
datatype player_x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT datatype player_x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT
datatype player_reaction = NO_REACTION | RECOIL of int datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int
type player = type player =
{ yAxis: player_y_axis { yAxis: player_y_axis
, xAxis: player_x_axis , xAxis: player_x_axis
, reaction: player_reaction , recoil: player_recoil
, health: int , health: int
, x: int , x: int
, y: int , y: int
@@ -85,7 +85,7 @@ struct
val player = val player =
{ yAxis = JUMPING 0 { yAxis = JUMPING 0
, xAxis = STAY_STILL , xAxis = STAY_STILL
, reaction = NO_REACTION , recoil = NO_RECOIL
, health = 3 , health = 3
, x = 500 , x = 500
, y = 500 , y = 500

View File

@@ -12,17 +12,17 @@ struct
val floatLimit = 3 val floatLimit = 3
val recoilLimit = 5 val recoilLimit = 5
fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, reaction) = fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, recoil) =
{ yAxis = yAxis { yAxis = yAxis
, xAxis = xAxis , xAxis = xAxis
, reaction = reaction , recoil = recoil
, health = health , health = health
, x = x , x = x
, y = y , y = y
, jumpPressed = jumpPressed , jumpPressed = jumpPressed
} }
fun checkWalls (yAxis, xAxis, x, y, health, jumpPressed, reaction, lst, game: game_type) = fun checkWalls (yAxis, xAxis, x, y, health, jumpPressed, recoil, lst, game: game_type) =
let let
open QuadTree open QuadTree
in in
@@ -35,7 +35,7 @@ struct
val newX = wallX + wallWidth val newX = wallX + wallWidth
in in
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, reaction, tl, game) checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, recoil, tl, game)
end end
| (QUERY_ON_RIGHT_SIDE, wallID) :: tl => | (QUERY_ON_RIGHT_SIDE, wallID) :: tl =>
let let
@@ -45,7 +45,7 @@ struct
val newX = wallX - size val newX = wallX - size
in in
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, reaction, tl, game) checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, recoil, tl, game)
end end
| (QUERY_ON_BOTTOM_SIDE, wallID) :: tl => | (QUERY_ON_BOTTOM_SIDE, wallID) :: tl =>
let let
@@ -55,17 +55,17 @@ struct
val newY = wallY - size val newY = wallY - size
in in
checkWalls checkWalls
(ON_GROUND, xAxis, x, newY, health, jumpPressed, reaction, tl, game) (ON_GROUND, xAxis, x, newY, health, jumpPressed, recoil, tl, game)
end end
| (QUERY_ON_TOP_SIDE, wallID) :: tl => | (QUERY_ON_TOP_SIDE, wallID) :: tl =>
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, reaction, tl, game) checkWalls (yAxis, xAxis, x, y, health, jumpPressed, recoil, tl, game)
| [] => | [] =>
mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, reaction) mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, recoil)
end end
fun helpCheckPlatforms fun helpCheckPlatforms
( yAxis, xAxis, x, y, health ( yAxis, xAxis, x, y, health
, jumpPressed, reaction , jumpPressed, recoil
, platList, wallList, game , platList, wallList, game
) = ) =
let let
@@ -77,11 +77,11 @@ struct
DROP_BELOW_PLATFORM => DROP_BELOW_PLATFORM =>
(* pass through, allowing player to drop below the platform *) (* pass through, allowing player to drop below the platform *)
helpCheckPlatforms helpCheckPlatforms
(yAxis, xAxis, x, y, health, jumpPressed, reaction, tl, wallList, game) (yAxis, xAxis, x, y, health, jumpPressed, recoil, tl, wallList, game)
| JUMPING _ => | JUMPING _ =>
(* pass through, allowing player to jump above the platform *) (* pass through, allowing player to jump above the platform *)
helpCheckPlatforms helpCheckPlatforms
(yAxis, xAxis, x, y, health, jumpPressed, reaction, tl, wallList, game) (yAxis, xAxis, x, y, health, jumpPressed, recoil, tl, wallList, game)
| _ => | _ =>
let let
(* default case: (* default case:
@@ -104,13 +104,13 @@ struct
val newY = platY - size val newY = platY - size
in in
helpCheckPlatforms helpCheckPlatforms
(ON_GROUND, xAxis, x, newY, health, jumpPressed, reaction, tl, wallList, game) (ON_GROUND, xAxis, x, newY, health, jumpPressed, recoil, tl, wallList, game)
end) end)
| [] => | [] =>
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, reaction, wallList, game) checkWalls (yAxis, xAxis, x, y, health, jumpPressed, recoil, wallList, game)
end end
fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, reaction, game) = fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, recoil, game) =
let let
val {wallTree, platformTree, ...} = game val {wallTree, platformTree, ...} = game
val platCollisions = QuadTree.getCollisionsBelow val platCollisions = QuadTree.getCollisionsBelow
@@ -120,12 +120,12 @@ struct
(x, y, size, size, 0, 0, 1920, 1080, 0, wallTree) (x, y, size, size, 0, 0, 1920, 1080, 0, wallTree)
in in
helpCheckPlatforms helpCheckPlatforms
( yAxis, xAxis, x, y, health, jumpPressed, reaction ( yAxis, xAxis, x, y, health, jumpPressed, recoil
, platCollisions, wallCollisions, game , platCollisions, wallCollisions, game
) )
end end
fun helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game) = fun helpMove (x, y, xAxis, yAxis, health, jumpPressed, recoil, game) =
let let
(* check against wall quad tree *) (* check against wall quad tree *)
val desiredX = val desiredX =
@@ -137,28 +137,28 @@ struct
case yAxis of case yAxis of
ON_GROUND => ON_GROUND =>
checkPlatforms checkPlatforms
(yAxis, xAxis, desiredX, y, health, jumpPressed, reaction, game) (yAxis, xAxis, desiredX, y, health, jumpPressed, recoil, game)
| FLOATING floated => | FLOATING floated =>
let let
val yAxis = val yAxis =
if floated = floatLimit then FALLING else FLOATING (floated + 1) if floated = floatLimit then FALLING else FLOATING (floated + 1)
in in
checkPlatforms checkPlatforms
(yAxis, xAxis, desiredX, y, health, jumpPressed, reaction, game) (yAxis, xAxis, desiredX, y, health, jumpPressed, recoil, game)
end end
| FALLING => | FALLING =>
let let
val desiredY = y + moveBy val desiredY = y + moveBy
in in
checkPlatforms checkPlatforms
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, reaction, game) (yAxis, xAxis, desiredX, desiredY, health, jumpPressed, recoil, game)
end end
| DROP_BELOW_PLATFORM => | DROP_BELOW_PLATFORM =>
let let
val desiredY = y + moveBy val desiredY = y + moveBy
in in
checkPlatforms checkPlatforms
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, reaction, game) (yAxis, xAxis, desiredX, desiredY, health, jumpPressed, recoil, game)
end end
| JUMPING jumped => | JUMPING jumped =>
if jumped + moveBy > jumpLimit then if jumped + moveBy > jumpLimit then
@@ -167,7 +167,7 @@ struct
val newYAxis = FLOATING 0 val newYAxis = FLOATING 0
in in
checkPlatforms checkPlatforms
(newYAxis, xAxis, desiredX, y, health, jumpPressed, reaction, game) (newYAxis, xAxis, desiredX, y, health, jumpPressed, recoil, game)
end end
else else
(* jump *) (* jump *)
@@ -178,7 +178,7 @@ struct
in in
checkPlatforms checkPlatforms
( newYAxis, xAxis, desiredX, desiredY ( newYAxis, xAxis, desiredX, desiredY
, health, jumpPressed, reaction, game , health, jumpPressed, recoil, game
) )
end end
end end
@@ -220,7 +220,7 @@ struct
ON_GROUND => if jumpPressed then prevAxis else JUMPING 0 ON_GROUND => if jumpPressed then prevAxis else JUMPING 0
| _ => prevAxis | _ => prevAxis
fun handleInput (game: game_type, input, reaction) = fun handleInput (game: game_type, input, recoil) =
let let
val {x, y, yAxis, health, jumpPressed, ...} = #player game val {x, y, yAxis, health, jumpPressed, ...} = #player game
val {leftHeld, rightHeld, upHeld, downHeld} = input val {leftHeld, rightHeld, upHeld, downHeld} = input
@@ -233,53 +233,72 @@ struct
val yAxis = defaultYAxis yAxis val yAxis = defaultYAxis yAxis
val jumpPressed = false val jumpPressed = false
in in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game) helpMove (x, y, xAxis, yAxis, health, jumpPressed, recoil, game)
end end
| (true, true) => | (true, true) =>
let val yAxis = defaultYAxis yAxis let val yAxis = defaultYAxis yAxis
in helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game) in helpMove (x, y, xAxis, yAxis, health, jumpPressed, recoil, game)
end end
| (true, false) => | (true, false) =>
let let
val yAxis = onJumpPressed (yAxis, jumpPressed) val yAxis = onJumpPressed (yAxis, jumpPressed)
val jumpPressed = true val jumpPressed = true
in in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game) helpMove (x, y, xAxis, yAxis, health, jumpPressed, recoil, game)
end end
| (false, true) => | (false, true) =>
let let
val jumpPressed = false val jumpPressed = false
val yAxis = DROP_BELOW_PLATFORM val yAxis = DROP_BELOW_PLATFORM
in in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game) helpMove (x, y, xAxis, yAxis, health, jumpPressed, recoil, game)
end end
end end
fun move (game: game_type, input) = fun move (game: game_type, input) =
let let
val player = #player game val player = #player game
val reaction = #reaction player val recoil = #recoil player
in in
case reaction of case recoil of
NO_REACTION => handleInput (game, input, reaction) NO_RECOIL => handleInput (game, input, recoil)
| RECOIL recoiled => | RECOIL_LEFT recoiled =>
(* if player is recoiling, don't accept or adjust any input. (* if player is recoiling, don't accept or adjust any input.
* However, if player has reached the recoil limit, exit the recoil * However, if player has reached the recoil limit, exit the recoil
* state and accept input. * state and accept input.
* *) * *)
if recoiled = recoilLimit then if recoiled = recoilLimit then
handleInput (game, input, NO_REACTION) handleInput (game, input, NO_RECOIL)
else else
let let
val {x, y, health, ...} = player val {x, y, health, ...} = player
(* difference between RECOIL_LEFT and RECOIL_RIGHT
* is the direction player moves back in *)
val x = x - 5 val x = x - 5
val xAxis = STAY_STILL val xAxis = STAY_STILL
val yAxis = FALLING val yAxis = FALLING
val jumpPressed = false val jumpPressed = false
val recoiled = recoiled + 1 val recoiled = recoiled + 1
val reaction = RECOIL recoiled val recoil = RECOIL_LEFT recoiled
in in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game) helpMove (x, y, xAxis, yAxis, health, jumpPressed, recoil, game)
end
| RECOIL_RIGHT recoiled =>
if recoiled = recoilLimit then
handleInput (game, input, NO_RECOIL)
else
let
val {x, y, health, ...} = player
val x = x + 5
val xAxis = STAY_STILL
val yAxis = FALLING
val jumpPressed = false
val recoiled = recoiled + 1
val recoil = RECOIL_RIGHT recoiled
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, recoil, game)
end end
end end