refactor constants out from player.sml

This commit is contained in:
2025-01-12 11:46:32 +00:00
parent a7c2e2ef01
commit ec44dd9966
4 changed files with 63 additions and 67 deletions

View File

@@ -2,12 +2,14 @@ structure Constants =
struct struct
val worldWidth = 1920 val worldWidth = 1920
val worldHeight = 1080 val worldHeight = 1080
val worldWidthReal: Real32.real = 1920.0
val worldHeightReal: Real32.real = 1080.0
(* constants for player *) (* constants for player *)
val playerSize = 35 val playerSize = 35
val playerSizeReal = 35.0 val playerSizeReal: Real32.real = 35.0
val halfPlayerSize = 35 div 2 val halfPlayerSize = 35 div 2
val halfPlayerSizeReal = 35.0 / 2.0 val halfPlayerSizeReal: Real32.real = 35.0 / 2.0
val movePlayerBy = 5 val movePlayerBy = 5
(* player timing values *) (* player timing values *)

View File

@@ -165,7 +165,7 @@ struct
, y = 500 , y = 500
, jumpPressed = false , jumpPressed = false
, enemies = Vector.fromList [] , enemies = Vector.fromList []
, charge = 60 , charge = Constants.maxCharge
, projectiles = Vector.fromList [] , projectiles = Vector.fromList []
} }

View File

@@ -29,8 +29,8 @@ struct
* and then chose appropriate direction to recoil in *) * and then chose appropriate direction to recoil in *)
let let
val {x, ...} = player val {x, ...} = player
val pFinishX = x + Player.size val pFinishX = x + Constants.playerSize
val pHalfW = Player.size div 2 val pHalfW = Constants.playerSize div 2
val pCentreX = x + pHalfW val pCentreX = x + pHalfW
val {x = ex, y = ey, ...} = Enemy.find (id, enemies) val {x = ex, y = ey, ...} = Enemy.find (id, enemies)
@@ -89,15 +89,15 @@ struct
fun checkCollisions (player, enemies, enemyTree, projectiles) = fun checkCollisions (player, enemies, enemyTree, projectiles) =
let let
val {x, y, mainAttack, attacked, ...} = player val {x, y, mainAttack, attacked, ...} = player
val size = Player.size val size = Constants.playerSize
val projectileTree = Projectile.generateTree projectiles val projectileTree = Projectile.generateTree projectiles
in in
case mainAttack of case mainAttack of
MAIN_ATTACKING => MAIN_ATTACKING =>
let let
(* when attacking, player collision should be larger than player themselves *) (* when attacking, player collision should be larger than player themselves *)
val x = x - Player.halfSize val x = x - Constants.halfPlayerSize
val y = y - Player.halfSize val y = y - Constants.halfPlayerSize
val size = size * 2 val size = size * 2
(* get list of enemies player has collided with *) (* get list of enemies player has collided with *)
@@ -142,7 +142,7 @@ struct
(player, enemies, enemyTree) (player, enemies, enemyTree)
end end
| ATTACKED amt => | ATTACKED amt =>
if amt = Player.attackedLimit then if amt = Constants.attackedLimit then
(* if reached limit, detect enemies again *) (* if reached limit, detect enemies again *)
let let
val enemyCollisions = QuadTree.getCollisions val enemyCollisions = QuadTree.getCollisions

View File

@@ -285,23 +285,6 @@ struct
end end
| [] => player | [] => player
(* width/height *)
val size = 35
val realSize = 35.0
val halfSize = 35 div 2
val halfRealSize = 35.0 / 2.0
val moveBy = 5
(* timing variables; always start at 0,
* and revert to default state when limit is hit *)
val jumpLimit = 150
val floatLimit = 3
val recoilLimit = 15
val attackedLimit = 55
val maxCharge = 60
(* helper functions checking input *) (* helper functions checking input *)
fun getXAxis (lh, rh) = fun getXAxis (lh, rh) =
case (lh, rh) of case (lh, rh) of
@@ -368,7 +351,7 @@ struct
val {x = wallX, width = wallWidth, ...} = val {x = wallX, width = wallWidth, ...} =
Vector.sub (walls, wallID - 1) Vector.sub (walls, wallID - 1)
val newX = wallX - size val newX = wallX - Constants.playerSize
val acc = W_X newX :: acc val acc = W_X newX :: acc
in in
checkWalls (player, walls, tl, acc) checkWalls (player, walls, tl, acc)
@@ -377,7 +360,7 @@ struct
let let
val {y = wallY, ...} = Vector.sub (walls, wallID - 1) val {y = wallY, ...} = Vector.sub (walls, wallID - 1)
val newY = wallY - size val newY = wallY - Constants.playerSize
val acc = W_Y_AXIS ON_GROUND :: W_Y newY :: acc val acc = W_Y_AXIS ON_GROUND :: W_Y newY :: acc
in in
checkWalls (player, walls, tl, acc) checkWalls (player, walls, tl, acc)
@@ -405,7 +388,7 @@ struct
* player will land on platform and stay on the ground there. *) * player will land on platform and stay on the ground there. *)
val {y = platY, ...} = Vector.sub (platforms, platID - 1) val {y = platY, ...} = Vector.sub (platforms, platID - 1)
val newY = platY - size val newY = platY - Constants.playerSize
val acc = W_Y_AXIS ON_GROUND :: W_Y newY :: acc val acc = W_Y_AXIS ON_GROUND :: W_Y newY :: acc
in in
checkPlatforms (player, platforms, tl, acc) checkPlatforms (player, platforms, tl, acc)
@@ -420,12 +403,16 @@ struct
val {x, y, ...} = player val {x, y, ...} = player
val size = Constants.playerSize
val ww = Constants.worldWidth
val wh = Constants.worldHeight
val platCollisions = QuadTree.getCollisionsBelow val platCollisions = QuadTree.getCollisionsBelow
(x, y, size, size, 0, 0, 1920, 1080, 0, platformTree) (x, y, size, size, 0, 0, ww, wh, 0, platformTree)
val acc = checkPlatforms (player, platforms, platCollisions, []) val acc = checkPlatforms (player, platforms, platCollisions, [])
val wallCollisions = QuadTree.getCollisionSides val wallCollisions = QuadTree.getCollisionSides
(x, y, size, size, 0, 0, 1920, 1080, 0, wallTree) (x, y, size, size, 0, 0, ww, wh, 0, wallTree)
in in
checkWalls (player, walls, wallCollisions, acc) checkWalls (player, walls, wallCollisions, acc)
end end
@@ -437,28 +424,29 @@ struct
val desiredX = val desiredX =
case xAxis of case xAxis of
STAY_STILL => x STAY_STILL => x
| MOVE_LEFT => x - moveBy | MOVE_LEFT => x - Constants.movePlayerBy
| MOVE_RIGHT => x + moveBy | MOVE_RIGHT => x + Constants.movePlayerBy
in in
case yAxis of case yAxis of
ON_GROUND => [W_X desiredX] ON_GROUND => [W_X desiredX]
| FLOATING floated => | FLOATING floated =>
let let
val yAxis = val yAxis =
if floated = floatLimit then FALLING else FLOATING (floated + 1) if floated = Constants.floatLimit then FALLING
else FLOATING (floated + 1)
in in
[W_X desiredX, W_Y_AXIS yAxis] [W_X desiredX, W_Y_AXIS yAxis]
end end
| FALLING => | FALLING =>
let val desiredY = y + moveBy let val desiredY = y + Constants.movePlayerBy
in [W_X desiredX, W_Y desiredY] in [W_X desiredX, W_Y desiredY]
end end
| DROP_BELOW_PLATFORM => | DROP_BELOW_PLATFORM =>
let val desiredY = y + moveBy let val desiredY = y + Constants.movePlayerBy
in [W_X desiredX, W_Y desiredY] in [W_X desiredX, W_Y desiredY]
end end
| JUMPING jumped => | JUMPING jumped =>
if jumped + moveBy > jumpLimit then if jumped + Constants.movePlayerBy > Constants.jumpLimit then
(* if we are above the jump limit, trigger a fall *) (* if we are above the jump limit, trigger a fall *)
let val newYAxis = FLOATING 0 let val newYAxis = FLOATING 0
in [W_X desiredX, W_Y_AXIS newYAxis] in [W_X desiredX, W_Y_AXIS newYAxis]
@@ -466,9 +454,9 @@ struct
else else
(* jump *) (* jump *)
let let
val newJumped = jumped + moveBy val newJumped = jumped + Constants.movePlayerBy
val newYAxis = JUMPING newJumped val newYAxis = JUMPING newJumped
val desiredY = y - moveBy val desiredY = y - Constants.movePlayerBy
in in
[W_X desiredX, W_Y desiredY, W_Y_AXIS newYAxis] [W_X desiredX, W_Y desiredY, W_Y_AXIS newYAxis]
end end
@@ -522,7 +510,8 @@ struct
Vector.fromList acc Vector.fromList acc
else else
let let
val diff = halfRealSize - (Constants.projectileSize / 2.0) val diff =
Constants.halfPlayerSizeReal - (Constants.projectileSize / 2.0)
val x = Real32.fromInt x + diff val x = Real32.fromInt x + diff
val y = Real32.fromInt y + diff val y = Real32.fromInt y + diff
@@ -630,7 +619,7 @@ struct
val charge = val charge =
case mainAttack of case mainAttack of
MAIN_CHARGING => Int.min (charge + 1, maxCharge) MAIN_CHARGING => Int.min (charge + 1, Constants.maxCharge)
| MAIN_ATTACKING => Int.max (charge - 1, 0) | MAIN_ATTACKING => Int.max (charge - 1, 0)
| _ => charge | _ => charge
@@ -660,7 +649,7 @@ struct
* However, if player has reached the recoil limit, exit the recoil * However, if player has reached the recoil limit, exit the recoil
* state and accept input. * state and accept input.
* *) * *)
if recoiled = recoilLimit then if recoiled = Constants.recoilLimit then
[W_RECOIL NO_RECOIL] [W_RECOIL NO_RECOIL]
else else
let let
@@ -685,7 +674,7 @@ struct
] ]
end end
| RECOIL_RIGHT recoiled => | RECOIL_RIGHT recoiled =>
if recoiled = recoilLimit then if recoiled = Constants.recoilLimit then
[W_RECOIL NO_RECOIL] [W_RECOIL NO_RECOIL]
else else
let let
@@ -715,15 +704,15 @@ struct
let let
val {x, y, facing} = Vector.sub (projectiles, pos) val {x, y, facing} = Vector.sub (projectiles, pos)
in in
if x <= 0 orelse x >= 1920 then if x <= 0 orelse x >= Constants.worldWidth then
(* filter out since projectile is not visible *) (* filter out since projectile is not visible *)
helpMoveProjectiles (pos - 1, projectiles, acc) helpMoveProjectiles (pos - 1, projectiles, acc)
else else
let let
val x = val x =
case facing of case facing of
FACING_LEFT => x - moveBy FACING_LEFT => x - Constants.movePlayerBy
| FACING_RIGHT => x + moveBy | FACING_RIGHT => x + Constants.movePlayerBy
val newTile = {x = x, y = y, facing = facing} val newTile = {x = x, y = y, facing = facing}
val acc = newTile :: acc val acc = newTile :: acc
@@ -821,12 +810,12 @@ struct
fun getDrawVec (player: player, width, height) = fun getDrawVec (player: player, width, height) =
let let
val {x, y, attacked, mainAttack, ...} = player val {x, y, attacked, mainAttack, ...} = player
val wratio = width / 1920.0 val wratio = width / Constants.worldWidthReal
val hratio = height / 1080.0 val hratio = height / Constants.worldHeightReal
in in
if wratio < hratio then if wratio < hratio then
let let
val scale = 1080.0 * wratio val scale = Constants.worldHeightReal * wratio
val yOffset = val yOffset =
if height > scale then (height - scale) / 2.0 if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0 else if height < scale then (scale - height) / 2.0
@@ -835,13 +824,13 @@ struct
val x = Real32.fromInt x * wratio val x = Real32.fromInt x * wratio
val y = Real32.fromInt y * wratio + yOffset val y = Real32.fromInt y * wratio + yOffset
val realSize = realSize * wratio val realSize = Constants.playerSizeReal * wratio
in in
helpGetDrawVec (x, y, realSize, width, height, attacked, mainAttack) helpGetDrawVec (x, y, realSize, width, height, attacked, mainAttack)
end end
else else
let let
val scale = 1920.0 * hratio val scale = Constants.worldWidthReal * hratio
val xOffset = val xOffset =
if width > scale then (width - scale) / 2.0 if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0 else if width < scale then (scale - width) / 2.0
@@ -850,7 +839,7 @@ struct
val x = Real32.fromInt x * hratio + xOffset val x = Real32.fromInt x * hratio + xOffset
val y = Real32.fromInt y * hratio val y = Real32.fromInt y * hratio
val realSize = realSize * hratio val realSize = Constants.playerSizeReal * hratio
in in
helpGetDrawVec (x, y, realSize, width, height, attacked, mainAttack) helpGetDrawVec (x, y, realSize, width, height, attacked, mainAttack)
end end
@@ -863,21 +852,23 @@ struct
| _ => | _ =>
let let
val {x, y, ...} = player val {x, y, ...} = player
val wratio = width / 1920.0 val wratio = width / Constants.worldWidthReal
val hratio = height / 1080.0 val hratio = height / Constants.worldHeightReal
in in
if wratio < hratio then if wratio < hratio then
let let
val scale = 1080.0 * wratio val scale = Constants.worldHeightReal * wratio
val yOffset = val yOffset =
if height > scale then (height - scale) / 2.0 if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0 else if height < scale then (scale - height) / 2.0
else 0.0 else 0.0
val x = (Real32.fromInt x - halfRealSize) * wratio val x = (Real32.fromInt x - Constants.halfPlayerSizeReal) * wratio
val y = (Real32.fromInt y - halfRealSize) * wratio + yOffset val y =
(Real32.fromInt y - Constants.halfPlayerSizeReal) * wratio
+ yOffset
val realSize = (realSize * 2.0) * wratio val realSize = (Constants.playerSizeReal * 2.0) * wratio
val {charge, ...} = player val {charge, ...} = player
val alpha = Real32.fromInt charge / 60.0 val alpha = Real32.fromInt charge / 60.0
@@ -887,16 +878,18 @@ struct
end end
else else
let let
val scale = 1920.0 * hratio val scale = Constants.worldWidthReal * hratio
val xOffset = val xOffset =
if width > scale then (width - scale) / 2.0 if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0 else if width < scale then (scale - width) / 2.0
else 0.0 else 0.0
val x = (Real32.fromInt x - halfRealSize) * hratio + xOffset val x =
val y = (Real32.fromInt y - halfRealSize) * hratio (Real32.fromInt x - Constants.halfPlayerSizeReal) * hratio
+ xOffset
val y = (Real32.fromInt y - Constants.halfPlayerSizeReal) * hratio
val realSize = (realSize * 2.0) * hratio val realSize = (Constants.playerSizeReal * 2.0) * hratio
val {charge, ...} = player val {charge, ...} = player
val alpha = Real32.fromInt charge / 60.0 val alpha = Real32.fromInt charge / 60.0
@@ -973,16 +966,17 @@ struct
val {x, y, enemies, ...} = player val {x, y, enemies, ...} = player
(* get centre (x, y) coordinates of player *) (* get centre (x, y) coordinates of player *)
val diff = halfRealSize - (Constants.projectileSize / 2.0) val diff =
Constants.halfPlayerSizeReal - (Constants.projectileSize / 2.0)
val x = Real32.fromInt x + diff val x = Real32.fromInt x + diff
val y = Real32.fromInt y + diff val y = Real32.fromInt y + diff
val wratio = width / 1920.0 val wratio = width / Constants.worldWidthReal
val hratio = height / 1080.0 val hratio = height / Constants.worldHeightReal
in in
if wratio < hratio then if wratio < hratio then
let let
val scale = 1080.0 * wratio val scale = Constants.worldHeightReal * wratio
val yOffset = val yOffset =
if height > scale then (height - scale) / 2.0 if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0 else if height < scale then (scale - height) / 2.0
@@ -993,7 +987,7 @@ struct
end end
else else
let let
val scale = 1920.0 * hratio val scale = Constants.worldWidthReal * hratio
val xOffset = val xOffset =
if width > scale then (width - scale) / 2.0 if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0 else if width < scale then (scale - width) / 2.0