From a7c2e2ef013e8321a50330cf4c5b1d63b106b982 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sun, 12 Jan 2025 07:18:44 +0000 Subject: [PATCH] progress moving constant values into own module --- fcore/constants.sml | 25 ++++++++++++++++ fcore/game-type.sml | 24 +++++++-------- fcore/physics.sml | 73 +++++++++++++++++++++++++++++++++++++++++++++ fcore/player.sml | 29 ++++++++---------- oms.mlb | 3 ++ 5 files changed, 125 insertions(+), 29 deletions(-) create mode 100644 fcore/constants.sml create mode 100644 fcore/physics.sml diff --git a/fcore/constants.sml b/fcore/constants.sml new file mode 100644 index 0000000..82ed69c --- /dev/null +++ b/fcore/constants.sml @@ -0,0 +1,25 @@ +structure Constants = +struct + val worldWidth = 1920 + val worldHeight = 1080 + + (* constants for player *) + val playerSize = 35 + val playerSizeReal = 35.0 + val halfPlayerSize = 35 div 2 + val halfPlayerSizeReal = 35.0 / 2.0 + val movePlayerBy = 5 + + (* player timing values *) + val jumpLimit = 150 + val floatLimit = 3 + val recoilLimit = 15 + val attackedLimit = 55 + val maxCharge = 60 + + (* constants for projectiles *) + val projectilePi: Real32.real = Real32.Math.pi / 180.0 + val projectileSize: Real32.real = 9.0 + val projectileDistance: Real32.real = 13.0 + val projectileSizeInt = 9 +end diff --git a/fcore/game-type.sml b/fcore/game-type.sml index ad460c5..e4e66f7 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -4,14 +4,14 @@ sig type platform = {id: int, x: int, y: int, width: int} - datatype player_y_axis = + datatype y_axis = ON_GROUND | FALLING | DROP_BELOW_PLATFORM | JUMPING of int | FLOATING of int - datatype player_x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT + datatype x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int @@ -30,8 +30,8 @@ sig type player_projectile = {x: int, y: int, facing: facing} type player = - { yAxis: player_y_axis - , xAxis: player_x_axis + { yAxis: y_axis + , xAxis: x_axis , recoil: player_recoil , attacked: player_attacked , mainAttack: main_attack @@ -47,8 +47,8 @@ sig } datatype player_patch = - W_X_AXIS of player_x_axis - | W_Y_AXIS of player_y_axis + W_X_AXIS of x_axis + | W_Y_AXIS of y_axis | W_RECOIL of player_recoil | W_ATTACKED of player_attacked | W_MAIN_ATTACK of main_attack @@ -82,14 +82,14 @@ struct (* all platforms have a fixed visual height and a fixed collision height *) type platform = {id: int, x: int, y: int, width: int} - datatype player_y_axis = + datatype y_axis = ON_GROUND | FALLING | DROP_BELOW_PLATFORM | JUMPING of int | FLOATING of int - datatype player_x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT + datatype x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT datatype player_recoil = NO_RECOIL | RECOIL_LEFT of int | RECOIL_RIGHT of int @@ -108,8 +108,8 @@ struct type player_projectile = {x: int, y: int, facing: facing} type player = - { yAxis: player_y_axis - , xAxis: player_x_axis + { yAxis: y_axis + , xAxis: x_axis , recoil: player_recoil , attacked: player_attacked , mainAttack: main_attack @@ -125,8 +125,8 @@ struct } datatype player_patch = - W_X_AXIS of player_x_axis - | W_Y_AXIS of player_y_axis + W_X_AXIS of x_axis + | W_Y_AXIS of y_axis | W_RECOIL of player_recoil | W_ATTACKED of player_attacked | W_MAIN_ATTACK of main_attack diff --git a/fcore/physics.sml b/fcore/physics.sml new file mode 100644 index 0000000..bf2375b --- /dev/null +++ b/fcore/physics.sml @@ -0,0 +1,73 @@ +signature PHYSICS_INPUT = +sig + type t + type patch + + (* constants for physics *) + val moveBy: int + val floatLimit: int + val jumpLimit: int + + (* destructuring functions *) + val getX: t -> int + val getY: t -> int + val getXAxis: t -> GameType.x_axis + val getYAxis: t -> GameType.y_axis + + val W_X: int -> patch + val W_Y: int -> patch + val W_Y_AXIS: GameType.y_axis -> patch +end + +functor MakePhysics(Fn: PHYSICS_INPUT) = +struct + open GameType + + fun run input = + let + val x = Fn.getX input + val y = Fn.getY input + val xAxis = Fn.getXAxis input + val yAxis = Fn.getYAxis input + + val desiredX = + case xAxis of + STAY_STILL => x + | MOVE_LEFT => x - Fn.moveBy + | MOVE_RIGHT => x + Fn.moveBy + in + case yAxis of + ON_GROUND => [Fn.W_X desiredX] + | FLOATING floated => + let + val yAxis = + if floated = Fn.floatLimit then FALLING + else FLOATING (floated + 1) + in + [Fn.W_X desiredX, Fn.W_Y_AXIS yAxis] + end + | FALLING => + let val desiredY = y + Fn.moveBy + in [Fn.W_X desiredX, Fn.W_Y desiredY] + end + | DROP_BELOW_PLATFORM => + let val desiredY = y + Fn.moveBy + in [Fn.W_X desiredX, Fn.W_Y desiredY] + end + | JUMPING jumped => + if jumped + Fn.moveBy > Fn.jumpLimit then + (* if we are above the jump limit, trigger a fall *) + let val newYAxis = FLOATING 0 + in [Fn.W_X desiredX, Fn.W_Y_AXIS newYAxis] + end + else + (* jump *) + let + val newJumped = jumped + Fn.moveBy + val newYAxis = JUMPING newJumped + val desiredY = y - Fn.moveBy + in + [Fn.W_X desiredX, Fn.W_Y desiredY, Fn.W_Y_AXIS newYAxis] + end + end +end diff --git a/fcore/player.sml b/fcore/player.sml index 49c7082..6191e16 100644 --- a/fcore/player.sml +++ b/fcore/player.sml @@ -294,12 +294,6 @@ struct val moveBy = 5 - (* defeated enemy constants *) - val defeatedPi = Real32.Math.pi / 180.0 - val defeatedSize = 9.0 - val defeatedDistance = 13.0 - val defeatedSizeInt = 9 - (* timing variables; always start at 0, * and revert to default state when limit is hit *) val jumpLimit = 150 @@ -422,10 +416,9 @@ struct (* only checks for collisions with environment (walls and platforms) *) fun getEnvironmentPatches (player, game) = let - val {walls, wallTree, platformTree, platforms, enemies, ...} = - game + val {walls, wallTree, platformTree, platforms, ...} = game - val {x, y, attacked, mainAttack, ...} = player + val {x, y, ...} = player val platCollisions = QuadTree.getCollisionsBelow (x, y, size, size, 0, 0, 1920, 1080, 0, platformTree) @@ -521,7 +514,7 @@ struct else if chargeHeld andalso not attackHeld then W_MAIN_ATTACK MAIN_CHARGING else W_MAIN_ATTACK MAIN_NOT_ATTACKING - fun degreesToRadians degrees = Real32.fromInt degrees * defeatedPi + fun degreesToRadians degrees = Real32.fromInt degrees * Constants.projectilePi fun defeatedEnemiesToProjectiles (pos, defeteadEnemies, player as {x, y, facing, ...}, acc) = @@ -529,15 +522,15 @@ struct Vector.fromList acc else let - val diff = halfRealSize - (defeatedSize / 2.0) + val diff = halfRealSize - (Constants.projectileSize / 2.0) val x = Real32.fromInt x + diff val y = Real32.fromInt y + diff val {angle} = Vector.sub (defeteadEnemies, pos) val angle = degreesToRadians angle - val x = ((Real32.Math.cos angle) * defeatedDistance) + x - val y = ((Real32.Math.sin angle) * defeatedDistance) + y + val x = ((Real32.Math.cos angle) * Constants.projectileDistance) + x + val y = ((Real32.Math.sin angle) * Constants.projectileDistance) + y val x = Real32.toInt IEEEReal.TO_NEAREST x val y = Real32.toInt IEEEReal.TO_NEAREST y @@ -934,13 +927,15 @@ struct val angle = degreesToRadians angle (* calculate pellet's x and y *) - val pelletX = ((Real32.Math.cos angle) * defeatedDistance) + playerX + val pelletX = + ((Real32.Math.cos angle) * Constants.projectileDistance) + playerX val pelletX = pelletX * ratio + xOffset - val pelletY = ((Real32.Math.sin angle) * defeatedDistance) + playerY + val pelletY = + ((Real32.Math.sin angle) * Constants.projectileDistance) + playerY val pelletY = pelletY * ratio + yOffset - val defeatedSize = defeatedSize * ratio + val defeatedSize = Constants.projectileSize * ratio val vec = Field.lerp ( pelletX @@ -978,7 +973,7 @@ struct val {x, y, enemies, ...} = player (* get centre (x, y) coordinates of player *) - val diff = halfRealSize - (defeatedSize / 2.0) + val diff = halfRealSize - (Constants.projectileSize / 2.0) val x = Real32.fromInt x + diff val y = Real32.fromInt y + diff diff --git a/oms.mlb b/oms.mlb index ef2e24e..73f8f59 100644 --- a/oms.mlb +++ b/oms.mlb @@ -1,6 +1,7 @@ $(SML_LIB)/basis/basis.mlb (* fcore *) +fcore/constants.sml fcore/quad-tree.sml fcore/bin-search.sml @@ -37,3 +38,5 @@ shell/input-state.sml shell/gl-shaders.sml shell/gl-draw.sml shell/shell.sml + +fcore/physics.sml