a little bit of refactoring

This commit is contained in:
2025-01-11 21:58:34 +00:00
parent 75e28b892e
commit 8498eacde2
5 changed files with 89 additions and 103 deletions

View File

@@ -3,14 +3,31 @@ struct
val size = 35
val realSize = 35.0
(* called when filtering enemies,
* to adjust enemy data on collision with projectile *)
fun onCollisionWithProjectile (enemy, projectileTree, acc) =
let
val {x, y, health, id} = enemy
val hasCollision = QuadTree.hasCollisionAt
(x, y, size, size, 0, 0, 1920, 1080, ~1, projectileTree)
in
if hasCollision then
if health = 1 then
(* filter out if decrementing health by one = 0 *)
acc
else
{health = health - 1, x = x, y = y, id = id} :: acc
else
enemy :: acc
end
fun helpGenerateTree (pos, enemyVec, acc) =
if pos = Vector.length enemyVec then
acc
else
let
val {id, x, y, health = _} = Vector.sub (enemyVec, pos)
val acc = QuadTree.insert
(x, y, size, size, 0, 0, 1920, 1080, id, acc)
val acc = QuadTree.insert (x, y, size, size, 0, 0, 1920, 1080, id, acc)
in
helpGenerateTree (pos + 1, enemyVec, acc)
end
@@ -24,12 +41,9 @@ struct
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)
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) =

View File

@@ -68,28 +68,10 @@ struct
let
val enemy = Vector.sub (enemies, pos)
val acc =
if exists (#id enemy, collisions) then
(* filter out *)
acc
else
let
val {x, y, health, id} = enemy
val eSize = Enemy.size
val hasCollision = QuadTree.hasCollisionAt
(x, y, eSize, eSize, 0, 0, 1920, 1080, ~1, projectileTree)
in
if hasCollision then
if health = 1 then
(* filter out if decrementing health by one = 0 *)
acc
else
{health = health - 1, x = x, y = y, id = id} :: acc
else
enemy :: acc
end
if exists (#id enemy, collisions) then (* filter out *) acc
else Enemy.onCollisionWithProjectile (enemy, projectileTree, acc)
in
filterEnemyAttacked
(pos - 1, collisions, enemies, projectileTree, acc)
filterEnemyAttacked (pos - 1, collisions, enemies, projectileTree, acc)
end
(* filter enemy projectiles when player is not attacking *)
@@ -99,20 +81,7 @@ struct
else
let
val enemy = Vector.sub (enemies, pos)
val {x, y, health, id} = enemy
val eSize = Enemy.size
val hasCollision = QuadTree.hasCollisionAt
(x, y, eSize, eSize, 0, 0, 1920, 1080, ~1, projectileTree)
val acc =
if hasCollision then
if health = 1 then
(* filter out if decrementing health by one = 0 *)
acc
else
{health = health - 1, x = x, y = y, id = id} :: acc
else
enemy :: acc
val acc = Enemy.onCollisionWithProjectile (enemy, projectileTree, acc)
in
filterEnemyProjectiles (pos - 1, enemies, projectileTree, acc)
end

View File

@@ -1008,60 +1008,4 @@ struct
(x, y, 0, enemies, width, height, hratio, xOffset, 0.0, [])
end
end
fun helpGetProjectileVec
(pos, projectiles, width, height, ratio, xOffset, yOffset, acc) =
if pos = Vector.length projectiles then
Vector.concat acc
else
let
val {x, y, ...} = Vector.sub (projectiles, pos)
val x = Real32.fromInt x * ratio + xOffset
val y = Real32.fromInt y * ratio + yOffset
val defeatedSize = defeatedSize * ratio
val vec = Field.lerp
(x, y, defeatedSize, defeatedSize, width, height, 0.3, 0.9, 0.3, 1.0)
val acc = vec :: acc
in
helpGetProjectileVec
(pos + 1, projectiles, width, height, ratio, xOffset, yOffset, acc)
end
fun getProjectileVec (player: player, width, height) =
let
val {x, y, projectiles, ...} = player
val wratio = width / 1920.0
val hratio = height / 1080.0
in
if wratio < hratio then
let
val scale = 1080.0 * wratio
val yOffset =
if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0
else 0.0
val xOffset = 0.0
in
helpGetProjectileVec
(0, projectiles, width, height, wratio, xOffset, yOffset, [])
end
else
let
val scale = 1920.0 * hratio
val xOffset =
if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0
else 0.0
val yOffset = 0.0
in
helpGetProjectileVec
(0, projectiles, width, height, hratio, xOffset, yOffset, [])
end
end
end

View File

@@ -1,11 +1,14 @@
structure Projectile =
struct
val projectileSize = 9.0
val projectileSizeInt = 9
fun helpGenerateTree (pos, projectiles, acc) =
if pos = Vector.length projectiles then
acc
else
let
val size = Player.defeatedSizeInt
val size = projectileSizeInt
val {x, y, facing = _} = Vector.sub (projectiles, pos)
val acc = QuadTree.insert (x, y, size, size, 0, 0, 1920, 1080, pos, acc)
@@ -15,4 +18,60 @@ struct
fun generateTree projectiles =
helpGenerateTree (0, projectiles, QuadTree.empty)
fun helpGetProjectileVec
(pos, projectiles, width, height, ratio, xOffset, yOffset, acc) =
if pos = Vector.length projectiles then
Vector.concat acc
else
let
val {x, y, ...} = Vector.sub (projectiles, pos)
val x = Real32.fromInt x * ratio + xOffset
val y = Real32.fromInt y * ratio + yOffset
val size = projectileSize * ratio
val vec = Field.lerp
(x, y, size, size, width, height, 0.3, 0.9, 0.3, 1.0)
val acc = vec :: acc
in
helpGetProjectileVec
(pos + 1, projectiles, width, height, ratio, xOffset, yOffset, acc)
end
fun getProjectileVec (player: GameType.player, width, height) =
let
val {projectiles, ...} = player
val wratio = width / 1920.0
val hratio = height / 1080.0
in
if wratio < hratio then
let
val scale = 1080.0 * wratio
val yOffset =
if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0
else 0.0
val xOffset = 0.0
in
helpGetProjectileVec
(0, projectiles, width, height, wratio, xOffset, yOffset, [])
end
else
let
val scale = 1920.0 * hratio
val xOffset =
if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0
else 0.0
val yOffset = 0.0
in
helpGetProjectileVec
(0, projectiles, width, height, hratio, xOffset, yOffset, [])
end
end
end

View File

@@ -248,7 +248,7 @@ struct
(* temp *)
val pelletVec = Player.getPelletVec (#player game, width, height)
val projectileVec =
Player.getProjectileVec (#player game, width, height)
Projectile.getProjectileVec (#player game, width, height)
val fieldVec = Vector.concat [pelletVec, projectileVec, fieldVec]
val shellState = uploadWall (shellState, wallVec)