have proper state transitions for recoiling; tested, and works correctly too

This commit is contained in:
2024-12-19 07:53:31 +00:00
parent 9ed32f38d2
commit 65ce098a71

View File

@@ -10,7 +10,7 @@ struct
val jumpLimit = 150 val jumpLimit = 150
val floatLimit = 3 val floatLimit = 3
val recoilLimit = 5 val recoilLimit = 15
fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, recoil) = fun mkPlayer (health, xAxis, yAxis, x, y, jumpPressed, recoil) =
{ yAxis = yAxis { yAxis = yAxis
@@ -110,15 +110,71 @@ struct
checkWalls (yAxis, xAxis, x, y, health, jumpPressed, recoil, wallList, game) checkWalls (yAxis, xAxis, x, y, health, jumpPressed, recoil, wallList, game)
end end
fun checkPlatforms (yAxis, xAxis, x, y, health, jumpPressed, recoil, game) = fun checkEnemies
( yAxis, xAxis, x, y, health, jumpPressed, recoil
, enemyCollisions, platCollisions, wallCollisions, game
) =
case enemyCollisions of
id :: tl =>
let let
val {wallTree, platformTree, ...} = game val newRecoil =
(* check if collision is closer to left side of enemy or right
* and then chose appropriate direction to recoil in *)
let
val pFinishX = x + size
val pHalfW = size div 2
val pCentreX = x + pHalfW
val {x = ex, y = ey, ...} = Vector.sub (#enemies game, id - 1)
val eFinishX = ex + Enemy.size
val eHalfW = Enemy.size div 2
val eCentreX = ex + eHalfW
in
if eCentreX < pCentreX then
RECOIL_RIGHT 0
else
RECOIL_LEFT 0
end
in
checkEnemies
( FALLING, STAY_STILL, x, y, health, false, newRecoil
, tl, platCollisions, wallCollisions, game
)
end
| [] =>
helpCheckPlatforms
( yAxis, xAxis, x, y, health, jumpPressed, recoil
, platCollisions, wallCollisions, game
)
fun checkCollisions (yAxis, xAxis, x, y, health, jumpPressed, recoil, game) =
let
val {wallTree, platformTree, enemyTree, ...} = game
(* control flow is: check enemies -> check platforms -> check walls
* but this is not visible in this function as everything is implemented
* by tail call.
* So, when one function hits the end of its collision list,
* it calls the next function at its tail. *)
val platCollisions = QuadTree.getCollisionsBelow val platCollisions = QuadTree.getCollisionsBelow
(x, y, size, size, 0, 0, 1920, 1080, 0, platformTree) (x, y, size, size, 0, 0, 1920, 1080, 0, platformTree)
val wallCollisions = QuadTree.getCollisionSides val wallCollisions = QuadTree.getCollisionSides
(x, y, size, size, 0, 0, 1920, 1080, 0, wallTree) (x, y, size, size, 0, 0, 1920, 1080, 0, wallTree)
in in
case recoil of
NO_RECOIL =>
let
val enemyCollisions = QuadTree.getCollisions
(x, y, size, size, 0, 0, 1920, 1080, 0, enemyTree)
in
checkEnemies
( yAxis, xAxis, x, y, health, jumpPressed, recoil
, enemyCollisions, platCollisions, wallCollisions, game
)
end
| _ =>
helpCheckPlatforms helpCheckPlatforms
( yAxis, xAxis, x, y, health, jumpPressed, recoil ( yAxis, xAxis, x, y, health, jumpPressed, recoil
, platCollisions, wallCollisions, game , platCollisions, wallCollisions, game
@@ -136,28 +192,28 @@ struct
in in
case yAxis of case yAxis of
ON_GROUND => ON_GROUND =>
checkPlatforms checkCollisions
(yAxis, xAxis, desiredX, y, health, jumpPressed, recoil, 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 checkCollisions
(yAxis, xAxis, desiredX, y, health, jumpPressed, recoil, 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 checkCollisions
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, recoil, 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 checkCollisions
(yAxis, xAxis, desiredX, desiredY, health, jumpPressed, recoil, game) (yAxis, xAxis, desiredX, desiredY, health, jumpPressed, recoil, game)
end end
| JUMPING jumped => | JUMPING jumped =>
@@ -166,7 +222,7 @@ struct
let let
val newYAxis = FLOATING 0 val newYAxis = FLOATING 0
in in
checkPlatforms checkCollisions
(newYAxis, xAxis, desiredX, y, health, jumpPressed, recoil, game) (newYAxis, xAxis, desiredX, y, health, jumpPressed, recoil, game)
end end
else else
@@ -176,7 +232,7 @@ struct
val newYAxis = JUMPING newJumped val newYAxis = JUMPING newJumped
val desiredY = y - moveBy val desiredY = y - moveBy
in in
checkPlatforms checkCollisions
( newYAxis, xAxis, desiredX, desiredY ( newYAxis, xAxis, desiredX, desiredY
, health, jumpPressed, recoil, game , health, jumpPressed, recoil, game
) )