From 93aebe5fa126e2b6b29fa005f8ac14c85510c9d6 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Fri, 14 Feb 2025 09:06:32 +0000 Subject: [PATCH] a bit of refactoring with regards to enemy's structures and files --- fcore/enemy/enemy-map.sml | 84 +--------------------------------- fcore/enemy/enemy.sml | 96 +++++++++++++++++++++++++++++++++++++++ fcore/game-update.sml | 6 +-- oms.mlb | 2 +- shell/gl-draw.sml | 6 +-- 5 files changed, 100 insertions(+), 94 deletions(-) diff --git a/fcore/enemy/enemy-map.sml b/fcore/enemy/enemy-map.sml index 540dbea..ba3f2be 100644 --- a/fcore/enemy/enemy-map.sml +++ b/fcore/enemy/enemy-map.sml @@ -1,83 +1 @@ -structure EnemyPair = -struct - type key = int - type value = EnemyType.enemy - - fun l (a: int, b: int) = a < b - fun eq (a: int, b: int) = a = b - fun g (a: int, b: int) = a > b - - val maxNodeSize = 8 -end - -structure EnemyMap = MakeGapMap(EnemyPair) - -structure EnemyTreeFolder = -struct - structure Pair = EnemyPair - - type env = unit - type state = QuadTree.t - - fun fold (enemyID, enemy: EnemyType.enemy, (), quadTree) = - let - val {id, x, y, ...} = enemy - val size = Constants.enemySize - in - QuadTree.insert (x, y, size, size, id, quadTree) - end -end - -structure MakeEnemyTree = MakeGapMapFolder(EnemyTreeFolder) - -structure EnemyDrawVec = - MakeGapMapFolder - (struct - structure Pair = EnemyPair - - type env = Real32.real * Real32.real - type state = Real32.real vector list - - fun helpGetDrawVec (enemy: EnemyType.enemy, width, height) = - let - val {x, y, ...} = enemy - val wratio = width / Constants.worldWidthReal - val hratio = height / Constants.worldHeightReal - in - if wratio < hratio then - let - val scale = Constants.worldHeightReal * wratio - val yOffset = - if height > scale then (height - scale) / 2.0 - else if height < scale then (scale - height) / 2.0 - else 0.0 - - val x = Real32.fromInt x * wratio - val y = Real32.fromInt y * wratio + yOffset - - 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 = Constants.worldWidthReal * hratio - val xOffset = - if width > scale then (width - scale) / 2.0 - else if width < scale then (scale - width) / 2.0 - else 0.0 - - val x = Real32.fromInt x * hratio + xOffset - val y = Real32.fromInt y * hratio - - val realSize = Constants.enemySizeReal * hratio - in - Block.lerp - (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0) - end - end - - fun fold (_, enemy: EnemyType.enemy, (width, height), acc) = - helpGetDrawVec (enemy, width, height) :: acc - end) +structure EnemyMap = Enemy.EnemyMap diff --git a/fcore/enemy/enemy.sml b/fcore/enemy/enemy.sml index 98a6115..dfe5d65 100644 --- a/fcore/enemy/enemy.sml +++ b/fcore/enemy/enemy.sml @@ -1,4 +1,100 @@ structure Enemy = struct + structure EnemyPair = + struct + type key = int + type value = EnemyType.enemy + + fun l (a: int, b: int) = a < b + fun eq (a: int, b: int) = a = b + fun g (a: int, b: int) = a > b + + val maxNodeSize = 8 + end + + structure EnemyMap = MakeGapMap(EnemyPair) + + (* - Generating enemy tree - *) + structure EnemyTree = + MakeGapMapFolder + (struct + structure Pair = EnemyPair + + type env = unit + type state = QuadTree.t + + fun fold (enemyID, enemy: EnemyType.enemy, (), quadTree) = + let + val {id, x, y, ...} = enemy + val size = Constants.enemySize + in + QuadTree.insert (x, y, size, size, id, quadTree) + end + end) + + fun generateTree enemies = + EnemyTree.foldUnordered + ( enemies + , () + , QuadTree.create (Constants.worldWidth, Constants.worldHeight) + ) + + (* - Drawing enemies - *) + structure EnemyDrawVec = + MakeGapMapFolder + (struct + structure Pair = EnemyPair + + type env = Real32.real * Real32.real + type state = Real32.real vector list + + fun helpGetDrawVec (enemy: EnemyType.enemy, width, height) = + let + val {x, y, ...} = enemy + val wratio = width / Constants.worldWidthReal + val hratio = height / Constants.worldHeightReal + in + if wratio < hratio then + let + val scale = Constants.worldHeightReal * wratio + val yOffset = + if height > scale then (height - scale) / 2.0 + else if height < scale then (scale - height) / 2.0 + else 0.0 + + val x = Real32.fromInt x * wratio + val y = Real32.fromInt y * wratio + yOffset + + 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 = Constants.worldWidthReal * hratio + val xOffset = + if width > scale then (width - scale) / 2.0 + else if width < scale then (scale - width) / 2.0 + else 0.0 + + val x = Real32.fromInt x * hratio + xOffset + val y = Real32.fromInt y * hratio + + val realSize = Constants.enemySizeReal * hratio + in + Block.lerp + (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0) + end + end + + fun fold (_, enemy: EnemyType.enemy, (width, height), acc) = + helpGetDrawVec (enemy, width, height) :: acc + end) + + fun getDrawVec (enemies, width, height) = + let val vec = EnemyDrawVec.foldUnordered (enemies, (width, height), []) + in Vector.concat vec + end end diff --git a/fcore/game-update.sml b/fcore/game-update.sml index 8caa804..c0eafa4 100644 --- a/fcore/game-update.sml +++ b/fcore/game-update.sml @@ -13,11 +13,7 @@ struct , fallingEnemies } = game - val enemyTree = MakeEnemyTree.foldUnordered - ( enemies - , () - , QuadTree.create (Constants.worldWidth, Constants.worldHeight) - ) + val enemyTree = Enemy.generateTree enemies val player = Player.runPhysicsAndInput (game, input, enemyTree) val projectiles = #projectiles player diff --git a/oms.mlb b/oms.mlb index f283667..8ee47ab 100644 --- a/oms.mlb +++ b/oms.mlb @@ -29,6 +29,7 @@ fcore/path-finding.sml fcore/entity-type.sml fcore/enemy/enemy-type.sml +fcore/enemy/enemy.sml fcore/enemy/enemy-map.sml fcore/game-type.sml @@ -38,7 +39,6 @@ fcore/physics.sml fcore/trace-jump.sml fcore/enemy/enemy-behaviour.sml -fcore/enemy/enemy.sml fcore/enemy/falling-enemies.sml fcore/player.sml fcore/projectile.sml diff --git a/shell/gl-draw.sml b/shell/gl-draw.sml index c6bf685..8f632a7 100644 --- a/shell/gl-draw.sml +++ b/shell/gl-draw.sml @@ -236,11 +236,7 @@ struct val game = GameUpdate.update (game, input) val playerVec = Player.getDrawVec (#player game, width, height) - - val enemyVec = - EnemyDrawVec.foldUnordered (#enemies game, (width, height), []) - val enemyVec = Vector.concat enemyVec - + val enemyVec = Enemy.getDrawVec (#enemies game, width, height) val playerVec = Vector.concat [playerVec, enemyVec] val wallVec = Wall.getDrawVec (#walls game, width, height)