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

View File

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

View File

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