done getting physics + environment to work on enemy as well, in addition to player

This commit is contained in:
2025-01-13 09:52:25 +00:00
parent 4484eb0ef0
commit 50238b8dac
3 changed files with 88 additions and 15 deletions

View File

@@ -12,7 +12,8 @@ struct
(* called when filtering enemies, (* called when filtering enemies,
* to adjust enemy data on collision with projectile *) * to adjust enemy data on collision with projectile *)
fun onCollisionWithProjectile (enemy, projectileTree, acc) = fun onCollisionWithProjectile
(enemy, projectileTree, acc, walls, wallTree, platforms, platformTree) =
let let
val {x, y, health, id, xAxis, yAxis} = enemy val {x, y, health, id, xAxis, yAxis} = enemy
@@ -32,6 +33,10 @@ struct
val patches = EnemyPhysics.getPhysicsPatches enemy val patches = EnemyPhysics.getPhysicsPatches enemy
val patches = EnemyPatch.W_HEALTH (health - 1) :: patches val patches = EnemyPatch.W_HEALTH (health - 1) :: patches
val enemy = EnemyPatch.withPatches (enemy, patches) val enemy = EnemyPatch.withPatches (enemy, patches)
val patches = EnemyPhysics.getEnvironmentPatches
(enemy, walls, wallTree, platforms, platformTree)
val enemy = EnemyPatch.withPatches (enemy, patches)
in in
enemy :: acc enemy :: acc
end end
@@ -39,37 +44,90 @@ struct
let let
val patches = EnemyPhysics.getPhysicsPatches enemy val patches = EnemyPhysics.getPhysicsPatches enemy
val enemy = EnemyPatch.withPatches (enemy, patches) val enemy = EnemyPatch.withPatches (enemy, patches)
val patches = EnemyPhysics.getEnvironmentPatches
(enemy, walls, wallTree, platforms, platformTree)
val enemy = EnemyPatch.withPatches (enemy, patches)
in in
enemy :: acc enemy :: acc
end end
end end
(* filter enemy projectiles when player is not attacking *) (* filter enemy projectiles when player is not attacking *)
fun filterProjectileCollisions (pos, enemies, projectileTree, acc) = fun filterProjectileCollisions
( pos
, enemies
, projectileTree
, acc
, walls
, wallTree
, platforms
, platformTree
) =
if pos < 0 then if pos < 0 then
Vector.fromList acc Vector.fromList acc
else else
let let
val enemy = Vector.sub (enemies, pos) val enemy = Vector.sub (enemies, pos)
val acc = onCollisionWithProjectile (enemy, projectileTree, acc) val acc = onCollisionWithProjectile
(enemy, projectileTree, acc, walls, wallTree, platforms, platformTree)
in in
filterProjectileCollisions (pos - 1, enemies, projectileTree, acc) filterProjectileCollisions
( pos - 1
, enemies
, projectileTree
, acc
, walls
, wallTree
, platforms
, platformTree
)
end end
(* removes enemies from `enemies` vector when player is attacking that enemy (* removes enemies from `enemies` vector when player is attacking that enemy
* and also filter enemy (or change enemyh health) * and also filter enemy (or change enemyh health)
* if enemy has collided with projectile *) * if enemy has collided with projectile *)
fun filterWhenAttacked (pos, collisions, enemies, projectileTree, acc) = fun filterWhenAttacked
( pos
, collisions
, enemies
, projectileTree
, acc
, walls
, wallTree
, platforms
, platformTree
) =
if pos < 0 then if pos < 0 then
Vector.fromList acc Vector.fromList acc
else else
let let
val enemy = Vector.sub (enemies, pos) val enemy = Vector.sub (enemies, pos)
val acc = val acc =
if exists (#id enemy, collisions) then (* filter out *) acc if exists (#id enemy, collisions) then (* filter out *)
else onCollisionWithProjectile (enemy, projectileTree, acc) acc
else
onCollisionWithProjectile
( enemy
, projectileTree
, acc
, walls
, wallTree
, platforms
, platformTree
)
in in
filterWhenAttacked (pos - 1, collisions, enemies, projectileTree, acc) filterWhenAttacked
( pos - 1
, collisions
, enemies
, projectileTree
, acc
, walls
, wallTree
, platforms
, platformTree
)
end end
fun helpGenerateTree (pos, enemyVec, acc) = fun helpGenerateTree (pos, enemyVec, acc) =

View File

@@ -9,9 +9,16 @@ struct
val enemyTree = Enemy.generateTree enemies val enemyTree = Enemy.generateTree enemies
(* check player-enemy collisions and react *) (* check player-enemy collisions and react *)
val (player, enemies) = val (player, enemies) = PlayerEnemy.checkCollisions
PlayerEnemy.checkCollisions ( player
(player, enemies, enemyTree, #projectiles player) , enemies
, enemyTree
, #projectiles player
, walls
, wallTree
, platforms
, platformTree
)
in in
{ player = player { player = player
, walls = walls , walls = walls

View File

@@ -3,7 +3,8 @@ struct
open GameType open GameType
open PlayerPatch open PlayerPatch
fun checkCollisions (player, enemies, enemyTree, projectiles) = fun checkCollisions (player, enemies, enemyTree, projectiles, walls, wallTree,
platforms, platformTree) =
let let
val {x, y, mainAttack, attacked, ...} = player val {x, y, mainAttack, attacked, ...} = player
val size = Constants.playerSize val size = Constants.playerSize
@@ -21,6 +22,10 @@ struct
, enemies , enemies
, projectileTree , projectileTree
, [] , []
, walls
, wallTree
, platforms
, platformTree
) )
(* add collided enemies to player record, (* add collided enemies to player record,
@@ -41,7 +46,8 @@ struct
(player, enemies, enemyCollisions, []) (player, enemies, enemyCollisions, [])
val enemies = Enemy.filterProjectileCollisions val enemies = Enemy.filterProjectileCollisions
(Vector.length enemies - 1, enemies, projectileTree, []) (Vector.length enemies - 1, enemies, projectileTree, [],
walls, wallTree, platforms, platformTree)
in in
(player, enemies) (player, enemies)
end end
@@ -57,7 +63,8 @@ struct
(player, enemies, enemyCollisions) (player, enemies, enemyCollisions)
val enemies = Enemy.filterProjectileCollisions val enemies = Enemy.filterProjectileCollisions
(Vector.length enemies - 1, enemies, projectileTree, []) (Vector.length enemies - 1, enemies, projectileTree, [],
walls, wallTree, platforms, platformTree)
in in
(player, enemies) (player, enemies)
end end
@@ -68,7 +75,8 @@ struct
let let
val player = Player.incrementAttacked (player, amt) val player = Player.incrementAttacked (player, amt)
val enemies = Enemy.filterProjectileCollisions val enemies = Enemy.filterProjectileCollisions
(Vector.length enemies - 1, enemies, projectileTree, []) (Vector.length enemies - 1, enemies, projectileTree, [],
walls, wallTree, platforms, platformTree)
in in
(player, enemies) (player, enemies)
end) end)