add code to stop player from moving when main attack is pressed

This commit is contained in:
2024-12-23 00:28:53 +00:00
parent d8b70dfccb
commit 5819c4facf

View File

@@ -13,6 +13,7 @@ struct
| W_X of int | W_X of int
| W_Y of int | W_Y of int
| W_JUMP_PRESSED of bool | W_JUMP_PRESSED of bool
| W_MAIN_ATTACK_PRESSED of bool
fun mkPlayer fun mkPlayer
( health ( health
@@ -197,6 +198,20 @@ struct
, facing , facing
, mainAttackPressed , mainAttackPressed
) )
| W_MAIN_ATTACK_PRESSED mainAttackPressed =>
mkPlayer
( health
, xAxis
, yAxis
, x
, y
, jumpPressed
, recoil
, attacked
, mainAttack
, facing
, mainAttackPressed
)
end end
fun withPatches (player: player, lst) = fun withPatches (player: player, lst) =
@@ -333,7 +348,7 @@ struct
case lst of case lst of
id :: tl => id :: tl =>
let let
val newRecoil = val playerOnRight =
(* check if collision is closer to left side of enemy or right (* check if collision is closer to left side of enemy or right
* and then chose appropriate direction to recoil in *) * and then chose appropriate direction to recoil in *)
let let
@@ -347,9 +362,12 @@ struct
val eHalfW = Enemy.size div 2 val eHalfW = Enemy.size div 2
val eCentreX = ex + eHalfW val eCentreX = ex + eHalfW
in in
if eCentreX < pCentreX then RECOIL_RIGHT 0 else RECOIL_LEFT 0 eCentreX < pCentreX
end end
val newRecoil =
if playerOnRight then RECOIL_RIGHT 0 else RECOIL_LEFT 0
val acc = W_RECOIL newRecoil :: acc val acc = W_RECOIL newRecoil :: acc
val acc = val acc =
@@ -494,27 +512,69 @@ struct
end end
end end
fun getMainAttackPatches (prevAttack, attackHeld) = fun getMainAttackPatches (prevAttack, attackHeld, mainAttackPressed) =
case prevAttack of case prevAttack of
MAIN_NOT_ATTACKING => if attackHeld then MAIN_ATTACKING 0 else prevAttack MAIN_NOT_ATTACKING =>
if attackHeld andalso not mainAttackPressed then MAIN_ATTACKING 0
else prevAttack
| MAIN_ATTACKING amt => | MAIN_ATTACKING amt =>
if amt = mainAttackLimit then MAIN_NOT_ATTACKING if amt = mainAttackLimit then MAIN_NOT_ATTACKING
else let val amt = amt + 1 in MAIN_ATTACKING amt end else let val amt = amt + 1 in MAIN_ATTACKING amt end
fun getInputPatches (player: player, input) = fun getInputPatches (player: player, input) =
let case #mainAttack player of
val {x, y, yAxis, jumpPressed, facing, mainAttack, ...} = player MAIN_NOT_ATTACKING =>
val {leftHeld, rightHeld, upHeld, downHeld, attackHeld} = input let
val
{ x
, y
, yAxis
, jumpPressed
, facing
, mainAttack
, mainAttackPressed
, ...
} = player
val xAxis = getXAxis (leftHeld, rightHeld) val {leftHeld, rightHeld, upHeld, downHeld, attackHeld} = input
val facing = getFacing (facing, xAxis)
val mainAttack = getMainAttackPatches (mainAttack, attackHeld)
val acc = [W_X_AXIS xAxis, W_FACING facing, W_MAIN_ATTACK mainAttack] val xAxis = getXAxis (leftHeld, rightHeld)
val acc = getJumpPatches (player, upHeld, downHeld, acc) val facing = getFacing (facing, xAxis)
in val mainAttack =
acc getMainAttackPatches (mainAttack, attackHeld, mainAttackPressed)
end
val mainAttackPressed =
case mainAttack of
MAIN_ATTACKING _ => true
| _ => attackHeld
val acc =
[ W_X_AXIS xAxis
, W_FACING facing
, W_MAIN_ATTACK mainAttack
, W_MAIN_ATTACK_PRESSED mainAttackPressed
]
val acc = getJumpPatches (player, upHeld, downHeld, acc)
in
acc
end
| MAIN_ATTACKING _ =>
let
val {mainAttack, mainAttackPressed, ...} = player
val {attackHeld, ...} = input
val mainAttack =
getMainAttackPatches (mainAttack, attackHeld, mainAttackPressed)
val mainAttackPressed =
case mainAttack of
MAIN_ATTACKING _ => true
| _ => mainAttackPressed
in
[ W_X_AXIS STAY_STILL
, W_Y_AXIS FALLING
, W_MAIN_ATTACK mainAttack
, W_MAIN_ATTACK_PRESSED mainAttackPressed
]
end
fun getRecoilPatches player = fun getRecoilPatches player =
case #recoil player of case #recoil player of