refactor constants out of enemy.sml

This commit is contained in:
2025-01-12 12:53:41 +00:00
parent 9280a12911
commit c4989711df
3 changed files with 24 additions and 13 deletions

View File

@@ -24,4 +24,8 @@ struct
val projectileSize: Real32.real = 9.0 val projectileSize: Real32.real = 9.0
val projectileDistance: Real32.real = 13.0 val projectileDistance: Real32.real = 13.0
val projectileSizeInt = 9 val projectileSizeInt = 9
(* constants for enemy *)
val enemySize = 35
val enemySizeReal: Real32.real = 35.0
end end

View File

@@ -1,15 +1,17 @@
structure Enemy = structure Enemy =
struct struct
val size = 35
val realSize = 35.0
(* called when filtering enemies, (* called when filtering enemies,
* to adjust enemy data on collision with projectile *) * to adjust enemy data on collision with projectile *)
fun onCollisionWithProjectile (enemy, projectileTree, acc) = fun onCollisionWithProjectile (enemy, projectileTree, acc) =
let let
val {x, y, health, id} = enemy val {x, y, health, id} = enemy
val size = Constants.enemySize
val ww = Constants.worldWidth
val wh = Constants.worldHeight
val hasCollision = QuadTree.hasCollisionAt val hasCollision = QuadTree.hasCollisionAt
(x, y, size, size, 0, 0, 1920, 1080, ~1, projectileTree) (x, y, size, size, 0, 0, ww, wh, ~1, projectileTree)
in in
if hasCollision then if hasCollision then
if health = 1 then if health = 1 then
@@ -27,7 +29,12 @@ struct
else else
let let
val {id, x, y, health = _} = Vector.sub (enemyVec, pos) val {id, x, y, health = _} = Vector.sub (enemyVec, pos)
val acc = QuadTree.insert (x, y, size, size, 0, 0, 1920, 1080, id, acc)
val size = Constants.enemySize
val ww = Constants.worldWidth
val wh = Constants.worldHeight
val acc = QuadTree.insert (x, y, size, size, 0, 0, ww, wh, id, acc)
in in
helpGenerateTree (pos + 1, enemyVec, acc) helpGenerateTree (pos + 1, enemyVec, acc)
end end
@@ -51,12 +58,12 @@ struct
fun helpGetDrawVec ({x, y, id = _, health = _}, width, height) = fun helpGetDrawVec ({x, y, id = _, health = _}, width, height) =
let let
val wratio = width / 1920.0 val wratio = width / Constants.worldWidthReal
val hratio = height / 1080.0 val hratio = height / Constants.worldHeightReal
in in
if wratio < hratio then if wratio < hratio then
let let
val scale = 1080.0 * wratio val scale = Constants.worldHeightReal * wratio
val yOffset = val yOffset =
if height > scale then (height - scale) / 2.0 if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0 else if height < scale then (scale - height) / 2.0
@@ -65,13 +72,13 @@ struct
val x = Real32.fromInt x * wratio val x = Real32.fromInt x * wratio
val y = Real32.fromInt y * wratio + yOffset val y = Real32.fromInt y * wratio + yOffset
val realSize = realSize * wratio val realSize = Constants.enemySizeReal * wratio
in in
Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0) Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0)
end end
else else
let let
val scale = 1920.0 * hratio val scale = Constants.worldWidthReal * hratio
val xOffset = val xOffset =
if width > scale then (width - scale) / 2.0 if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0 else if width < scale then (scale - width) / 2.0
@@ -80,7 +87,7 @@ struct
val x = Real32.fromInt x * hratio + xOffset val x = Real32.fromInt x * hratio + xOffset
val y = Real32.fromInt y * hratio val y = Real32.fromInt y * hratio
val realSize = realSize * hratio val realSize = Constants.enemySizeReal * hratio
in in
Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0) Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0)
end end

View File

@@ -34,8 +34,8 @@ struct
val pCentreX = x + pHalfW val pCentreX = x + pHalfW
val {x = ex, y = ey, ...} = Enemy.find (id, enemies) val {x = ex, y = ey, ...} = Enemy.find (id, enemies)
val eFinishX = ex + Enemy.size val eFinishX = ex + Constants.enemySize
val eHalfW = Enemy.size div 2 val eHalfW = Constants.enemySize div 2
val eCentreX = ex + eHalfW val eCentreX = ex + eHalfW
in in
eCentreX < pCentreX eCentreX < pCentreX