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

View File

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