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

View File

@@ -7,19 +7,22 @@ struct
val realSize = 35.0
val moveBy = 5
val jumpLimit = 150
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
, xAxis = xAxis
, reaction = reaction
, health = health
, x = x
, y = y
, 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
open QuadTree
in
@@ -32,7 +35,7 @@ struct
val newX = wallX + wallWidth
in
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, tl, game)
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, reaction, tl, game)
end
| (QUERY_ON_RIGHT_SIDE, wallID) :: tl =>
let
@@ -42,7 +45,7 @@ struct
val newX = wallX - size
in
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, tl, game)
checkWalls (yAxis, xAxis, newX, y, health, jumpPressed, reaction, tl, game)
end
| (QUERY_ON_BOTTOM_SIDE, wallID) :: tl =>
let
@@ -52,17 +55,18 @@ struct
val newY = wallY - size
in
checkWalls
(ON_GROUND, xAxis, x, newY, health, jumpPressed, tl, game)
(ON_GROUND, xAxis, x, newY, health, jumpPressed, reaction, tl, game)
end
| (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
fun helpCheckPlatforms
( yAxis, xAxis, x, y, health
, jumpPressed, platList, wallList, game
, jumpPressed, reaction
, platList, wallList, game
) =
let
open QuadTree
@@ -73,11 +77,11 @@ struct
DROP_BELOW_PLATFORM =>
(* pass through, allowing player to drop below the platform *)
helpCheckPlatforms
(yAxis, xAxis, x, y, health, jumpPressed, tl, wallList, game)
(yAxis, xAxis, x, y, health, jumpPressed, reaction, tl, wallList, game)
| JUMPING _ =>
(* pass through, allowing player to jump above the platform *)
helpCheckPlatforms
(yAxis, xAxis, x, y, health, jumpPressed, tl, wallList, game)
(yAxis, xAxis, x, y, health, jumpPressed, reaction, tl, wallList, game)
| _ =>
let
(* default case:
@@ -100,13 +104,13 @@ struct
val newY = platY - size
in
helpCheckPlatforms
(ON_GROUND, xAxis, x, newY, health, jumpPressed, tl, wallList, game)
(ON_GROUND, xAxis, x, newY, health, jumpPressed, reaction, tl, wallList, game)
end)
| [] =>
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, wallList, game)
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, reaction, wallList, game)
end
fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, game) =
fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, reaction, game) =
let
val {wallTree, platformTree, ...} = game
val platCollisions = QuadTree.getCollisionsBelow
@@ -116,12 +120,12 @@ struct
(x, y, size, size, 0, 0, 1920, 1080, 0, wallTree)
in
helpCheckPlatforms
( yAxis, xAxis, x, y, health, jumpPressed
( yAxis, xAxis, x, y, health, jumpPressed, reaction
, platCollisions, wallCollisions, game
)
end
fun helpMove (x, y, xAxis, yAxis, health, jumpPressed, game: game_type) =
fun helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game) =
let
(* check against wall quad tree *)
val desiredX =
@@ -133,28 +137,28 @@ struct
case yAxis of
ON_GROUND =>
checkPlatforms
(yAxis, xAxis, desiredX, y, health, jumpPressed, game)
(yAxis, xAxis, desiredX, y, health, jumpPressed, reaction, game)
| FLOATING floated =>
let
val yAxis =
if floated = floatLimit then FALLING else FLOATING (floated + 1)
in
checkPlatforms
(yAxis, xAxis, desiredX, y, health, jumpPressed, game)
(yAxis, xAxis, desiredX, y, health, jumpPressed, reaction, game)
end
| FALLING =>
let
val desiredY = y + moveBy
in
checkPlatforms
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, game)
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, reaction, game)
end
| DROP_BELOW_PLATFORM =>
let
val desiredY = y + moveBy
in
checkPlatforms
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, game)
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, reaction, game)
end
| JUMPING jumped =>
if jumped + moveBy > jumpLimit then
@@ -163,7 +167,7 @@ struct
val newYAxis = FLOATING 0
in
checkPlatforms
(newYAxis, xAxis, desiredX, y, health, jumpPressed, game)
(newYAxis, xAxis, desiredX, y, health, jumpPressed, reaction, game)
end
else
(* jump *)
@@ -173,7 +177,9 @@ struct
val desiredY = y - moveBy
in
checkPlatforms
(newYAxis, xAxis, desiredX, desiredY, health, jumpPressed, game)
( newYAxis, xAxis, desiredX, desiredY
, health, jumpPressed, reaction, game
)
end
end
@@ -214,7 +220,7 @@ struct
ON_GROUND => if jumpPressed then prevAxis else JUMPING 0
| _ => prevAxis
fun move (game: game_type, input) =
fun handleInput (game: game_type, input, reaction) =
let
val {x, y, yAxis, health, jumpPressed, ...} = #player game
val {leftHeld, rightHeld, upHeld, downHeld} = input
@@ -227,29 +233,56 @@ struct
val yAxis = defaultYAxis yAxis
val jumpPressed = false
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, game)
helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game)
end
| (true, true) =>
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
| (true, false) =>
let
val yAxis = onJumpPressed (yAxis, jumpPressed)
val jumpPressed = true
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, game)
helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game)
end
| (false, true) =>
(* todo: should move down if on platform *)
let
val jumpPressed = false
val yAxis = DROP_BELOW_PLATFORM
in
helpMove (x, y, xAxis, yAxis, health, jumpPressed, game)
helpMove (x, y, xAxis, yAxis, health, jumpPressed, reaction, game)
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 *)
fun getDrawVec ({x, y, ...}: player, width, height) =
let