use binary search to find enemy with ID in 'GameUpdate.checkEnemies' function, because we previously relied on the enemy's index
This commit is contained in:
@@ -17,6 +17,24 @@ struct
|
||||
|
||||
fun generateTree enemyVec = helpGenerateTree (0, enemyVec, QuadTree.empty)
|
||||
|
||||
fun helpFind (findNum, vec, low, high) =
|
||||
(* should only be called when we know enemy already exists in vec *)
|
||||
let
|
||||
val mid = low + ((high - low) div 2)
|
||||
val enemy = Vector.sub (vec, mid)
|
||||
val {id = curNum, x = _, y = _, health = _} = enemy
|
||||
in
|
||||
if curNum = findNum then
|
||||
enemy
|
||||
else if curNum < findNum then
|
||||
helpFind (findNum, vec, mid + 1, high)
|
||||
else
|
||||
helpFind (findNum, vec, low, mid - 1)
|
||||
end
|
||||
|
||||
fun find (findNum, vec) =
|
||||
helpFind (findNum, vec, 0, Vector.length vec - 1)
|
||||
|
||||
fun helpGetDrawVec ({x, y, id = _, health = _}, width, height) =
|
||||
let
|
||||
val wratio = width / 1920.0
|
||||
|
||||
@@ -162,7 +162,9 @@ struct
|
||||
val platformTree = Platform.generateTree platforms
|
||||
|
||||
val enemy1 = {id = 1, x = 300, y = 945, health = 5}
|
||||
val enemies = Vector.fromList [enemy1]
|
||||
val enemy2 = {id = 2, x = 555, y = 945, health = 5}
|
||||
val enemy3 = {id = 3, x = 979, y = 945, health = 5}
|
||||
val enemies = Vector.fromList [enemy1, enemy2, enemy3]
|
||||
val enemyTree = Enemy.generateTree enemies
|
||||
in
|
||||
{ player = player
|
||||
|
||||
@@ -33,7 +33,7 @@ struct
|
||||
val pHalfW = Player.size div 2
|
||||
val pCentreX = x + pHalfW
|
||||
|
||||
val {x = ex, y = ey, ...} = Vector.sub (enemies, id - 1)
|
||||
val {x = ex, y = ey, ...} = Enemy.find (id, enemies)
|
||||
val eFinishX = ex + Enemy.size
|
||||
val eHalfW = Enemy.size div 2
|
||||
val eCentreX = ex + eHalfW
|
||||
@@ -47,6 +47,15 @@ struct
|
||||
end
|
||||
| [] => acc
|
||||
|
||||
fun checkEnemiesWhileAttacking (player, enemies, lst, acc) =
|
||||
let
|
||||
open QuadTree
|
||||
in
|
||||
case lst of
|
||||
enemyID :: tl => (* placeholder *) acc
|
||||
| [] => acc
|
||||
end
|
||||
|
||||
(* removes enemies from `enemies` vector when that enemy is in collisions *)
|
||||
fun filterEnemyCollisions (pos, collisions, enemies: enemy vector, acc) =
|
||||
if pos < 0 then
|
||||
|
||||
Reference in New Issue
Block a user