From f1521acec15a03d76cc08b2a68012d25700fb1a0 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Thu, 13 Feb 2025 11:24:44 +0000 Subject: [PATCH] refactor file order in preparation for adding GapMap for enemy --- .gitmodules | 3 + fcore/enemy/enemy-behaviour.sml | 79 +++++++++++++------------ fcore/enemy/enemy-patch.sml | 16 ++--- fcore/enemy/enemy-type.sml | 49 ++++++++++++++++ fcore/enemy/enemy-variants.sml | 7 --- fcore/enemy/enemy.sml | 2 +- fcore/enemy/falling-enemies.sml | 8 ++- fcore/entity-type.sml | 27 +++++++++ fcore/game-type.sml | 100 ++++++-------------------------- fcore/physics.sml | 9 +-- fcore/player-patch.sml | 12 ++-- fcore/player.sml | 3 +- fcore/trace-jump.sml | 12 ++-- oms.mlb | 3 +- vendored/brolib-sml | 1 + 15 files changed, 173 insertions(+), 158 deletions(-) create mode 100644 .gitmodules create mode 100644 fcore/enemy/enemy-type.sml delete mode 100644 fcore/enemy/enemy-variants.sml create mode 100644 fcore/entity-type.sml create mode 160000 vendored/brolib-sml diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4860291 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendored/brolib-sml"] + path = vendored/brolib-sml + url = https://github.com/hummy123/brolib-sml diff --git a/fcore/enemy/enemy-behaviour.sml b/fcore/enemy/enemy-behaviour.sml index 6d67e95..7c2d7af 100644 --- a/fcore/enemy/enemy-behaviour.sml +++ b/fcore/enemy/enemy-behaviour.sml @@ -1,6 +1,9 @@ structure EnemyBehaviour = struct open GameType + open EnemyType + open EntityType + (* if player is attacking, does enemy collide with attack? *) fun isCollidingWithPlayerAttack (player: player, enemy: enemy) = let @@ -569,44 +572,40 @@ struct , enemyList , fallingList ) = - let - open EnemyVariants - in - case #variant enemy of - PATROL_SLIME => - updatePatrolState - ( player - , enemy - , walls - , wallTree - , platforms - , platformTree - , projectileTree - , enemyList - , fallingList - ) - | FOLLOW_SLIME => - updateFollowState - ( player - , enemy - , walls - , wallTree - , platforms - , platformTree - , projectileTree - , graph - , enemyList - , fallingList - ) - | STRAIGHT_BAT => - updateStraightBat - ( player - , enemy - , walls - , wallTree - , projectileTree - , enemyList - , fallingList - ) - end + case #variant enemy of + PATROL_SLIME => + updatePatrolState + ( player + , enemy + , walls + , wallTree + , platforms + , platformTree + , projectileTree + , enemyList + , fallingList + ) + | FOLLOW_SLIME => + updateFollowState + ( player + , enemy + , walls + , wallTree + , platforms + , platformTree + , projectileTree + , graph + , enemyList + , fallingList + ) + | STRAIGHT_BAT => + updateStraightBat + ( player + , enemy + , walls + , wallTree + , projectileTree + , enemyList + , fallingList + ) end diff --git a/fcore/enemy/enemy-patch.sml b/fcore/enemy/enemy-patch.sml index 65cc98a..2474bb4 100644 --- a/fcore/enemy/enemy-patch.sml +++ b/fcore/enemy/enemy-patch.sml @@ -4,18 +4,18 @@ sig W_HEALTH of int | W_X of int | W_Y of int - | W_X_AXIS of GameType.x_axis - | W_Y_AXIS of GameType.y_axis + | W_X_AXIS of EntityType.x_axis + | W_Y_AXIS of EntityType.y_axis | W_PLAT_ID of int | W_NEXT_PLAT_ID of int | W_BAT_REST of int | W_BAT_MAX_Y of int | W_BAT_MIN_Y of int - | W_BAT_DIR_Y of GameType.bat_dir_y + | W_BAT_DIR_Y of EnemyType.bat_dir_y - val withPatch: GameType.enemy * enemy_patch -> GameType.enemy + val withPatch: EnemyType.enemy * enemy_patch -> EnemyType.enemy - val withPatches: GameType.enemy * enemy_patch list -> GameType.enemy + val withPatches: EnemyType.enemy * enemy_patch list -> EnemyType.enemy end structure EnemyPatch: ENEMY_PATCH = @@ -24,14 +24,14 @@ struct W_HEALTH of int | W_X of int | W_Y of int - | W_X_AXIS of GameType.x_axis - | W_Y_AXIS of GameType.y_axis + | W_X_AXIS of EntityType.x_axis + | W_Y_AXIS of EntityType.y_axis | W_PLAT_ID of int | W_NEXT_PLAT_ID of int | W_BAT_REST of int | W_BAT_MAX_Y of int | W_BAT_MIN_Y of int - | W_BAT_DIR_Y of GameType.bat_dir_y + | W_BAT_DIR_Y of EnemyType.bat_dir_y fun mkEnemy ( id diff --git a/fcore/enemy/enemy-type.sml b/fcore/enemy/enemy-type.sml new file mode 100644 index 0000000..c63ceb3 --- /dev/null +++ b/fcore/enemy/enemy-type.sml @@ -0,0 +1,49 @@ +signature ENEMY_TYPE = +sig + datatype variant = PATROL_SLIME | FOLLOW_SLIME | STRAIGHT_BAT + + datatype bat_dir_y = UP | DOWN + + type enemy = + { id: int + , health: int + , x: int + , y: int + , xAxis: EntityType.x_axis + , yAxis: EntityType.y_axis + , variant: variant + , platID: int + , nextPlatID: int + , batRest: int + , batDirY: bat_dir_y + , batMaxY: int + , batMinY: int + } + + type falling_enemy = {x: int, y: int, variant: variant} +end + +structure EnemyType: ENEMY_TYPE = +struct + datatype variant = PATROL_SLIME | FOLLOW_SLIME | STRAIGHT_BAT + + datatype bat_dir_y = UP | DOWN + + type enemy = + { id: int + , health: int + , x: int + , y: int + , xAxis: EntityType.x_axis + , yAxis: EntityType.y_axis + , variant: variant + , platID: int + , nextPlatID: int + , batRest: int + , batDirY: bat_dir_y + , batMaxY: int + , batMinY: int + } + + type falling_enemy = {x: int, y: int, variant: variant} +end diff --git a/fcore/enemy/enemy-variants.sml b/fcore/enemy/enemy-variants.sml deleted file mode 100644 index 11e02d1..0000000 --- a/fcore/enemy/enemy-variants.sml +++ /dev/null @@ -1,7 +0,0 @@ -signature ENEMY_VARIANTS = -sig - datatype t = PATROL_SLIME | FOLLOW_SLIME | STRAIGHT_BAT -end - -structure EnemyVariants: ENEMY_VARIANTS = -struct datatype t = PATROL_SLIME | FOLLOW_SLIME | STRAIGHT_BAT end diff --git a/fcore/enemy/enemy.sml b/fcore/enemy/enemy.sml index 4519fdc..12ebd5a 100644 --- a/fcore/enemy/enemy.sml +++ b/fcore/enemy/enemy.sml @@ -1,6 +1,6 @@ structure Enemy = struct - open GameType + open EnemyType (* returns a vector of enemies, with new state (like position, etc.). * Also filters any enemies from list if defeated. diff --git a/fcore/enemy/falling-enemies.sml b/fcore/enemy/falling-enemies.sml index abf3e5e..ad78069 100644 --- a/fcore/enemy/falling-enemies.sml +++ b/fcore/enemy/falling-enemies.sml @@ -1,6 +1,8 @@ structure FallingEnemies = struct + open EnemyType open GameType + open EntityType fun helpGenerateTree (pos, fallingVec: falling_enemy vector, acc) = if pos = Vector.length fallingVec then @@ -23,7 +25,7 @@ struct , QuadTree.create (Constants.worldWidth, Constants.worldHeight) ) - fun isCollidingWithPlayerAttack (player: player, fx, fy) = + fun isCollidingWithPlayerAttack (player: GameType.player, fx, fy) = let val {x = px, y = py, mainAttack, facing, ...} = player in @@ -44,7 +46,7 @@ struct | _ => false end - fun updateList (pos, vec, player: player, acc) = + fun updateList (pos, vec, player: GameType.player, acc) = if pos < 0 then acc else @@ -55,7 +57,7 @@ struct val ww = Constants.worldWidth val wh = Constants.worldHeight in - if isCollidingWithPlayerAttack (player : player, x, y) then + if isCollidingWithPlayerAttack (player, x, y) then (* filter out if player is attacking falling enemy *) updateList (pos - 1, vec, player, acc) else if Collision.isCollidingPlus (x, y, size, size, 0, 0, ww, wh) then diff --git a/fcore/entity-type.sml b/fcore/entity-type.sml new file mode 100644 index 0000000..a2e1ecc --- /dev/null +++ b/fcore/entity-type.sml @@ -0,0 +1,27 @@ +signature ENTITY_TYPE = +sig + datatype y_axis = + ON_GROUND + | FALLING + | DROP_BELOW_PLATFORM + | JUMPING of int + | FLOATING of int + + datatype x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT + + datatype facing = FACING_LEFT | FACING_RIGHT +end + +structure EntityType :> ENTITY_TYPE = +struct + datatype y_axis = + ON_GROUND + | FALLING + | DROP_BELOW_PLATFORM + | JUMPING of int + | FLOATING of int + + datatype x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT + + datatype facing = FACING_LEFT | FACING_RIGHT +end diff --git a/fcore/game-type.sml b/fcore/game-type.sml index 8fc6efe..60de393 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -4,21 +4,10 @@ sig type platform = {id: int, x: int, y: int, width: int} - datatype y_axis = - ON_GROUND - | FALLING - | DROP_BELOW_PLATFORM - | JUMPING of int - | FLOATING of int - - datatype x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT - datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int datatype player_attacked = NOT_ATTACKED | ATTACKED of int - datatype facing = FACING_LEFT | FACING_RIGHT - datatype main_attack = MAIN_NOT_ATTACKING | MAIN_ATTACKING of {length: int, growing: bool} @@ -26,16 +15,16 @@ sig type defeated_enemies = {angle: int} - type player_projectile = {x: int, y: int, facing: facing} + type player_projectile = {x: int, y: int, facing: EntityType.facing} type player = - { yAxis: y_axis - , xAxis: x_axis + { yAxis: EntityType.y_axis + , xAxis: EntityType.x_axis , recoil: player_recoil , attacked: player_attacked , mainAttack: main_attack , mainAttackPressed: bool - , facing: facing + , facing: EntityType.facing , health: int , x: int , y: int @@ -46,35 +35,15 @@ sig , platID: int } - datatype bat_dir_y = UP | DOWN - - type enemy = - { id: int - , health: int - , x: int - , y: int - , xAxis: x_axis - , yAxis: y_axis - , variant: EnemyVariants.t - , platID: int - , nextPlatID: int - , batRest: int - , batDirY: bat_dir_y - , batMaxY: int - , batMinY: int - } - - type falling_enemy = {x: int, y: int, variant: EnemyVariants.t} - type game_type = { player: player , walls: wall vector , wallTree: QuadTree.t , platforms: platform vector , platformTree: QuadTree.t - , enemies: enemy vector + , enemies: EnemyType.enemy vector , graph: PlatSet.elem vector vector - , fallingEnemies: falling_enemy vector + , fallingEnemies: EnemyType.falling_enemy vector } val initial: game_type @@ -87,21 +56,10 @@ struct (* all platforms have a fixed visual height and a fixed collision height *) type platform = {id: int, x: int, y: int, width: int} - datatype y_axis = - ON_GROUND - | FALLING - | DROP_BELOW_PLATFORM - | JUMPING of int - | FLOATING of int - - datatype x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT - datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int datatype player_attacked = NOT_ATTACKED | ATTACKED of int - datatype facing = FACING_LEFT | FACING_RIGHT - datatype main_attack = MAIN_NOT_ATTACKING | MAIN_ATTACKING of {length: int, growing: bool} @@ -109,16 +67,16 @@ struct type defeated_enemies = {angle: int} - type player_projectile = {x: int, y: int, facing: facing} + type player_projectile = {x: int, y: int, facing: EntityType.facing} type player = - { yAxis: y_axis - , xAxis: x_axis + { yAxis: EntityType.y_axis + , xAxis: EntityType.x_axis , recoil: player_recoil , attacked: player_attacked , mainAttack: main_attack , mainAttackPressed: bool - , facing: facing + , facing: EntityType.facing , health: int , x: int , y: int @@ -129,47 +87,27 @@ struct , platID: int } - datatype bat_dir_y = UP | DOWN - - type enemy = - { id: int - , health: int - , x: int - , y: int - , xAxis: x_axis - , yAxis: y_axis - , variant: EnemyVariants.t - , platID: int - , nextPlatID: int - , batRest: int - , batDirY: bat_dir_y - , batMaxY: int - , batMinY: int - } - - type falling_enemy = {x: int, y: int, variant: EnemyVariants.t} - type game_type = { player: player , walls: wall vector , wallTree: QuadTree.t , platforms: platform vector , platformTree: QuadTree.t - , enemies: enemy vector + , enemies: EnemyType.enemy vector , graph: PlatSet.elem vector vector - , fallingEnemies: falling_enemy vector + , fallingEnemies: EnemyType.falling_enemy vector } val initial: game_type = let val player = - { yAxis = JUMPING 0 - , xAxis = STAY_STILL + { yAxis = EntityType.JUMPING 0 + , xAxis = EntityType.STAY_STILL + , facing = EntityType.FACING_RIGHT , recoil = NO_RECOIL , attacked = NOT_ATTACKED , mainAttack = MAIN_NOT_ATTACKING , mainAttackPressed = false - , facing = FACING_RIGHT , health = 3 , x = 500 , y = 800 @@ -227,13 +165,13 @@ struct , x = 751 , y = 555 , health = 1 - , xAxis = MOVE_RIGHT - , yAxis = FALLING - , variant = EnemyVariants.STRAIGHT_BAT + , xAxis = EntityType.MOVE_RIGHT + , yAxis = EntityType.FALLING + , variant = EnemyType.STRAIGHT_BAT + , batDirY = EnemyType.UP , platID = ~1 , nextPlatID = ~1 , batRest = 0 - , batDirY = UP , batMaxY = 485 , batMinY = 625 } diff --git a/fcore/physics.sml b/fcore/physics.sml index 6a01bc2..80c4ce8 100644 --- a/fcore/physics.sml +++ b/fcore/physics.sml @@ -13,18 +13,19 @@ sig (* destructuring functions *) val getX: t -> int val getY: t -> int - val getXAxis: t -> GameType.x_axis - val getYAxis: t -> GameType.y_axis + val getXAxis: t -> EntityType.x_axis + val getYAxis: t -> EntityType.y_axis val W_X: int -> patch val W_Y: int -> patch - val W_Y_AXIS: GameType.y_axis -> patch + val W_Y_AXIS: EntityType.y_axis -> patch val W_PLAT_ID: int -> patch end functor MakePhysics(Fn: PHYSICS_INPUT) = struct open GameType + open EntityType fun getPhysicsPatches input = let @@ -245,7 +246,7 @@ structure PlayerPhysics = structure EnemyPhysics = MakePhysics (struct - type t = GameType.enemy + type t = EnemyType.enemy type patch = EnemyPatch.enemy_patch val entitySize = Constants.enemySize diff --git a/fcore/player-patch.sml b/fcore/player-patch.sml index 279918c..39a51b3 100644 --- a/fcore/player-patch.sml +++ b/fcore/player-patch.sml @@ -1,12 +1,12 @@ signature PLAYER_PATCH = sig datatype player_patch = - W_X_AXIS of GameType.x_axis - | W_Y_AXIS of GameType.y_axis + W_X_AXIS of EntityType.x_axis + | W_Y_AXIS of EntityType.y_axis + | W_FACING of EntityType.facing | W_RECOIL of GameType.player_recoil | W_ATTACKED of GameType.player_attacked | W_MAIN_ATTACK of GameType.main_attack - | W_FACING of GameType.facing | W_HEALTH of int | W_X of int | W_Y of int @@ -24,12 +24,12 @@ end structure PlayerPatch: PLAYER_PATCH = struct datatype player_patch = - W_X_AXIS of GameType.x_axis - | W_Y_AXIS of GameType.y_axis + W_X_AXIS of EntityType.x_axis + | W_Y_AXIS of EntityType.y_axis + | W_FACING of EntityType.facing | W_RECOIL of GameType.player_recoil | W_ATTACKED of GameType.player_attacked | W_MAIN_ATTACK of GameType.main_attack - | W_FACING of GameType.facing | W_HEALTH of int | W_X of int | W_Y of int diff --git a/fcore/player.sml b/fcore/player.sml index 27c2756..59fdb73 100644 --- a/fcore/player.sml +++ b/fcore/player.sml @@ -2,6 +2,7 @@ structure Player = struct open GameType open PlayerPatch + open EntityType (* helper functions checking input *) fun getXAxis (lh, rh) = @@ -315,7 +316,7 @@ struct structure FoldEnemies = MakeQuadTreeFold (struct - type env = enemy vector * player + type env = EnemyType.enemy vector * player type state = PlayerPatch.player_patch list fun getEnemyRecoilPatches (player, playerOnRight, acc) = diff --git a/fcore/trace-jump.sml b/fcore/trace-jump.sml index 2ec2132..fb84f1b 100644 --- a/fcore/trace-jump.sml +++ b/fcore/trace-jump.sml @@ -29,7 +29,7 @@ struct orelse traceRightDescent (nextX, nextY, nextPlatID, platTree) end - fun traceRightDrop (enemy: GameType.enemy, nextPlatID, platTree) = + fun traceRightDrop (enemy: EnemyType.enemy, nextPlatID, platTree) = let val {x, y, ...} = enemy val x = x - Constants.enemySize @@ -59,12 +59,12 @@ struct traceRightJumpAscent (nextX, nextY, nextJump, nextPlatID, platTree) end - fun traceRightJump (enemy: GameType.enemy, nextPlatID, platTree) = + fun traceRightJump (enemy: EnemyType.enemy, nextPlatID, platTree) = let val {x, y, ...} = enemy val x = x - Constants.enemySize - open GameType + open EntityType in if EnemyPhysics.standingOnArea (x, y, platTree) then traceRightJumpAscent (x, y, 0, nextPlatID, platTree) @@ -94,7 +94,7 @@ struct orelse traceLeftDescent (nextX, nextY, nextPlatID, platTree) end - fun traceLeftDrop (enemy: GameType.enemy, nextPlatID, platTree) = + fun traceLeftDrop (enemy: EnemyType.enemy, nextPlatID, platTree) = let val {x, y, ...} = enemy val x = x + Constants.enemySize @@ -121,12 +121,12 @@ struct traceLeftJumpAscent (nextX, nextY, nextJump, nextPlatID, platTree) end - fun traceLeftJump (enemy: GameType.enemy, nextPlatID, platTree) = + fun traceLeftJump (enemy: EnemyType.enemy, nextPlatID, platTree) = let val {x, y, ...} = enemy val x = x + 75 - open GameType + open EntityType in if EnemyPhysics.standingOnArea (x, y, platTree) then traceLeftJumpAscent (x, y, 0, nextPlatID, platTree) diff --git a/oms.mlb b/oms.mlb index 23a4ad7..76fd5dd 100644 --- a/oms.mlb +++ b/oms.mlb @@ -25,7 +25,8 @@ fcore/platform.sml fcore/graph.sml fcore/path-finding.sml -fcore/enemy/enemy-variants.sml +fcore/entity-type.sml +fcore/enemy/enemy-type.sml fcore/game-type.sml fcore/player-patch.sml diff --git a/vendored/brolib-sml b/vendored/brolib-sml new file mode 160000 index 0000000..d23396f --- /dev/null +++ b/vendored/brolib-sml @@ -0,0 +1 @@ +Subproject commit d23396f5d10041fee7d424dde261ad23f4cf2b87