From 51401231e1c3c5eb2abbc841925cb08504d1c227 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sat, 8 Feb 2025 09:49:32 +0000 Subject: [PATCH] update state of falling enemies per loop --- fcore/enemy/enemy-behaviour.sml | 14 ++------ fcore/enemy/falling-enemies.sml | 61 +++++++++++++++++++++++++++++++++ fcore/game-type.sml | 6 ++-- fcore/game-update.sml | 10 ++++-- oms.mlb | 1 + 5 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 fcore/enemy/falling-enemies.sml diff --git a/fcore/enemy/enemy-behaviour.sml b/fcore/enemy/enemy-behaviour.sml index 4f32a0b..cde20f1 100644 --- a/fcore/enemy/enemy-behaviour.sml +++ b/fcore/enemy/enemy-behaviour.sml @@ -405,12 +405,7 @@ struct * fallingEnemies *) let val fallingList = - { falling = false - , x = x - , y = y - , variant = #variant enemy - , jumped = 0 - } :: fallingList + {x = x, y = y, variant = #variant enemy, jumped = 0} :: fallingList in (enemyList, fallingList) end @@ -457,12 +452,7 @@ struct * fallingEnemies *) let val fallingList = - { falling = false - , x = x - , y = y - , variant = #variant enemy - , jumped = 0 - } :: fallingList + {x = x, y = y, variant = #variant enemy, jumped = 0} :: fallingList in (enemyList, fallingList) end diff --git a/fcore/enemy/falling-enemies.sml b/fcore/enemy/falling-enemies.sml new file mode 100644 index 0000000..cf3a33a --- /dev/null +++ b/fcore/enemy/falling-enemies.sml @@ -0,0 +1,61 @@ +structure FallingEnemies = +struct + open GameType + + fun helpGenerateTree (pos, fallingVec: falling_enemy vector, acc) = + if pos = Vector.length fallingVec then + acc + else + let + val {x, y, ...} = Vector.sub (fallingVec, pos) + + val size = Constants.enemySize + + val acc = QuadTree.insert (x, y, size, size, pos + 1, acc) + in + helpGenerateTree (pos + 1, fallingVec, acc) + end + + fun generateTree fallingVec = + helpGenerateTree + ( 0 + , fallingVec + , QuadTree.create (Constants.worldWidth, Constants.worldHeight) + ) + + fun updateList (pos, vec, acc) = + if pos < 0 then + acc + else + let + val {x, y, jumped, variant} = Vector.sub (vec, pos) + + val size = Constants.enemySize + val ww = Constants.worldWidth + val wh = Constants.worldHeight + in + if Collision.isColliding (x, y, size, size, 0, 0, ww, wh) then + (* move falling enemy up or down depending on jumped *) + let + val updated = + if jumped < Constants.jumpLimit then + { x = x + , y = y - Constants.moveEnemyBy + , jumped = jumped + Constants.moveEnemyBy + , variant = variant + } + else + { x = x + , y = y + Constants.moveEnemyBy + , jumped = jumped + , variant = variant + } + in + updateList (pos - 1, vec, updated :: acc) + end + else + (* if current is not colliding with world's bounds, then filter out + * as it is off screen *) + updateList (pos - 1, vec, acc) + end +end diff --git a/fcore/game-type.sml b/fcore/game-type.sml index 6d961fc..097d7e8 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -58,8 +58,7 @@ sig , nextPlatID: int } - type falling_enemy = - {falling: bool, x: int, y: int, variant: EnemyVariants.t, jumped: int} + type falling_enemy = {x: int, y: int, variant: EnemyVariants.t, jumped: int} type game_type = { player: player @@ -136,8 +135,7 @@ struct , nextPlatID: int } - type falling_enemy = - {falling: bool, x: int, y: int, variant: EnemyVariants.t, jumped: int} + type falling_enemy = {x: int, y: int, variant: EnemyVariants.t, jumped: int} type game_type = { player: player diff --git a/fcore/game-update.sml b/fcore/game-update.sml index 45e32fe..e385f96 100644 --- a/fcore/game-update.sml +++ b/fcore/game-update.sml @@ -19,7 +19,11 @@ struct val projectiles = #projectiles player val projectileTree = Projectile.generateTree projectiles - val (enemies, newFallingEnemies) = Enemy.updateEnemyList + (* update state of falling enemies and possibly filter *) + val fallingEnemies = FallingEnemies.updateList + (Vector.length fallingEnemies - 1, fallingEnemies, []) + + val (enemies, fallingEnemies) = Enemy.updateEnemyList ( Vector.length enemies - 1 , enemies , projectiles @@ -31,10 +35,10 @@ struct , player , graph , [] - , [] + , fallingEnemies ) - val fallingEnemies = Vector.fromList newFallingEnemies + val fallingEnemies = Vector.fromList fallingEnemies in { player = player , walls = walls diff --git a/oms.mlb b/oms.mlb index 5857ff3..23a4ad7 100644 --- a/oms.mlb +++ b/oms.mlb @@ -35,6 +35,7 @@ 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