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,19 +13,21 @@ struct
, fallingEnemies
} = game
val player = Player.runPhysicsAndInput (game, input)
val enemyTree = Enemy.generateTree enemies
val player = Player.runPhysicsAndInput (game, input, enemyTree)
val player = Player.checkEnemyCollisions (player, enemies, enemyTree)
val enemies = Enemy.update
(enemies, walls, wallTree, platforms, platformTree, player, graph)
(* update state of falling enemies and possibly filter *)
(* todo: use enemy map
val fallingEnemies = FallingEnemies.updateList
(Vector.length fallingEnemies - 1, fallingEnemies, player, [])
(* update state of falling enemies and possibly filter *)
(* todo: use enemy map
val fallingEnemies = FallingEnemies.updateList
(Vector.length fallingEnemies - 1, fallingEnemies, player, [])
val fallingEnemies = Vector.fromList fallingEnemies
*)
val fallingEnemies = Vector.fromList fallingEnemies
*)
in
{ player = player
, walls = walls

View File

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