From c4989711dfc5038552ecee0e1f64a6abf20fb7e2 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sun, 12 Jan 2025 12:53:41 +0000 Subject: [PATCH] refactor constants out of enemy.sml --- fcore/constants.sml | 4 ++++ fcore/enemy.sml | 29 ++++++++++++++++++----------- fcore/player-enemy.sml | 4 ++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/fcore/constants.sml b/fcore/constants.sml index efa43d5..4811ddb 100644 --- a/fcore/constants.sml +++ b/fcore/constants.sml @@ -24,4 +24,8 @@ struct val projectileSize: Real32.real = 9.0 val projectileDistance: Real32.real = 13.0 val projectileSizeInt = 9 + + (* constants for enemy *) + val enemySize = 35 + val enemySizeReal: Real32.real = 35.0 end diff --git a/fcore/enemy.sml b/fcore/enemy.sml index ef1ead5..3408d23 100644 --- a/fcore/enemy.sml +++ b/fcore/enemy.sml @@ -1,15 +1,17 @@ structure Enemy = 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 size = Constants.enemySize + val ww = Constants.worldWidth + val wh = Constants.worldHeight + 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 if hasCollision then if health = 1 then @@ -27,7 +29,12 @@ struct 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 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 helpGenerateTree (pos + 1, enemyVec, acc) end @@ -51,12 +58,12 @@ struct fun helpGetDrawVec ({x, y, id = _, health = _}, width, height) = let - val wratio = width / 1920.0 - val hratio = height / 1080.0 + val wratio = width / Constants.worldWidthReal + val hratio = height / Constants.worldHeightReal in if wratio < hratio then let - val scale = 1080.0 * wratio + val scale = Constants.worldHeightReal * wratio val yOffset = if height > scale then (height - scale) / 2.0 else if height < scale then (scale - height) / 2.0 @@ -65,13 +72,13 @@ struct val x = Real32.fromInt x * wratio val y = Real32.fromInt y * wratio + yOffset - val realSize = realSize * wratio + val realSize = Constants.enemySizeReal * wratio in Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0) end else let - val scale = 1920.0 * hratio + val scale = Constants.worldWidthReal * hratio val xOffset = if width > scale then (width - scale) / 2.0 else if width < scale then (scale - width) / 2.0 @@ -80,7 +87,7 @@ struct val x = Real32.fromInt x * hratio + xOffset val y = Real32.fromInt y * hratio - val realSize = realSize * hratio + val realSize = Constants.enemySizeReal * hratio in Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0) end diff --git a/fcore/player-enemy.sml b/fcore/player-enemy.sml index b57c4bb..4429b40 100644 --- a/fcore/player-enemy.sml +++ b/fcore/player-enemy.sml @@ -34,8 +34,8 @@ struct val pCentreX = x + pHalfW val {x = ex, y = ey, ...} = Enemy.find (id, enemies) - val eFinishX = ex + Enemy.size - val eHalfW = Enemy.size div 2 + val eFinishX = ex + Constants.enemySize + val eHalfW = Constants.enemySize div 2 val eCentreX = ex + eHalfW in eCentreX < pCentreX