From 46a1836ae2732bd83f9f549c23cf3273ad9f3abb Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Fri, 14 Feb 2025 10:13:03 +0000 Subject: [PATCH] additional refactoring, moving player type into its own directory --- fcore/enemy/enemy-behaviour.sml | 14 ++++-- fcore/enemy/enemy-map.sml | 2 +- fcore/enemy/enemy-pair.sml | 11 +++++ fcore/enemy/enemy.sml | 17 +------ fcore/enemy/falling-enemies.sml | 9 ++-- fcore/game-type.sml | 72 ++--------------------------- fcore/physics.sml | 3 +- fcore/{ => player}/player-patch.sml | 26 +++++------ fcore/player/player-type.sml | 33 +++++++++++++ fcore/{ => player}/player.sml | 4 +- fcore/projectile.sml | 2 +- oms.mlb | 9 ++-- 12 files changed, 88 insertions(+), 114 deletions(-) create mode 100644 fcore/enemy/enemy-pair.sml rename fcore/{ => player}/player-patch.sml (91%) create mode 100644 fcore/player/player-type.sml rename fcore/{ => player}/player.sml (99%) diff --git a/fcore/enemy/enemy-behaviour.sml b/fcore/enemy/enemy-behaviour.sml index 64abf94..41ef07b 100644 --- a/fcore/enemy/enemy-behaviour.sml +++ b/fcore/enemy/enemy-behaviour.sml @@ -1,11 +1,10 @@ structure EnemyBehaviour = struct - open GameType open EnemyType open EntityType (* if player is attacking, does enemy collide with attack? *) - fun isCollidingWithPlayerAttack (player: player, enemy: enemy) = + fun isCollidingWithPlayerAttack (player: PlayerType.player, enemy: enemy) = let val {x = px, y = py, facing, mainAttack, ...} = player val pSize = Constants.playerSize @@ -14,7 +13,7 @@ struct val eSize = Constants.enemySize in case mainAttack of - MAIN_ATTACKING {length, ...} => + PlayerType.MAIN_ATTACKING {length, ...} => (case facing of FACING_RIGHT => let @@ -359,7 +358,14 @@ struct end fun getFollowPatches - (player: player, enemy, wallTree, platformTree, platforms, graph, acc) = + ( player: PlayerType.player + , enemy + , wallTree + , platformTree + , platforms + , graph + , acc + ) = let val pID = #platID player val eID = #platID enemy diff --git a/fcore/enemy/enemy-map.sml b/fcore/enemy/enemy-map.sml index ba3f2be..b6cee75 100644 --- a/fcore/enemy/enemy-map.sml +++ b/fcore/enemy/enemy-map.sml @@ -1 +1 @@ -structure EnemyMap = Enemy.EnemyMap +structure EnemyMap = MakeGapMap(EnemyPair) diff --git a/fcore/enemy/enemy-pair.sml b/fcore/enemy/enemy-pair.sml new file mode 100644 index 0000000..9cc70fb --- /dev/null +++ b/fcore/enemy/enemy-pair.sml @@ -0,0 +1,11 @@ +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 diff --git a/fcore/enemy/enemy.sml b/fcore/enemy/enemy.sml index de34279..443cfae 100644 --- a/fcore/enemy/enemy.sml +++ b/fcore/enemy/enemy.sml @@ -1,21 +1,6 @@ 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) - - (* - Updating state of enemies per-frame - *) + (* - Updating state of enemies per loop - *) (* structure UpdateEnemies = MakeGapMapMapper (struct structure Pair = EnemyPair diff --git a/fcore/enemy/falling-enemies.sml b/fcore/enemy/falling-enemies.sml index ad78069..b672234 100644 --- a/fcore/enemy/falling-enemies.sml +++ b/fcore/enemy/falling-enemies.sml @@ -1,7 +1,6 @@ structure FallingEnemies = struct open EnemyType - open GameType open EntityType fun helpGenerateTree (pos, fallingVec: falling_enemy vector, acc) = @@ -25,12 +24,12 @@ struct , QuadTree.create (Constants.worldWidth, Constants.worldHeight) ) - fun isCollidingWithPlayerAttack (player: GameType.player, fx, fy) = + fun isCollidingWithPlayerAttack (player: PlayerType.player, fx, fy) = let val {x = px, y = py, mainAttack, facing, ...} = player in case mainAttack of - MAIN_ATTACKING {length, ...} => + PlayerType.MAIN_ATTACKING {length, ...} => let val px = (case facing of @@ -46,7 +45,7 @@ struct | _ => false end - fun updateList (pos, vec, player: GameType.player, acc) = + fun updateList (pos, vec, player: PlayerType.player, acc) = if pos < 0 then acc else @@ -93,7 +92,7 @@ struct (pos + 1, fallingVec, width, height, ratio, xOffset, yOffset, acc) end - fun getDrawVec (game: game_type, width, height) = + fun getDrawVec (game: GameType.game_type, width, height) = if Vector.length (#fallingEnemies game) = 0 then Vector.fromList [] else diff --git a/fcore/game-type.sml b/fcore/game-type.sml index 6734082..e9bd219 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -1,38 +1,7 @@ signature GAME_TYPE = sig - datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int - - datatype player_attacked = NOT_ATTACKED | ATTACKED of int - - datatype main_attack = - MAIN_NOT_ATTACKING - | MAIN_ATTACKING of {length: int, growing: bool} - | MAIN_THROWING - - type defeated_enemies = {angle: int} - - type player_projectile = {x: int, y: int, facing: EntityType.facing} - - type player = - { yAxis: EntityType.y_axis - , xAxis: EntityType.x_axis - , recoil: player_recoil - , attacked: player_attacked - , mainAttack: main_attack - , mainAttackPressed: bool - , facing: EntityType.facing - , health: int - , x: int - , y: int - , jumpPressed: bool - , enemies: defeated_enemies vector - , charge: int - , projectiles: player_projectile vector - , platID: int - } - type game_type = - { player: player + { player: PlayerType.player , walls: Wall.t vector , wallTree: QuadTree.t , platforms: Platform.t vector @@ -47,39 +16,8 @@ end structure GameType :> GAME_TYPE = struct - datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int - - datatype player_attacked = NOT_ATTACKED | ATTACKED of int - - datatype main_attack = - MAIN_NOT_ATTACKING - | MAIN_ATTACKING of {length: int, growing: bool} - | MAIN_THROWING - - type defeated_enemies = {angle: int} - - type player_projectile = {x: int, y: int, facing: EntityType.facing} - - type player = - { yAxis: EntityType.y_axis - , xAxis: EntityType.x_axis - , recoil: player_recoil - , attacked: player_attacked - , mainAttack: main_attack - , mainAttackPressed: bool - , facing: EntityType.facing - , health: int - , x: int - , y: int - , jumpPressed: bool - , enemies: defeated_enemies vector - , charge: int - , projectiles: player_projectile vector - , platID: int - } - type game_type = - { player: player + { player: PlayerType.player , walls: Wall.t vector , wallTree: QuadTree.t , platforms: Platform.t vector @@ -101,9 +39,9 @@ struct { yAxis = EntityType.JUMPING 0 , xAxis = EntityType.STAY_STILL , facing = EntityType.FACING_RIGHT - , recoil = NO_RECOIL - , attacked = NOT_ATTACKED - , mainAttack = MAIN_NOT_ATTACKING + , recoil = PlayerType.NO_RECOIL + , attacked = PlayerType.NOT_ATTACKED + , mainAttack = PlayerType.MAIN_NOT_ATTACKING , mainAttackPressed = false , health = 3 , x = 500 diff --git a/fcore/physics.sml b/fcore/physics.sml index ecbdb4a..7569b28 100644 --- a/fcore/physics.sml +++ b/fcore/physics.sml @@ -24,7 +24,6 @@ end functor MakePhysics(Fn: PHYSICS_INPUT) = struct - open GameType open EntityType fun getPhysicsPatches input = @@ -220,7 +219,7 @@ end structure PlayerPhysics = MakePhysics (struct - type t = GameType.player + type t = PlayerType.player type patch = PlayerPatch.player_patch val entitySize = Constants.playerSize diff --git a/fcore/player-patch.sml b/fcore/player/player-patch.sml similarity index 91% rename from fcore/player-patch.sml rename to fcore/player/player-patch.sml index 39a51b3..98ce941 100644 --- a/fcore/player-patch.sml +++ b/fcore/player/player-patch.sml @@ -4,21 +4,21 @@ sig 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_RECOIL of PlayerType.player_recoil + | W_ATTACKED of PlayerType.player_attacked + | W_MAIN_ATTACK of PlayerType.main_attack | W_HEALTH of int | W_X of int | W_Y of int | W_JUMP_PRESSED of bool | W_MAIN_ATTACK_PRESSED of bool - | W_ENEMIES of GameType.defeated_enemies vector + | W_ENEMIES of PlayerType.defeated_enemies vector | W_CHARGE of int - | W_PROJECTILES of GameType.player_projectile vector + | W_PROJECTILES of PlayerType.player_projectile vector | W_PLAT_ID of int - val withPatch: GameType.player * player_patch -> GameType.player - val withPatches: GameType.player * player_patch list -> GameType.player + val withPatch: PlayerType.player * player_patch -> PlayerType.player + val withPatches: PlayerType.player * player_patch list -> PlayerType.player end structure PlayerPatch: PLAYER_PATCH = @@ -27,17 +27,17 @@ struct 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_RECOIL of PlayerType.player_recoil + | W_ATTACKED of PlayerType.player_attacked + | W_MAIN_ATTACK of PlayerType.main_attack | W_HEALTH of int | W_X of int | W_Y of int | W_JUMP_PRESSED of bool | W_MAIN_ATTACK_PRESSED of bool - | W_ENEMIES of GameType.defeated_enemies vector + | W_ENEMIES of PlayerType.defeated_enemies vector | W_CHARGE of int - | W_PROJECTILES of GameType.player_projectile vector + | W_PROJECTILES of PlayerType.player_projectile vector | W_PLAT_ID of int fun mkPlayer @@ -367,7 +367,7 @@ struct ) end - fun withPatches (player: GameType.player, lst) = + fun withPatches (player: PlayerType.player, lst) = case lst of hd :: tl => let val player = withPatch (player, hd) diff --git a/fcore/player/player-type.sml b/fcore/player/player-type.sml new file mode 100644 index 0000000..4afa1d9 --- /dev/null +++ b/fcore/player/player-type.sml @@ -0,0 +1,33 @@ +structure PlayerType = +struct + datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int + + datatype player_attacked = NOT_ATTACKED | ATTACKED of int + + datatype main_attack = + MAIN_NOT_ATTACKING + | MAIN_ATTACKING of {length: int, growing: bool} + | MAIN_THROWING + + type defeated_enemies = {angle: int} + + type player_projectile = {x: int, y: int, facing: EntityType.facing} + + type player = + { yAxis: EntityType.y_axis + , xAxis: EntityType.x_axis + , recoil: player_recoil + , attacked: player_attacked + , mainAttack: main_attack + , mainAttackPressed: bool + , facing: EntityType.facing + , health: int + , x: int + , y: int + , jumpPressed: bool + , enemies: defeated_enemies vector + , charge: int + , projectiles: player_projectile vector + , platID: int + } +end diff --git a/fcore/player.sml b/fcore/player/player.sml similarity index 99% rename from fcore/player.sml rename to fcore/player/player.sml index 224199c..957a6f2 100644 --- a/fcore/player.sml +++ b/fcore/player/player.sml @@ -1,8 +1,8 @@ structure Player = struct - open GameType open PlayerPatch open EntityType + open PlayerType (* helper functions checking input *) fun getXAxis (lh, rh) = @@ -378,7 +378,7 @@ struct fun fold (_, (), defeatedList) = {angle = 1} :: defeatedList end) - fun runPhysicsAndInput (game: game_type, input, enemyTree) = + fun runPhysicsAndInput (game: GameType.game_type, input, enemyTree) = let val player = #player game diff --git a/fcore/projectile.sml b/fcore/projectile.sml index f4bb28a..21be11f 100644 --- a/fcore/projectile.sml +++ b/fcore/projectile.sml @@ -44,7 +44,7 @@ struct (pos + 1, projectiles, width, height, ratio, xOffset, yOffset, acc) end - fun getProjectileVec (player: GameType.player, width, height) = + fun getProjectileVec (player: PlayerType.player, width, height) = let val {projectiles, ...} = player diff --git a/oms.mlb b/oms.mlb index 8ee47ab..76f0e43 100644 --- a/oms.mlb +++ b/oms.mlb @@ -29,18 +29,21 @@ fcore/path-finding.sml fcore/entity-type.sml fcore/enemy/enemy-type.sml -fcore/enemy/enemy.sml +fcore/enemy/enemy-pair.sml fcore/enemy/enemy-map.sml + +fcore/player/player-type.sml +fcore/enemy/enemy.sml fcore/game-type.sml -fcore/player-patch.sml +fcore/player/player-patch.sml fcore/enemy/enemy-patch.sml fcore/physics.sml fcore/trace-jump.sml fcore/enemy/enemy-behaviour.sml fcore/enemy/falling-enemies.sml -fcore/player.sml +fcore/player/player.sml fcore/projectile.sml fcore/game-update.sml