add facing datatype to player record, which represents the last position the player is facing (player is facing left if they moved leftwards or colllided with an enemy who is to the left, and player is facing right if they move rightwards or player collides with enemy who is to the right)

This commit is contained in:
2024-12-20 17:41:21 +00:00
parent aeb3756e5b
commit c71dbb7fca
2 changed files with 84 additions and 35 deletions

View File

@@ -17,11 +17,14 @@ sig
datatype player_attacked = NOT_ATTACKED | ATTACKED of int
datatype facing = FACING_LEFT | FACING_RIGHT
type player =
{ yAxis: player_y_axis
, xAxis: player_x_axis
, recoil: player_recoil
, attacked: player_attacked
, facing: facing
, health: int
, x: int
, y: int
@@ -63,11 +66,14 @@ struct
datatype player_attacked = NOT_ATTACKED | ATTACKED of int
datatype facing = FACING_LEFT | FACING_RIGHT
type player =
{ yAxis: player_y_axis
, xAxis: player_x_axis
, recoil: player_recoil
, attacked: player_attacked
, facing: facing
, health: int
, x: int
, y: int
@@ -93,6 +99,7 @@ struct
, xAxis = STAY_STILL
, recoil = NO_RECOIL
, attacked = NOT_ATTACKED
, facing = FACING_RIGHT
, health = 3
, x = 500
, y = 500

View File

@@ -13,18 +13,27 @@ struct
val recoilLimit = 15
val attackLimit = 55
fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, recoil, attacked) =
fun mkPlayer
( health, xAxis, yAxis, x, y
, jumpPressed, recoil, attacked
, facing
) =
{ yAxis = yAxis
, xAxis = xAxis
, recoil = recoil
, attacked = attacked
, facing = facing
, health = health
, x = x
, y = y
, jumpPressed = jumpPressed
}
fun checkWalls (yAxis, xAxis, x, y, health, jumpPressed, recoil, attacked, lst, game: game_type) =
fun checkWalls
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, attacked
, facing, lst, game: game_type
) =
let
open QuadTree
in
@@ -39,7 +48,7 @@ struct
in
checkWalls
( yAxis, xAxis, newX, y, health, jumpPressed
, recoil, attacked, tl, game
, recoil, attacked, facing, tl, game
)
end
| (QUERY_ON_RIGHT_SIDE, wallID) :: tl =>
@@ -52,7 +61,7 @@ struct
in
checkWalls
( yAxis, xAxis, newX, y, health, jumpPressed
, recoil, attacked, tl, game
, recoil, attacked, facing, tl, game
)
end
| (QUERY_ON_BOTTOM_SIDE, wallID) :: tl =>
@@ -64,21 +73,24 @@ struct
in
checkWalls
( ON_GROUND, xAxis, x, newY, health, jumpPressed
, recoil, attacked, tl, game
, recoil, attacked, facing, tl, game
)
end
| (QUERY_ON_TOP_SIDE, wallID) :: tl =>
checkWalls
( yAxis, xAxis, x, y, health, jumpPressed
, recoil, attacked, tl, game
, recoil, attacked, facing, tl, game
)
| [] =>
mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, recoil, attacked)
mkPlayer
( health, xAxis, yAxis, x, y
, jumpPressed, recoil, attacked, facing
)
end
fun helpCheckPlatforms
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, attacked
, jumpPressed, recoil, attacked, facing
, platList, wallList, game
) =
let
@@ -91,14 +103,14 @@ struct
(* pass through, allowing player to drop below the platform *)
helpCheckPlatforms
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, attacked
, jumpPressed, recoil, attacked, facing
, tl, wallList, game
)
| JUMPING _ =>
(* pass through, allowing player to jump above the platform *)
helpCheckPlatforms
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, attacked
, jumpPressed, recoil, attacked, facing
, tl, wallList, game
)
| _ =>
@@ -124,20 +136,20 @@ struct
in
helpCheckPlatforms
( ON_GROUND, xAxis, x, newY, health
, jumpPressed, recoil, attacked
, jumpPressed, recoil, attacked, facing
, tl, wallList, game
)
end)
| [] =>
checkWalls
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, attacked
, jumpPressed, recoil, attacked, facing
, wallList, game
)
end
fun checkEnemies
( yAxis, xAxis, x, y, health, jumpPressed, recoil, attacked
( yAxis, xAxis, x, y, health, jumpPressed, recoil, attacked, facing
, enemyCollisions, platCollisions, wallCollisions, game
) =
case enemyCollisions of
@@ -162,25 +174,31 @@ struct
RECOIL_LEFT 0
end
val facing =
case newRecoil of
RECOIL_LEFT _ => FACING_RIGHT
| RECOIL_RIGHT _ => FACING_LEFT
| NO_RECOIL => facing
val attacked = ATTACKED 0
in
checkEnemies
( FALLING, STAY_STILL, x, y, health
, jumpPressed, newRecoil, ATTACKED 0
, jumpPressed, newRecoil, ATTACKED 0, facing
, tl, platCollisions, wallCollisions, game
)
end
| [] =>
helpCheckPlatforms
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, attacked
, jumpPressed, recoil, attacked, facing
, platCollisions, wallCollisions, game
)
fun checkCollisions
( yAxis, xAxis, x, y, health
, jumpPressed, recoil
, attacked, game
, attacked, facing, game
) =
let
val {wallTree, platformTree, enemyTree, ...} = game
@@ -210,7 +228,7 @@ struct
in
checkEnemies
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, attacked
, jumpPressed, recoil, attacked, facing
, enemyCollisions, platCollisions, wallCollisions, game
)
end
@@ -225,7 +243,7 @@ struct
in
checkEnemies
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, NOT_ATTACKED
, jumpPressed, recoil, NOT_ATTACKED, facing
, enemyCollisions, platCollisions, wallCollisions, game
)
end
@@ -236,13 +254,17 @@ struct
in
helpCheckPlatforms
( yAxis, xAxis, x, y, health
, jumpPressed, recoil, attacked
, jumpPressed, recoil, attacked, facing
, platCollisions, wallCollisions, game
)
end
end
fun helpMove (x, y, xAxis, yAxis, health, jumpPressed, recoil, attacked, game) =
fun helpMove
( x, y, xAxis, yAxis, health
, jumpPressed, recoil, attacked
, facing, game
) =
let
(* check against wall quad tree *)
val desiredX =
@@ -255,7 +277,8 @@ struct
ON_GROUND =>
checkCollisions
( yAxis, xAxis, desiredX, y, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked
, facing, game
)
| FLOATING floated =>
let
@@ -263,7 +286,10 @@ struct
if floated = floatLimit then FALLING else FLOATING (floated + 1)
in
checkCollisions
(yAxis, xAxis, desiredX, y, health, jumpPressed, recoil, attacked, game)
( yAxis, xAxis, desiredX, y, health
, jumpPressed, recoil, attacked
, facing, game
)
end
| FALLING =>
let
@@ -271,7 +297,7 @@ struct
in
checkCollisions
( yAxis, xAxis, desiredX, desiredY, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked, facing, game
)
end
| DROP_BELOW_PLATFORM =>
@@ -280,7 +306,7 @@ struct
in
checkCollisions
( yAxis, xAxis, desiredX, desiredY, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked, facing, game
)
end
| JUMPING jumped =>
@@ -291,7 +317,7 @@ struct
in
checkCollisions
( newYAxis, xAxis, desiredX, y, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked, facing, game
)
end
else
@@ -303,7 +329,8 @@ struct
in
checkCollisions
( newYAxis, xAxis, desiredX, desiredY
, health, jumpPressed, recoil, attacked, game
, health, jumpPressed, recoil, attacked
, facing, game
)
end
end
@@ -315,6 +342,12 @@ struct
| (true, false) => MOVE_LEFT
| (true, true) => STAY_STILL
fun getFacing (facing, xAxis) =
case xAxis of
STAY_STILL => facing
| MOVE_LEFT => FACING_LEFT
| MOVE_RIGHT => FACING_RIGHT
(* function returns default yAxis when neither up/down are pressed
* or both are pressed.
*
@@ -352,10 +385,11 @@ struct
fun handleInput (game: game_type, input, recoil) =
let
val {x, y, yAxis, health, jumpPressed, attacked, ...} = #player game
val {x, y, yAxis, health, jumpPressed, attacked, facing, ...} = #player game
val {leftHeld, rightHeld, upHeld, downHeld} = input
val xAxis = getXAxis (leftHeld, rightHeld)
val facing = getFacing (facing, xAxis)
in
case (upHeld, downHeld) of
(false, false) =>
@@ -365,7 +399,8 @@ struct
in
helpMove
( x, y, xAxis, yAxis, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked
, facing, game
)
end
| (true, true) =>
@@ -374,7 +409,8 @@ struct
in
helpMove
( x, y, xAxis, yAxis, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked
, facing, game
)
end
| (true, false) =>
@@ -384,7 +420,8 @@ struct
in
helpMove
( x, y, xAxis, yAxis, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked
, facing, game
)
end
| (false, true) =>
@@ -394,7 +431,8 @@ struct
in
helpMove
(x, y, xAxis, yAxis, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked
, facing, game
)
end
end
@@ -415,7 +453,7 @@ struct
handleInput (game, input, NO_RECOIL)
else
let
val {x, y, health, attacked, ...} = player
val {x, y, health, attacked, facing, xAxis, ...} = player
(* difference between RECOIL_LEFT and RECOIL_RIGHT
* is the direction player moves back in *)
val x = x - 5
@@ -425,10 +463,12 @@ struct
val jumpPressed = false
val recoiled = recoiled + 1
val recoil = RECOIL_LEFT recoiled
val facing = getFacing (facing, xAxis)
in
helpMove
( x, y, xAxis, yAxis, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked
, facing, game
)
end
| RECOIL_RIGHT recoiled =>
@@ -436,7 +476,7 @@ struct
handleInput (game, input, NO_RECOIL)
else
let
val {x, y, health, attacked, ...} = player
val {x, y, health, attacked, facing, xAxis, ...} = player
val x = x + 5
val xAxis = STAY_STILL
@@ -444,10 +484,12 @@ struct
val jumpPressed = false
val recoiled = recoiled + 1
val recoil = RECOIL_RIGHT recoiled
val facing = getFacing (facing, xAxis)
in
helpMove
( x, y, xAxis, yAxis, health
, jumpPressed, recoil, attacked, game
, jumpPressed, recoil, attacked
, facing, game
)
end
end