a little refactoring (Player.runPhysicsAndInput function now does just that, instead of also checking for collisions with enemy)

This commit is contained in:
2025-02-14 11:19:24 +00:00
parent e00db5d8a3
commit 50b57b9ef9
2 changed files with 38 additions and 28 deletions

View File

@@ -13,8 +13,10 @@ struct
, fallingEnemies , fallingEnemies
} = game } = game
val player = Player.runPhysicsAndInput (game, input)
val enemyTree = Enemy.generateTree enemies val enemyTree = Enemy.generateTree enemies
val player = Player.runPhysicsAndInput (game, input, enemyTree) val player = Player.checkEnemyCollisions (player, enemies, enemyTree)
val enemies = Enemy.update val enemies = Enemy.update
(enemies, walls, wallTree, platforms, platformTree, player, graph) (enemies, walls, wallTree, platforms, platformTree, player, graph)

View File

@@ -378,7 +378,7 @@ struct
fun fold (_, (), defeatedList) = {angle = 1} :: defeatedList fun fold (_, (), defeatedList) = {angle = 1} :: defeatedList
end) end)
fun runPhysicsAndInput (game: GameType.game_type, input, enemyTree) = fun runPhysicsAndInput (game: GameType.game_type, input) =
let let
val player = #player game val player = #player game
@@ -423,24 +423,31 @@ struct
val {walls, wallTree, platforms, platformTree, ...} = game val {walls, wallTree, platforms, platformTree, ...} = game
val patches = PlayerPhysics.getEnvironmentPatches val patches = PlayerPhysics.getEnvironmentPatches
(player, walls, wallTree, platforms, platformTree) (player, walls, wallTree, platforms, platformTree)
val player = PlayerPatch.withPatches (player, patches) in
PlayerPatch.withPatches (player, patches)
end
val patches =
(* player reaction to collisions with enemies. (* player reaction to collisions with enemies.
* We only detect collisions if player is not in invincibility period * We only detect collisions if player is not in invincibility period
* after being previously attacked. *) * after being previously attacked. *)
fun checkEnemyCollisions (player: PlayerType.player, enemies, enemyTree) =
case #attacked player of case #attacked player of
ATTACKED _ => [] ATTACKED _ => player
| _ => | _ =>
let let
val {x, y, ...} = player val {x, y, ...} = player
val size = Constants.playerSize val size = Constants.playerSize
val env = (#enemies game, player) val env = (enemies, player)
val state = [] val state = []
val patches = FoldEnemies.foldRegion
(x, y, size, size, env, state, enemyTree)
in in
FoldEnemies.foldRegion (x, y, size, size, env, state, enemyTree) PlayerPatch.withPatches (player, patches)
end end
(* todo: check which enemies are being attacked by player,
* updating player's 'defeatedEnemies' field (if enemy's health would reach 0)
* and updating enemy (if enemy's health wouldn't reach 0, decrement health)
val patches = val patches =
(* if player is attacking, check if enemies collide with attack *) (* if player is attacking, check if enemies collide with attack *)
case #mainAttack player of case #mainAttack player of
@@ -473,6 +480,7 @@ struct
in in
PlayerPatch.withPatches (player, patches) PlayerPatch.withPatches (player, patches)
end end
*)
(* todo: add attacked enemies to player's 'enemies' field *) (* todo: add attacked enemies to player's 'enemies' field *)
fun concatAttackedEnemies (player: player, enemyCollisions) = fun concatAttackedEnemies (player: player, enemyCollisions) =