2025-01-10 10:12:59 +00:00
|
|
|
structure PlayerEnemy =
|
|
|
|
|
struct
|
|
|
|
|
open GameType
|
2025-01-12 19:25:05 +00:00
|
|
|
open PlayerPatch
|
2025-01-10 10:12:59 +00:00
|
|
|
|
|
|
|
|
fun getEnemyRecoilPatches (player, playerOnRight, acc) =
|
|
|
|
|
if playerOnRight then
|
|
|
|
|
let
|
|
|
|
|
val newRecoil = RECOIL_RIGHT 0
|
|
|
|
|
val newAttacked = ATTACKED 0
|
|
|
|
|
in
|
|
|
|
|
W_RECOIL newRecoil :: W_ATTACKED newAttacked :: W_FACING FACING_LEFT
|
|
|
|
|
:: W_Y_AXIS FALLING :: W_X_AXIS STAY_STILL :: acc
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val newRecoil = RECOIL_LEFT 0
|
|
|
|
|
val newAttacked = ATTACKED 0
|
|
|
|
|
in
|
|
|
|
|
W_RECOIL newRecoil :: W_ATTACKED newAttacked :: W_FACING FACING_RIGHT
|
|
|
|
|
:: W_Y_AXIS FALLING :: W_X_AXIS STAY_STILL :: acc
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun checkEnemies (player: player, enemies: enemy vector, lst, acc) =
|
|
|
|
|
case lst of
|
|
|
|
|
id :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val playerOnRight =
|
|
|
|
|
(* check if collision is closer to left side of enemy or right
|
|
|
|
|
* and then chose appropriate direction to recoil in *)
|
|
|
|
|
let
|
|
|
|
|
val {x, ...} = player
|
2025-01-12 11:46:32 +00:00
|
|
|
val pFinishX = x + Constants.playerSize
|
|
|
|
|
val pHalfW = Constants.playerSize div 2
|
2025-01-10 10:12:59 +00:00
|
|
|
val pCentreX = x + pHalfW
|
|
|
|
|
|
|
|
|
|
val {x = ex, y = ey, ...} = Enemy.find (id, enemies)
|
2025-01-12 12:53:41 +00:00
|
|
|
val eFinishX = ex + Constants.enemySize
|
|
|
|
|
val eHalfW = Constants.enemySize div 2
|
2025-01-10 10:12:59 +00:00
|
|
|
val eCentreX = ex + eHalfW
|
|
|
|
|
in
|
|
|
|
|
eCentreX < pCentreX
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
val acc = getEnemyRecoilPatches (player, playerOnRight, acc)
|
|
|
|
|
in
|
|
|
|
|
checkEnemies (player, enemies, tl, acc)
|
|
|
|
|
end
|
|
|
|
|
| [] => acc
|
|
|
|
|
|
2025-01-11 13:45:29 +00:00
|
|
|
fun helpExists (pos, id, collisions) =
|
|
|
|
|
if pos = Vector.length collisions then
|
|
|
|
|
false
|
|
|
|
|
else
|
|
|
|
|
let val current = Vector.sub (collisions, pos)
|
|
|
|
|
in current = id orelse helpExists (pos + 1, id, collisions)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun exists (id, collisions) = helpExists (0, id, collisions)
|
2025-01-10 10:12:59 +00:00
|
|
|
|
2025-01-11 21:35:55 +00:00
|
|
|
(* 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 filterEnemyAttacked
|
|
|
|
|
(pos, collisions, enemies: enemy vector, projectileTree, acc) =
|
2025-01-10 10:12:59 +00:00
|
|
|
if pos < 0 then
|
|
|
|
|
Vector.fromList acc
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val enemy = Vector.sub (enemies, pos)
|
2025-01-11 13:45:29 +00:00
|
|
|
val acc =
|
2025-01-11 21:58:34 +00:00
|
|
|
if exists (#id enemy, collisions) then (* filter out *) acc
|
|
|
|
|
else Enemy.onCollisionWithProjectile (enemy, projectileTree, acc)
|
2025-01-11 21:35:55 +00:00
|
|
|
in
|
2025-01-11 21:58:34 +00:00
|
|
|
filterEnemyAttacked (pos - 1, collisions, enemies, projectileTree, acc)
|
2025-01-11 21:35:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
(* filter enemy projectiles when player is not attacking *)
|
|
|
|
|
fun filterEnemyProjectiles (pos, enemies, projectileTree, acc) =
|
|
|
|
|
if pos < 0 then
|
|
|
|
|
Vector.fromList acc
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val enemy = Vector.sub (enemies, pos)
|
2025-01-11 21:58:34 +00:00
|
|
|
val acc = Enemy.onCollisionWithProjectile (enemy, projectileTree, acc)
|
2025-01-10 10:12:59 +00:00
|
|
|
in
|
2025-01-11 21:35:55 +00:00
|
|
|
filterEnemyProjectiles (pos - 1, enemies, projectileTree, acc)
|
2025-01-10 10:12:59 +00:00
|
|
|
end
|
|
|
|
|
|
2025-01-11 21:35:55 +00:00
|
|
|
fun checkCollisions (player, enemies, enemyTree, projectiles) =
|
2025-01-10 10:12:59 +00:00
|
|
|
let
|
|
|
|
|
val {x, y, mainAttack, attacked, ...} = player
|
2025-01-12 11:46:32 +00:00
|
|
|
val size = Constants.playerSize
|
2025-01-11 21:35:55 +00:00
|
|
|
val projectileTree = Projectile.generateTree projectiles
|
2025-01-10 10:12:59 +00:00
|
|
|
in
|
|
|
|
|
case mainAttack of
|
|
|
|
|
MAIN_ATTACKING =>
|
|
|
|
|
let
|
|
|
|
|
(* when attacking, player collision should be larger than player themselves *)
|
2025-01-12 11:46:32 +00:00
|
|
|
val x = x - Constants.halfPlayerSize
|
|
|
|
|
val y = y - Constants.halfPlayerSize
|
2025-01-10 10:12:59 +00:00
|
|
|
val size = size * 2
|
|
|
|
|
|
2025-01-11 13:45:29 +00:00
|
|
|
(* get list of enemies player has collided with *)
|
2025-01-10 10:12:59 +00:00
|
|
|
val enemyCollisions = QuadTree.getCollisions
|
|
|
|
|
(x, y, size, size, 0, 0, 1920, 1080, 0, enemyTree)
|
|
|
|
|
|
2025-01-11 13:45:29 +00:00
|
|
|
(* filter enemies based on collisions *)
|
2025-01-10 10:12:59 +00:00
|
|
|
val enemyCollisions = Vector.fromList enemyCollisions
|
2025-01-11 21:35:55 +00:00
|
|
|
val enemies = filterEnemyAttacked
|
|
|
|
|
( Vector.length enemies - 1
|
|
|
|
|
, enemyCollisions
|
|
|
|
|
, enemies
|
|
|
|
|
, projectileTree
|
|
|
|
|
, []
|
|
|
|
|
)
|
2025-01-11 13:45:29 +00:00
|
|
|
val enemyTree = Enemy.generateTree enemies
|
2025-01-10 10:12:59 +00:00
|
|
|
|
|
|
|
|
(* add collided enemies to player record,
|
|
|
|
|
* concatenating with the previous enemies defeated *)
|
|
|
|
|
val newDefeated =
|
|
|
|
|
Vector.map (fn id => {angle = 360}) enemyCollisions
|
|
|
|
|
val oldDefeated = #enemies player
|
|
|
|
|
val allDefeated = Vector.concat [oldDefeated, newDefeated]
|
|
|
|
|
val player = Player.withPatches (player, [W_ENEMIES allDefeated])
|
|
|
|
|
in
|
2025-01-11 13:45:29 +00:00
|
|
|
(player, enemies, enemyTree)
|
2025-01-10 10:12:59 +00:00
|
|
|
end
|
|
|
|
|
| _ =>
|
|
|
|
|
(case attacked of
|
|
|
|
|
NOT_ATTACKED =>
|
|
|
|
|
let
|
|
|
|
|
val enemyCollisions = QuadTree.getCollisions
|
|
|
|
|
(x, y, size, size, 0, 0, 1920, 1080, 0, enemyTree)
|
|
|
|
|
|
|
|
|
|
val patches =
|
|
|
|
|
checkEnemies (player, enemies, enemyCollisions, [])
|
|
|
|
|
val player = Player.withPatches (player, patches)
|
2025-01-11 21:35:55 +00:00
|
|
|
|
|
|
|
|
val enemies = filterEnemyProjectiles
|
|
|
|
|
(Vector.length enemies - 1, enemies, projectileTree, [])
|
2025-01-10 10:12:59 +00:00
|
|
|
in
|
2025-01-11 13:45:29 +00:00
|
|
|
(player, enemies, enemyTree)
|
2025-01-10 10:12:59 +00:00
|
|
|
end
|
|
|
|
|
| ATTACKED amt =>
|
2025-01-12 11:46:32 +00:00
|
|
|
if amt = Constants.attackedLimit then
|
2025-01-10 10:12:59 +00:00
|
|
|
(* if reached limit, detect enemies again *)
|
|
|
|
|
let
|
|
|
|
|
val enemyCollisions = QuadTree.getCollisions
|
|
|
|
|
(x, y, size, size, 0, 0, 1920, 1080, 0, enemyTree)
|
|
|
|
|
val lst = [W_ATTACKED NOT_ATTACKED]
|
|
|
|
|
val patches =
|
|
|
|
|
checkEnemies (player, enemies, enemyCollisions, lst)
|
|
|
|
|
val player = Player.withPatches (player, patches)
|
2025-01-11 21:35:55 +00:00
|
|
|
|
|
|
|
|
val enemies = filterEnemyProjectiles
|
|
|
|
|
(Vector.length enemies - 1, enemies, projectileTree, [])
|
2025-01-10 10:12:59 +00:00
|
|
|
in
|
2025-01-11 13:45:29 +00:00
|
|
|
(player, enemies, enemyTree)
|
2025-01-10 10:12:59 +00:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
(* if attacked, don't detect collisions,
|
|
|
|
|
* allowing a brief invincibility period as is common in many games
|
|
|
|
|
* *)
|
|
|
|
|
let
|
|
|
|
|
val amt = amt + 1
|
|
|
|
|
val attacked = ATTACKED amt
|
|
|
|
|
val player = Player.withPatches
|
|
|
|
|
(player, [W_ATTACKED attacked])
|
2025-01-11 21:35:55 +00:00
|
|
|
|
|
|
|
|
val enemies = filterEnemyProjectiles
|
|
|
|
|
(Vector.length enemies - 1, enemies, projectileTree, [])
|
2025-01-10 10:12:59 +00:00
|
|
|
in
|
2025-01-11 13:45:29 +00:00
|
|
|
(player, enemies, enemyTree)
|
2025-01-10 10:12:59 +00:00
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
end
|