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 , 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)
(* update state of falling enemies and possibly filter *) (* update state of falling enemies and possibly filter *)
(* todo: use enemy map (* todo: use enemy map
val fallingEnemies = FallingEnemies.updateList val fallingEnemies = FallingEnemies.updateList
(Vector.length fallingEnemies - 1, fallingEnemies, player, []) (Vector.length fallingEnemies - 1, fallingEnemies, player, [])
val fallingEnemies = Vector.fromList fallingEnemies val fallingEnemies = Vector.fromList fallingEnemies
*) *)
in in
{ player = player { player = player
, walls = walls , walls = walls

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 = []
in val patches = FoldEnemies.foldRegion
FoldEnemies.foldRegion (x, y, size, size, env, state, enemyTree) (x, y, size, size, env, state, enemyTree)
end 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 = 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
@@ -452,18 +459,18 @@ struct
(case facing of (case facing of
FACING_RIGHT => x + Constants.playerSize FACING_RIGHT => x + Constants.playerSize
| FACING_LEFT => x - length) | FACING_LEFT => x - length)
val state = [] val state = []
(* detect collisions from enemies who are hit by attack *) (* detect collisions from enemies who are hit by attack *)
val newDefeated = AttackEnemies.foldRegion val newDefeated = AttackEnemies.foldRegion
(x, y, length, height, (), state, enemyTree) (x, y, length, height, (), state, enemyTree)
(* detect collisions from falling enemies too *) (* detect collisions from falling enemies too *)
val fallingTree = val fallingTree =
FallingEnemies.generateTree (#fallingEnemies game) FallingEnemies.generateTree (#fallingEnemies game)
val newDefeated = AttackEnemies.foldRegion val newDefeated = AttackEnemies.foldRegion
(x, y, length, height, (), newDefeated, fallingTree) (x, y, length, height, (), newDefeated, fallingTree)
val allDefeated = val allDefeated =
Vector.concat [Vector.fromList newDefeated, enemies] Vector.concat [Vector.fromList newDefeated, enemies]
in in
@@ -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) =