2024-12-09 04:37:40 +00:00
|
|
|
structure Player =
|
|
|
|
|
struct
|
2024-12-15 09:10:19 +00:00
|
|
|
open GameType
|
2025-01-12 19:25:05 +00:00
|
|
|
open PlayerPatch
|
2024-12-22 05:40:33 +00:00
|
|
|
|
|
|
|
|
(* helper functions checking input *)
|
|
|
|
|
fun getXAxis (lh, rh) =
|
|
|
|
|
case (lh, rh) of
|
|
|
|
|
(false, false) => STAY_STILL
|
|
|
|
|
| (false, true) => MOVE_RIGHT
|
|
|
|
|
| (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.
|
|
|
|
|
*
|
|
|
|
|
* In the case where the user was previously jumping,
|
|
|
|
|
* we enter the floating stage, because it's normal for games
|
|
|
|
|
* to have a very brief floating/gliding period before applying gravity.
|
|
|
|
|
*
|
|
|
|
|
* In the case where the user was previously floating, we want the player to
|
|
|
|
|
* keep floating at this point (another function will apply gravity if we
|
|
|
|
|
* floated enough).
|
|
|
|
|
*
|
|
|
|
|
* In every other case, we return the FALLING variant,
|
|
|
|
|
* which has the same effect as returning the ON_GROUND variant,
|
|
|
|
|
* except that it means gravity is applied if we walk off a platform.
|
|
|
|
|
* *)
|
|
|
|
|
fun defaultYAxis prevAxis =
|
|
|
|
|
case prevAxis of
|
|
|
|
|
JUMPING _ => FLOATING 0
|
|
|
|
|
| FLOATING _ => prevAxis
|
2024-12-23 03:59:27 +00:00
|
|
|
| DROP_BELOW_PLATFORM => prevAxis
|
2024-12-22 05:40:33 +00:00
|
|
|
| _ => FALLING
|
|
|
|
|
|
|
|
|
|
(* We want to prevent a double jump
|
|
|
|
|
* or jumping while the player is falling
|
|
|
|
|
* so we only switch to the JUMPING case if the player
|
|
|
|
|
* is on the ground. *)
|
|
|
|
|
fun onJumpPressed (prevAxis, jumpPressed) =
|
|
|
|
|
case prevAxis of
|
|
|
|
|
ON_GROUND =>
|
|
|
|
|
if jumpPressed then (* apply gravity *) FALLING else JUMPING 0
|
|
|
|
|
| _ => prevAxis
|
|
|
|
|
|
2024-12-22 20:21:16 +00:00
|
|
|
fun getJumpPatches (player, upHeld, downHeld, acc) =
|
2024-12-14 07:59:43 +00:00
|
|
|
let
|
2024-12-22 20:21:16 +00:00
|
|
|
val {yAxis, jumpPressed, ...} = player
|
2024-12-14 07:59:43 +00:00
|
|
|
in
|
2024-12-14 21:05:51 +00:00
|
|
|
case (upHeld, downHeld) of
|
|
|
|
|
(false, false) =>
|
|
|
|
|
let
|
|
|
|
|
val yAxis = defaultYAxis yAxis
|
|
|
|
|
val jumpPressed = false
|
|
|
|
|
in
|
2024-12-22 20:21:16 +00:00
|
|
|
W_JUMP_PRESSED jumpPressed :: W_Y_AXIS yAxis :: acc
|
2024-12-14 21:05:51 +00:00
|
|
|
end
|
|
|
|
|
| (true, true) =>
|
2024-12-22 05:40:33 +00:00
|
|
|
let val yAxis = defaultYAxis yAxis
|
2024-12-22 20:21:16 +00:00
|
|
|
in W_Y_AXIS yAxis :: acc
|
2024-12-14 21:05:51 +00:00
|
|
|
end
|
|
|
|
|
| (true, false) =>
|
|
|
|
|
let
|
|
|
|
|
val yAxis = onJumpPressed (yAxis, jumpPressed)
|
|
|
|
|
val jumpPressed = true
|
|
|
|
|
in
|
2024-12-22 20:21:16 +00:00
|
|
|
W_Y_AXIS yAxis :: W_JUMP_PRESSED jumpPressed :: acc
|
2024-12-14 21:05:51 +00:00
|
|
|
end
|
|
|
|
|
| (false, true) =>
|
2024-12-22 05:40:33 +00:00
|
|
|
let
|
2024-12-18 03:30:21 +00:00
|
|
|
val jumpPressed = false
|
|
|
|
|
val yAxis = DROP_BELOW_PLATFORM
|
2024-12-22 05:40:33 +00:00
|
|
|
in
|
2024-12-22 20:21:16 +00:00
|
|
|
W_Y_AXIS yAxis :: W_JUMP_PRESSED jumpPressed :: acc
|
2024-12-14 21:05:51 +00:00
|
|
|
end
|
2024-12-14 07:59:43 +00:00
|
|
|
end
|
2024-12-15 09:10:19 +00:00
|
|
|
|
2025-01-07 13:31:17 +00:00
|
|
|
fun prevWasNotAttacking prevAttack = prevAttack <> MAIN_ATTACKING
|
|
|
|
|
|
|
|
|
|
(* called only when player has no projectiles or was not previously attacking *)
|
|
|
|
|
fun helpGetMainAttackPatches (attackHeld, chargeHeld, charge) =
|
2025-01-08 12:49:29 +00:00
|
|
|
if attackHeld andalso charge > 0 then W_MAIN_ATTACK MAIN_ATTACKING
|
|
|
|
|
else if chargeHeld andalso not attackHeld then W_MAIN_ATTACK MAIN_CHARGING
|
|
|
|
|
else W_MAIN_ATTACK MAIN_NOT_ATTACKING
|
|
|
|
|
|
2025-01-12 07:18:44 +00:00
|
|
|
fun degreesToRadians degrees = Real32.fromInt degrees * Constants.projectilePi
|
2025-01-08 12:49:29 +00:00
|
|
|
|
|
|
|
|
fun defeatedEnemiesToProjectiles
|
2025-01-08 20:26:53 +00:00
|
|
|
(pos, defeteadEnemies, player as {x, y, facing, ...}, acc) =
|
2025-01-08 12:49:29 +00:00
|
|
|
if pos = Vector.length defeteadEnemies then
|
|
|
|
|
Vector.fromList acc
|
|
|
|
|
else
|
|
|
|
|
let
|
2025-01-12 11:46:32 +00:00
|
|
|
val diff =
|
|
|
|
|
Constants.halfPlayerSizeReal - (Constants.projectileSize / 2.0)
|
2025-01-08 12:49:29 +00:00
|
|
|
val x = Real32.fromInt x + diff
|
|
|
|
|
val y = Real32.fromInt y + diff
|
|
|
|
|
|
|
|
|
|
val {angle} = Vector.sub (defeteadEnemies, pos)
|
|
|
|
|
val angle = degreesToRadians angle
|
|
|
|
|
|
2025-01-12 07:18:44 +00:00
|
|
|
val x = ((Real32.Math.cos angle) * Constants.projectileDistance) + x
|
|
|
|
|
val y = ((Real32.Math.sin angle) * Constants.projectileDistance) + y
|
2025-01-08 12:49:29 +00:00
|
|
|
|
|
|
|
|
val x = Real32.toInt IEEEReal.TO_NEAREST x
|
|
|
|
|
val y = Real32.toInt IEEEReal.TO_NEAREST y
|
|
|
|
|
|
2025-01-08 20:26:53 +00:00
|
|
|
val acc = {x = x, y = y, facing = facing} :: acc
|
2025-01-08 12:49:29 +00:00
|
|
|
in
|
|
|
|
|
defeatedEnemiesToProjectiles (pos + 1, defeteadEnemies, player, acc)
|
|
|
|
|
end
|
2024-12-22 20:21:16 +00:00
|
|
|
|
2025-01-08 13:18:35 +00:00
|
|
|
fun getThrowPatches (defeteadEnemies, projectiles, player, acc) =
|
|
|
|
|
let
|
|
|
|
|
val newProjectiles =
|
|
|
|
|
defeatedEnemiesToProjectiles (0, defeteadEnemies, player, [])
|
|
|
|
|
|
|
|
|
|
(* concatenate new projectiles with previous projectiles *)
|
|
|
|
|
val allProjectiles = Vector.concat [newProjectiles, projectiles]
|
|
|
|
|
|
|
|
|
|
(* remove defeated enemies from player record *)
|
|
|
|
|
val enemies = Vector.fromList []
|
|
|
|
|
in
|
|
|
|
|
W_MAIN_ATTACK MAIN_THROWING :: W_PROJECTILES allProjectiles
|
|
|
|
|
:: W_ENEMIES enemies :: acc
|
|
|
|
|
end
|
|
|
|
|
|
2025-01-07 13:31:17 +00:00
|
|
|
fun getMainAttackPatches
|
2025-01-08 12:49:29 +00:00
|
|
|
( prevAttack
|
|
|
|
|
, defeteadEnemies
|
|
|
|
|
, projectiles
|
|
|
|
|
, attackHeld
|
|
|
|
|
, chargeHeld
|
|
|
|
|
, charge
|
|
|
|
|
, player
|
|
|
|
|
, acc
|
|
|
|
|
) =
|
2025-01-08 13:18:35 +00:00
|
|
|
case prevAttack of
|
|
|
|
|
MAIN_NOT_ATTACKING =>
|
|
|
|
|
if attackHeld andalso Vector.length defeteadEnemies > 0 then
|
|
|
|
|
(* shoot projectiles if player was not attacking previously,
|
|
|
|
|
* and there is more than one enemy *)
|
|
|
|
|
getThrowPatches (defeteadEnemies, projectiles, player, acc)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val mainAttack =
|
|
|
|
|
helpGetMainAttackPatches (attackHeld, chargeHeld, charge)
|
|
|
|
|
in
|
|
|
|
|
mainAttack :: acc
|
|
|
|
|
end
|
|
|
|
|
| MAIN_CHARGING =>
|
|
|
|
|
if attackHeld andalso Vector.length defeteadEnemies > 0 then
|
|
|
|
|
getThrowPatches (defeteadEnemies, projectiles, player, acc)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val mainAttack =
|
|
|
|
|
helpGetMainAttackPatches (attackHeld, chargeHeld, charge)
|
|
|
|
|
in
|
|
|
|
|
mainAttack :: acc
|
|
|
|
|
end
|
|
|
|
|
| MAIN_ATTACKING =>
|
2025-01-08 12:49:29 +00:00
|
|
|
let
|
|
|
|
|
val mainAttack =
|
|
|
|
|
helpGetMainAttackPatches (attackHeld, chargeHeld, charge)
|
|
|
|
|
in
|
|
|
|
|
mainAttack :: acc
|
|
|
|
|
end
|
2025-01-08 13:18:35 +00:00
|
|
|
| MAIN_THROWING =>
|
|
|
|
|
if attackHeld then
|
|
|
|
|
acc
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val mainAttack =
|
|
|
|
|
helpGetMainAttackPatches (attackHeld, chargeHeld, charge)
|
|
|
|
|
in
|
|
|
|
|
mainAttack :: acc
|
|
|
|
|
end
|
2025-01-07 13:31:17 +00:00
|
|
|
|
2024-12-22 20:21:16 +00:00
|
|
|
fun getInputPatches (player: player, input) =
|
2024-12-31 10:04:40 +00:00
|
|
|
let
|
|
|
|
|
val
|
|
|
|
|
{ x
|
|
|
|
|
, y
|
|
|
|
|
, yAxis
|
|
|
|
|
, jumpPressed
|
|
|
|
|
, facing
|
|
|
|
|
, mainAttack
|
|
|
|
|
, mainAttackPressed
|
|
|
|
|
, charge
|
2025-01-07 13:31:17 +00:00
|
|
|
, enemies
|
|
|
|
|
, projectiles
|
2024-12-31 10:04:40 +00:00
|
|
|
, ...
|
|
|
|
|
} = player
|
2024-12-23 00:28:53 +00:00
|
|
|
|
2024-12-31 10:04:40 +00:00
|
|
|
val {leftHeld, rightHeld, upHeld, downHeld, attackHeld, chargeHeld} =
|
|
|
|
|
input
|
|
|
|
|
|
|
|
|
|
val xAxis = getXAxis (leftHeld, rightHeld)
|
|
|
|
|
val facing = getFacing (facing, xAxis)
|
2025-01-07 13:31:17 +00:00
|
|
|
|
2024-12-31 10:04:40 +00:00
|
|
|
val charge =
|
|
|
|
|
case mainAttack of
|
2025-01-12 11:46:32 +00:00
|
|
|
MAIN_CHARGING => Int.min (charge + 1, Constants.maxCharge)
|
2024-12-31 10:04:40 +00:00
|
|
|
| MAIN_ATTACKING => Int.max (charge - 1, 0)
|
2025-01-08 13:18:35 +00:00
|
|
|
| _ => charge
|
2024-12-31 10:04:40 +00:00
|
|
|
|
2025-01-08 12:49:29 +00:00
|
|
|
val acc = [W_X_AXIS xAxis, W_FACING facing, W_CHARGE charge]
|
|
|
|
|
|
|
|
|
|
val acc = getMainAttackPatches
|
|
|
|
|
( mainAttack
|
|
|
|
|
, enemies
|
|
|
|
|
, projectiles
|
|
|
|
|
, attackHeld
|
|
|
|
|
, chargeHeld
|
|
|
|
|
, charge
|
|
|
|
|
, player
|
|
|
|
|
, acc
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-31 10:04:40 +00:00
|
|
|
val acc = getJumpPatches (player, upHeld, downHeld, acc)
|
|
|
|
|
in
|
|
|
|
|
acc
|
|
|
|
|
end
|
2024-12-22 20:21:16 +00:00
|
|
|
|
2025-02-05 19:33:56 +00:00
|
|
|
fun getRecoilPatches (player, patches) =
|
2024-12-22 05:40:33 +00:00
|
|
|
case #recoil player of
|
2025-02-05 19:33:56 +00:00
|
|
|
NO_RECOIL => patches
|
2024-12-22 05:40:33 +00:00
|
|
|
| RECOIL_LEFT 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.
|
|
|
|
|
* *)
|
2025-01-12 11:46:32 +00:00
|
|
|
if recoiled = Constants.recoilLimit then
|
2025-02-05 19:33:56 +00:00
|
|
|
W_RECOIL NO_RECOIL :: patches
|
2024-12-22 05:40:33 +00:00
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
val xAxis = STAY_STILL
|
|
|
|
|
val yAxis = FALLING
|
|
|
|
|
val jumpPressed = false
|
|
|
|
|
val recoiled = recoiled + 1
|
|
|
|
|
val recoil = RECOIL_LEFT recoiled
|
|
|
|
|
val facing = getFacing (facing, xAxis)
|
|
|
|
|
in
|
2025-02-05 19:33:56 +00:00
|
|
|
W_X x :: W_X_AXIS xAxis :: W_Y_AXIS yAxis
|
|
|
|
|
:: W_JUMP_PRESSED jumpPressed :: W_RECOIL recoil :: W_FACING facing
|
|
|
|
|
:: patches
|
2024-12-22 05:40:33 +00:00
|
|
|
end
|
|
|
|
|
| RECOIL_RIGHT recoiled =>
|
2025-01-12 11:46:32 +00:00
|
|
|
if recoiled = Constants.recoilLimit then
|
2025-02-05 19:33:56 +00:00
|
|
|
W_RECOIL NO_RECOIL :: patches
|
2024-12-22 05:40:33 +00:00
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val {x, y, health, attacked, facing, xAxis, ...} = player
|
|
|
|
|
val x = x + 5
|
|
|
|
|
|
|
|
|
|
val xAxis = STAY_STILL
|
|
|
|
|
val yAxis = FALLING
|
|
|
|
|
val jumpPressed = false
|
|
|
|
|
val recoiled = recoiled + 1
|
|
|
|
|
val recoil = RECOIL_RIGHT recoiled
|
|
|
|
|
val facing = getFacing (facing, xAxis)
|
|
|
|
|
in
|
2025-02-05 19:33:56 +00:00
|
|
|
W_X x :: W_X_AXIS xAxis :: W_Y_AXIS yAxis
|
|
|
|
|
:: W_JUMP_PRESSED jumpPressed :: W_RECOIL recoil :: W_FACING facing
|
|
|
|
|
:: patches
|
2024-12-22 05:40:33 +00:00
|
|
|
end
|
|
|
|
|
|
2025-01-09 11:08:15 +00:00
|
|
|
fun helpMoveProjectiles (pos, projectiles, acc) =
|
|
|
|
|
if pos < 0 then
|
|
|
|
|
Vector.fromList acc
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val {x, y, facing} = Vector.sub (projectiles, pos)
|
|
|
|
|
in
|
2025-01-12 11:46:32 +00:00
|
|
|
if x <= 0 orelse x >= Constants.worldWidth then
|
2025-01-09 11:08:15 +00:00
|
|
|
(* filter out since projectile is not visible *)
|
|
|
|
|
helpMoveProjectiles (pos - 1, projectiles, acc)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val x =
|
|
|
|
|
case facing of
|
2025-01-13 12:46:10 +00:00
|
|
|
FACING_LEFT => x - Constants.moveProjectileBy
|
|
|
|
|
| FACING_RIGHT => x + Constants.moveProjectileBy
|
2025-01-09 11:08:15 +00:00
|
|
|
|
|
|
|
|
val newTile = {x = x, y = y, facing = facing}
|
|
|
|
|
val acc = newTile :: acc
|
|
|
|
|
in
|
|
|
|
|
helpMoveProjectiles (pos - 1, projectiles, acc)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun getProjectilePatches ({projectiles, ...}) =
|
|
|
|
|
let
|
|
|
|
|
val newProjectiles = helpMoveProjectiles
|
|
|
|
|
(Vector.length projectiles - 1, projectiles, [])
|
|
|
|
|
in
|
|
|
|
|
[W_PROJECTILES newProjectiles]
|
|
|
|
|
end
|
|
|
|
|
|
2024-12-24 12:05:44 +00:00
|
|
|
fun runPhysicsAndInput (game: game_type, input) =
|
2024-12-19 03:58:37 +00:00
|
|
|
let
|
|
|
|
|
val player = #player game
|
2024-12-22 05:40:33 +00:00
|
|
|
|
2025-01-09 11:08:15 +00:00
|
|
|
val patches = getProjectilePatches player
|
2025-02-05 19:33:56 +00:00
|
|
|
val patches = getRecoilPatches (player, patches)
|
2025-01-12 19:25:05 +00:00
|
|
|
val player = PlayerPatch.withPatches (player, patches)
|
2025-01-09 11:08:15 +00:00
|
|
|
|
2025-02-05 19:33:56 +00:00
|
|
|
val patches =
|
|
|
|
|
(* we only accept and handle input if player is not recoiling.
|
|
|
|
|
* It's important to apply the recoil patches after handling input
|
|
|
|
|
* because we want to act on the latest recoil state straight away. *)
|
2024-12-22 05:40:33 +00:00
|
|
|
case #recoil player of
|
2025-02-05 19:33:56 +00:00
|
|
|
NO_RECOIL => getInputPatches (player, input)
|
|
|
|
|
| _ => []
|
2024-12-22 05:40:33 +00:00
|
|
|
|
2025-02-05 19:33:56 +00:00
|
|
|
(* animate projectiles *)
|
2025-01-01 14:17:42 +00:00
|
|
|
val player =
|
|
|
|
|
let
|
|
|
|
|
val e = #enemies player
|
2025-01-07 13:31:17 +00:00
|
|
|
val e =
|
|
|
|
|
Vector.map
|
|
|
|
|
(fn {angle} => {angle = if angle < 360 then angle + 5 else 0}) e
|
2025-02-05 19:33:56 +00:00
|
|
|
val patches = W_ENEMIES e :: patches
|
2025-01-01 14:17:42 +00:00
|
|
|
in
|
2025-01-12 19:25:05 +00:00
|
|
|
PlayerPatch.withPatches (player, patches)
|
2025-01-01 14:17:42 +00:00
|
|
|
end
|
|
|
|
|
|
2025-01-13 09:42:32 +00:00
|
|
|
val patches = PlayerPhysics.getPhysicsPatches player
|
2025-01-12 19:25:05 +00:00
|
|
|
val player = PlayerPatch.withPatches (player, patches)
|
2024-12-22 05:40:33 +00:00
|
|
|
|
2025-01-13 09:42:32 +00:00
|
|
|
val {walls, wallTree, platforms, platformTree, ...} = game
|
|
|
|
|
val patches = PlayerPhysics.getEnvironmentPatches
|
|
|
|
|
(player, walls, wallTree, platforms, platformTree)
|
2024-12-22 05:40:33 +00:00
|
|
|
in
|
2025-01-12 19:25:05 +00:00
|
|
|
PlayerPatch.withPatches (player, patches)
|
2024-12-19 03:58:37 +00:00
|
|
|
end
|
|
|
|
|
|
2025-01-12 21:59:33 +00:00
|
|
|
fun concatAttackedEnemies (player: player, enemyCollisions) =
|
|
|
|
|
let
|
|
|
|
|
val newDefeated = Vector.map (fn id => {angle = 360}) enemyCollisions
|
|
|
|
|
val oldDefeated = #enemies player
|
|
|
|
|
val allDefeated = Vector.concat [oldDefeated, newDefeated]
|
|
|
|
|
in
|
|
|
|
|
PlayerPatch.withPatch (player, W_ENEMIES allDefeated)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun getEnemyRecoilPatches (player, playerOnRight, acc) =
|
|
|
|
|
if playerOnRight then
|
|
|
|
|
let
|
|
|
|
|
val newRecoil = RECOIL_RIGHT 0
|
|
|
|
|
val newAttacked = ATTACKED 0
|
|
|
|
|
in
|
|
|
|
|
W_RECOIL newRecoil :: W_ATTACKED newAttacked :: W_FACING FACING_LEFT
|
|
|
|
|
:: W_Y_AXIS FALLING :: W_X_AXIS STAY_STILL :: acc
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val newRecoil = RECOIL_LEFT 0
|
|
|
|
|
val newAttacked = ATTACKED 0
|
|
|
|
|
in
|
|
|
|
|
W_RECOIL newRecoil :: W_ATTACKED newAttacked :: W_FACING FACING_RIGHT
|
|
|
|
|
:: W_Y_AXIS FALLING :: W_X_AXIS STAY_STILL :: acc
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun enemyCollisionReaction (player: player, enemies: enemy vector, lst, acc) =
|
|
|
|
|
case lst of
|
|
|
|
|
id :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val playerOnRight =
|
|
|
|
|
(* check if collision is closer to left side of enemy or right
|
|
|
|
|
* and then chose appropriate direction to recoil in *)
|
|
|
|
|
let
|
|
|
|
|
val {x, ...} = player
|
|
|
|
|
val pFinishX = x + Constants.playerSize
|
|
|
|
|
val pHalfW = Constants.playerSize div 2
|
|
|
|
|
val pCentreX = x + pHalfW
|
|
|
|
|
|
|
|
|
|
val {x = ex, y = ey, ...} = Enemy.find (id, enemies)
|
|
|
|
|
val eFinishX = ex + Constants.enemySize
|
|
|
|
|
val eHalfW = Constants.enemySize div 2
|
|
|
|
|
val eCentreX = ex + eHalfW
|
|
|
|
|
in
|
|
|
|
|
eCentreX < pCentreX
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
val acc = getEnemyRecoilPatches (player, playerOnRight, acc)
|
|
|
|
|
in
|
|
|
|
|
enemyCollisionReaction (player, enemies, tl, acc)
|
|
|
|
|
end
|
|
|
|
|
| [] => PlayerPatch.withPatches (player, acc)
|
|
|
|
|
|
2025-01-12 22:10:58 +00:00
|
|
|
fun incrementAttacked (player, amt) =
|
|
|
|
|
let val patch = ATTACKED (amt + 1)
|
|
|
|
|
in PlayerPatch.withPatch (player, W_ATTACKED patch)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun exitAttackedAndCheckEnemies (player, enemies, enemyCollisions) =
|
|
|
|
|
enemyCollisionReaction
|
|
|
|
|
(player, enemies, enemyCollisions, [W_ATTACKED NOT_ATTACKED])
|
|
|
|
|
|
|
|
|
|
fun getEnemyCollisionsWhenAttacking (x, y, enemyTree) =
|
|
|
|
|
let
|
|
|
|
|
val x = x - Constants.halfPlayerSize
|
|
|
|
|
val y = y - Constants.halfPlayerSize
|
|
|
|
|
val size = Constants.playerSize * 2
|
|
|
|
|
|
|
|
|
|
val ww = Constants.worldWidth
|
|
|
|
|
val wh = Constants.worldHeight
|
2025-01-27 23:41:59 +00:00
|
|
|
val enemyCollisions = QuadTree.getCollisions
|
|
|
|
|
(x, y, size, size, ~1, enemyTree)
|
2025-01-12 22:10:58 +00:00
|
|
|
in
|
|
|
|
|
Vector.fromList enemyCollisions
|
|
|
|
|
end
|
|
|
|
|
|
2024-12-16 00:58:59 +00:00
|
|
|
(* block is placeholder asset *)
|
2024-12-22 21:08:59 +00:00
|
|
|
fun helpGetDrawVec (x, y, size, width, height, attacked, mainAttack) =
|
|
|
|
|
case mainAttack of
|
|
|
|
|
MAIN_NOT_ATTACKING =>
|
|
|
|
|
(case attacked of
|
|
|
|
|
NOT_ATTACKED =>
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 0.5, 0.5, 0.5)
|
|
|
|
|
| ATTACKED amt =>
|
|
|
|
|
if amt mod 5 = 0 then
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 0.9, 0.9, 0.9)
|
2025-01-08 13:18:35 +00:00
|
|
|
else
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 0.5, 0.5, 0.5))
|
|
|
|
|
| MAIN_THROWING =>
|
|
|
|
|
(case attacked of
|
|
|
|
|
NOT_ATTACKED =>
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 0.5, 0.5, 0.5)
|
|
|
|
|
| ATTACKED amt =>
|
|
|
|
|
if amt mod 5 = 0 then
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 0.9, 0.9, 0.9)
|
2024-12-22 21:08:59 +00:00
|
|
|
else
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 0.5, 0.5, 0.5))
|
2024-12-31 10:04:40 +00:00
|
|
|
| MAIN_ATTACKING =>
|
|
|
|
|
(case attacked of
|
|
|
|
|
NOT_ATTACKED =>
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 1.0, 0.5, 0.5)
|
|
|
|
|
| ATTACKED amt =>
|
|
|
|
|
if amt mod 5 = 0 then
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 1.0, 0.9, 0.9)
|
|
|
|
|
else
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 1.0, 0.5, 0.5))
|
|
|
|
|
| MAIN_CHARGING =>
|
2024-12-22 21:08:59 +00:00
|
|
|
(case attacked of
|
|
|
|
|
NOT_ATTACKED =>
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 1.0, 0.5, 0.5)
|
|
|
|
|
| ATTACKED amt =>
|
|
|
|
|
if amt mod 5 = 0 then
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 1.0, 0.9, 0.9)
|
|
|
|
|
else
|
|
|
|
|
Block.lerp (x, y, size, size, width, height, 1.0, 0.5, 0.5))
|
|
|
|
|
|
|
|
|
|
fun getDrawVec (player: player, width, height) =
|
2024-12-17 09:16:22 +00:00
|
|
|
let
|
2024-12-22 21:08:59 +00:00
|
|
|
val {x, y, attacked, mainAttack, ...} = player
|
2025-01-12 11:46:32 +00:00
|
|
|
val wratio = width / Constants.worldWidthReal
|
|
|
|
|
val hratio = height / Constants.worldHeightReal
|
2024-12-17 09:16:22 +00:00
|
|
|
in
|
|
|
|
|
if wratio < hratio then
|
|
|
|
|
let
|
2025-01-12 11:46:32 +00:00
|
|
|
val scale = Constants.worldHeightReal * wratio
|
2024-12-17 09:16:22 +00:00
|
|
|
val yOffset =
|
|
|
|
|
if height > scale then (height - scale) / 2.0
|
|
|
|
|
else if height < scale then (scale - height) / 2.0
|
|
|
|
|
else 0.0
|
|
|
|
|
|
|
|
|
|
val x = Real32.fromInt x * wratio
|
|
|
|
|
val y = Real32.fromInt y * wratio + yOffset
|
|
|
|
|
|
2025-01-12 11:46:32 +00:00
|
|
|
val realSize = Constants.playerSizeReal * wratio
|
2024-12-17 09:16:22 +00:00
|
|
|
in
|
2024-12-22 21:08:59 +00:00
|
|
|
helpGetDrawVec (x, y, realSize, width, height, attacked, mainAttack)
|
2024-12-17 09:16:22 +00:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
2025-01-12 11:46:32 +00:00
|
|
|
val scale = Constants.worldWidthReal * hratio
|
2024-12-17 09:16:22 +00:00
|
|
|
val xOffset =
|
|
|
|
|
if width > scale then (width - scale) / 2.0
|
|
|
|
|
else if width < scale then (scale - width) / 2.0
|
|
|
|
|
else 0.0
|
|
|
|
|
|
|
|
|
|
val x = Real32.fromInt x * hratio + xOffset
|
|
|
|
|
val y = Real32.fromInt y * hratio
|
|
|
|
|
|
2025-01-12 11:46:32 +00:00
|
|
|
val realSize = Constants.playerSizeReal * hratio
|
2024-12-17 09:16:22 +00:00
|
|
|
in
|
2024-12-22 21:08:59 +00:00
|
|
|
helpGetDrawVec (x, y, realSize, width, height, attacked, mainAttack)
|
2024-12-17 09:16:22 +00:00
|
|
|
end
|
|
|
|
|
end
|
2024-12-28 04:06:27 +00:00
|
|
|
|
|
|
|
|
fun getFieldVec (player: player, width, height) =
|
|
|
|
|
case #mainAttack player of
|
|
|
|
|
MAIN_NOT_ATTACKING => Vector.fromList []
|
2025-01-09 10:38:34 +00:00
|
|
|
| MAIN_THROWING => Vector.fromList []
|
2024-12-31 10:04:40 +00:00
|
|
|
| _ =>
|
2024-12-28 04:06:27 +00:00
|
|
|
let
|
|
|
|
|
val {x, y, ...} = player
|
2025-01-12 11:46:32 +00:00
|
|
|
val wratio = width / Constants.worldWidthReal
|
|
|
|
|
val hratio = height / Constants.worldHeightReal
|
2024-12-28 04:06:27 +00:00
|
|
|
in
|
|
|
|
|
if wratio < hratio then
|
|
|
|
|
let
|
2025-01-12 11:46:32 +00:00
|
|
|
val scale = Constants.worldHeightReal * wratio
|
2024-12-28 04:06:27 +00:00
|
|
|
val yOffset =
|
|
|
|
|
if height > scale then (height - scale) / 2.0
|
|
|
|
|
else if height < scale then (scale - height) / 2.0
|
|
|
|
|
else 0.0
|
|
|
|
|
|
2025-01-12 11:46:32 +00:00
|
|
|
val x = (Real32.fromInt x - Constants.halfPlayerSizeReal) * wratio
|
|
|
|
|
val y =
|
|
|
|
|
(Real32.fromInt y - Constants.halfPlayerSizeReal) * wratio
|
|
|
|
|
+ yOffset
|
2024-12-28 04:06:27 +00:00
|
|
|
|
2025-01-12 11:46:32 +00:00
|
|
|
val realSize = (Constants.playerSizeReal * 2.0) * wratio
|
2024-12-31 10:08:18 +00:00
|
|
|
|
|
|
|
|
val {charge, ...} = player
|
|
|
|
|
val alpha = Real32.fromInt charge / 60.0
|
2024-12-28 04:06:27 +00:00
|
|
|
in
|
|
|
|
|
Field.lerp
|
|
|
|
|
(x, y, realSize, realSize, width, height, 0.7, 0.7, 1.0, alpha)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
2025-01-12 11:46:32 +00:00
|
|
|
val scale = Constants.worldWidthReal * hratio
|
2024-12-28 04:06:27 +00:00
|
|
|
val xOffset =
|
|
|
|
|
if width > scale then (width - scale) / 2.0
|
|
|
|
|
else if width < scale then (scale - width) / 2.0
|
|
|
|
|
else 0.0
|
|
|
|
|
|
2025-01-12 11:46:32 +00:00
|
|
|
val x =
|
|
|
|
|
(Real32.fromInt x - Constants.halfPlayerSizeReal) * hratio
|
|
|
|
|
+ xOffset
|
|
|
|
|
val y = (Real32.fromInt y - Constants.halfPlayerSizeReal) * hratio
|
2024-12-28 04:06:27 +00:00
|
|
|
|
2025-01-12 11:46:32 +00:00
|
|
|
val realSize = (Constants.playerSizeReal * 2.0) * hratio
|
2024-12-31 10:08:18 +00:00
|
|
|
|
|
|
|
|
val {charge, ...} = player
|
|
|
|
|
val alpha = Real32.fromInt charge / 60.0
|
2024-12-28 04:06:27 +00:00
|
|
|
in
|
|
|
|
|
Field.lerp
|
|
|
|
|
(x, y, realSize, realSize, width, height, 0.7, 0.7, 1.0, alpha)
|
|
|
|
|
end
|
|
|
|
|
end
|
2025-01-01 14:17:42 +00:00
|
|
|
|
|
|
|
|
fun helpGetPelletVec
|
|
|
|
|
( playerX
|
|
|
|
|
, playerY
|
|
|
|
|
, pos
|
|
|
|
|
, enemies
|
|
|
|
|
, width
|
|
|
|
|
, height
|
|
|
|
|
, ratio
|
|
|
|
|
, xOffset
|
|
|
|
|
, yOffset
|
|
|
|
|
, acc
|
|
|
|
|
) =
|
|
|
|
|
if pos = Vector.length enemies then
|
|
|
|
|
Vector.concat acc
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val {angle} = Vector.sub (enemies, pos)
|
|
|
|
|
(* convert degrees to radians *)
|
|
|
|
|
val angle = degreesToRadians angle
|
|
|
|
|
|
|
|
|
|
(* calculate pellet's x and y *)
|
2025-01-12 07:18:44 +00:00
|
|
|
val pelletX =
|
|
|
|
|
((Real32.Math.cos angle) * Constants.projectileDistance) + playerX
|
2025-01-01 14:17:42 +00:00
|
|
|
val pelletX = pelletX * ratio + xOffset
|
|
|
|
|
|
2025-01-12 07:18:44 +00:00
|
|
|
val pelletY =
|
|
|
|
|
((Real32.Math.sin angle) * Constants.projectileDistance) + playerY
|
2025-01-01 14:17:42 +00:00
|
|
|
val pelletY = pelletY * ratio + yOffset
|
|
|
|
|
|
2025-01-12 07:18:44 +00:00
|
|
|
val defeatedSize = Constants.projectileSize * ratio
|
2025-01-07 13:03:17 +00:00
|
|
|
|
2025-01-01 14:17:42 +00:00
|
|
|
val vec = Field.lerp
|
|
|
|
|
( pelletX
|
|
|
|
|
, pelletY
|
|
|
|
|
, defeatedSize
|
|
|
|
|
, defeatedSize
|
|
|
|
|
, width
|
|
|
|
|
, height
|
|
|
|
|
, 0.3
|
|
|
|
|
, 0.9
|
|
|
|
|
, 0.3
|
|
|
|
|
, 1.0
|
|
|
|
|
)
|
|
|
|
|
val acc = vec :: acc
|
|
|
|
|
in
|
|
|
|
|
helpGetPelletVec
|
|
|
|
|
( playerX
|
|
|
|
|
, playerY
|
|
|
|
|
, pos + 1
|
|
|
|
|
, enemies
|
|
|
|
|
, width
|
|
|
|
|
, height
|
|
|
|
|
, ratio
|
|
|
|
|
, xOffset
|
|
|
|
|
, yOffset
|
|
|
|
|
, acc
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun getPelletVec (player: player, width, height) =
|
|
|
|
|
if Vector.length (#enemies player) = 0 then
|
|
|
|
|
Vector.fromList []
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val {x, y, enemies, ...} = player
|
|
|
|
|
|
|
|
|
|
(* get centre (x, y) coordinates of player *)
|
2025-01-12 11:46:32 +00:00
|
|
|
val diff =
|
|
|
|
|
Constants.halfPlayerSizeReal - (Constants.projectileSize / 2.0)
|
2025-01-01 14:17:42 +00:00
|
|
|
val x = Real32.fromInt x + diff
|
|
|
|
|
val y = Real32.fromInt y + diff
|
|
|
|
|
|
2025-01-12 11:46:32 +00:00
|
|
|
val wratio = width / Constants.worldWidthReal
|
|
|
|
|
val hratio = height / Constants.worldHeightReal
|
2025-01-01 14:17:42 +00:00
|
|
|
in
|
|
|
|
|
if wratio < hratio then
|
|
|
|
|
let
|
2025-01-12 11:46:32 +00:00
|
|
|
val scale = Constants.worldHeightReal * wratio
|
2025-01-01 14:17:42 +00:00
|
|
|
val yOffset =
|
|
|
|
|
if height > scale then (height - scale) / 2.0
|
|
|
|
|
else if height < scale then (scale - height) / 2.0
|
|
|
|
|
else 0.0
|
2025-01-07 13:31:17 +00:00
|
|
|
in
|
2025-01-01 14:17:42 +00:00
|
|
|
helpGetPelletVec
|
|
|
|
|
(x, y, 0, enemies, width, height, wratio, 0.0, yOffset, [])
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
2025-01-12 11:46:32 +00:00
|
|
|
val scale = Constants.worldWidthReal * hratio
|
2025-01-01 14:17:42 +00:00
|
|
|
val xOffset =
|
|
|
|
|
if width > scale then (width - scale) / 2.0
|
|
|
|
|
else if width < scale then (scale - width) / 2.0
|
|
|
|
|
else 0.0
|
|
|
|
|
in
|
|
|
|
|
helpGetPelletVec
|
|
|
|
|
(x, y, 0, enemies, width, height, hratio, xOffset, 0.0, [])
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-12-09 04:37:40 +00:00
|
|
|
end
|