add recoil state for player (next: enter recoil state when player touches enemy)

This commit is contained in:
2024-12-19 03:58:37 +00:00
parent 05cc1d3a46
commit 8dc5ad9359
2 changed files with 67 additions and 27 deletions

View File

@@ -13,9 +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
type player = type player =
{ yAxis: player_y_axis { yAxis: player_y_axis
, xAxis: player_x_axis , xAxis: player_x_axis
, reaction: player_reaction
, health: int , health: int
, x: int , x: int
, y: int , y: int
@@ -53,9 +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
type player = type player =
{ yAxis: player_y_axis { yAxis: player_y_axis
, xAxis: player_x_axis , xAxis: player_x_axis
, reaction: player_reaction
, health: int , health: int
, x: int , x: int
, y: int , y: int
@@ -79,6 +85,7 @@ struct
val player = val player =
{ yAxis = JUMPING 0 { yAxis = JUMPING 0
, xAxis = STAY_STILL , xAxis = STAY_STILL
, reaction = NO_REACTION
, health = 3 , health = 3
, x = 500 , x = 500
, y = 500 , y = 500

View File

@@ -7,19 +7,22 @@ struct
val realSize = 35.0 val realSize = 35.0
val moveBy = 5 val moveBy = 5
val jumpLimit = 150 val jumpLimit = 150
val floatLimit = 3 val floatLimit = 3
val recoilLimit = 5
fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed) = fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, reaction) =
{ yAxis = yAxis { yAxis = yAxis
, xAxis = xAxis , xAxis = xAxis
, reaction = reaction
, health = health , health = health
, x = x , x = x
, y = y , y = y
, jumpPressed = jumpPressed , jumpPressed = jumpPressed
} }
fun checkWalls (yAxis, xAxis, x, y, health, jumpPressed, lst, game: game_type) = fun checkWalls (yAxis, xAxis, x, y, health, jumpPressed, reaction, lst, game: game_type) =
let let
open QuadTree open QuadTree
in in
@@ -32,7 +35,7 @@ struct
val newX = wallX + wallWidth val newX = wallX + wallWidth
in in
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, tl, game) checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, reaction, tl, game)
end end
| (QUERY_ON_RIGHT_SIDE, wallID) :: tl => | (QUERY_ON_RIGHT_SIDE, wallID) :: tl =>
let let
@@ -42,7 +45,7 @@ struct
val newX = wallX - size val newX = wallX - size
in in
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, tl, game) checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, reaction, tl, game)
end end
| (QUERY_ON_BOTTOM_SIDE, wallID) :: tl => | (QUERY_ON_BOTTOM_SIDE, wallID) :: tl =>
let let
@@ -52,17 +55,18 @@ struct
val newY = wallY - size val newY = wallY - size
in in
checkWalls checkWalls
(ON_GROUND, xAxis, x, newY, health, jumpPressed, tl, game) (ON_GROUND, xAxis, x, newY, health, jumpPressed, reaction, tl, game)
end end
| (QUERY_ON_TOP_SIDE, wallID) :: tl => | (QUERY_ON_TOP_SIDE, wallID) :: tl =>
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, tl, game) checkWalls (yAxis, xAxis, x, y, health, jumpPressed, reaction, tl, game)
| [] => | [] =>
mkPlayer (health, xAxis, yAxis, x, y, jumpPressed) mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, reaction)
end end
fun helpCheckPlatforms fun helpCheckPlatforms
( yAxis, xAxis, x, y, health ( yAxis, xAxis, x, y, health
, jumpPressed, platList, wallList, game , jumpPressed, reaction
, platList, wallList, game
) = ) =
let let
open QuadTree open QuadTree
@@ -73,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, tl, wallList, game) (yAxis, xAxis, x, y, health, jumpPressed, reaction, 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, tl, wallList, game) (yAxis, xAxis, x, y, health, jumpPressed, reaction, tl, wallList, game)
| _ => | _ =>
let let
(* default case: (* default case:
@@ -100,13 +104,13 @@ struct
val newY = platY - size val newY = platY - size
in in
helpCheckPlatforms helpCheckPlatforms
(ON_GROUND, xAxis, x, newY, health, jumpPressed, tl, wallList, game) (ON_GROUND, xAxis, x, newY, health, jumpPressed, reaction, tl, wallList, game)
end) end)
| [] => | [] =>
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, wallList, game) checkWalls (yAxis, xAxis, x, y, health, jumpPressed, reaction, wallList, game)
end end
fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, game) = fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, reaction, game) =
let let
val {wallTree, platformTree, ...} = game val {wallTree, platformTree, ...} = game
val platCollisions = QuadTree.getCollisionsBelow val platCollisions = QuadTree.getCollisionsBelow
@@ -116,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 ( yAxis, xAxis, x, y, health, jumpPressed, reaction
, platCollisions, wallCollisions, game , platCollisions, wallCollisions, game
) )
end end
fun helpMove (x, y, xAxis, yAxis, health, jumpPressed, game: game_type) = fun helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game) =
let let
(* check against wall quad tree *) (* check against wall quad tree *)
val desiredX = val desiredX =
@@ -133,28 +137,28 @@ struct
case yAxis of case yAxis of
ON_GROUND => ON_GROUND =>
checkPlatforms checkPlatforms
(yAxis, xAxis, desiredX, y, health, jumpPressed, game) (yAxis, xAxis, desiredX, y, health, jumpPressed, reaction, 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, game) (yAxis, xAxis, desiredX, y, health, jumpPressed, reaction, 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, game) (yAxis, xAxis, desiredX, desiredY, health, jumpPressed, reaction, 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, game) (yAxis, xAxis, desiredX, desiredY, health, jumpPressed, reaction, game)
end end
| JUMPING jumped => | JUMPING jumped =>
if jumped + moveBy > jumpLimit then if jumped + moveBy > jumpLimit then
@@ -163,7 +167,7 @@ struct
val newYAxis = FLOATING 0 val newYAxis = FLOATING 0
in in
checkPlatforms checkPlatforms
(newYAxis, xAxis, desiredX, y, health, jumpPressed, game) (newYAxis, xAxis, desiredX, y, health, jumpPressed, reaction, game)
end end
else else
(* jump *) (* jump *)
@@ -173,7 +177,9 @@ struct
val desiredY = y - moveBy val desiredY = y - moveBy
in in
checkPlatforms checkPlatforms
(newYAxis, xAxis, desiredX, desiredY, health, jumpPressed, game) ( newYAxis, xAxis, desiredX, desiredY
, health, jumpPressed, reaction, game
)
end end
end end
@@ -214,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 move (game: game_type, input) = fun handleInput (game: game_type, input, reaction) =
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
@@ -227,29 +233,56 @@ struct
val yAxis = defaultYAxis yAxis val yAxis = defaultYAxis yAxis
val jumpPressed = false val jumpPressed = false
in in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, game) helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, 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, game) in helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, 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, game) helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game)
end end
| (false, true) => | (false, true) =>
(* todo: should move down if on platform *)
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, game) helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game)
end end
end end
fun move (game: game_type, input) =
let
val player = #player game
val reaction = #reaction player
in
case reaction of
NO_REACTION => handleInput (game, input, reaction)
| RECOIL recoiled =>
(* if player is recoiling, don't accept or adjust any input.
* However, if player has reached the recoil limit, exit the recoil
* state and accept input.
* *)
if recoiled = recoilLimit then
handleInput (game, input, NO_REACTION)
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 reaction = RECOIL recoiled
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game)
end
end
(* block is placeholder asset *) (* block is placeholder asset *)
fun getDrawVec ({x, y, ...}: player, width, height) = fun getDrawVec ({x, y, ...}: player, width, height) =
let let