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:
2024-12-27 18:26:57 +00:00
parent ada63d54ce
commit 8804847d7a
3 changed files with 31 additions and 2 deletions

View File

@@ -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