functorise physics for player
This commit is contained in:
@@ -23,7 +23,7 @@ functor MakePhysics(Fn: PHYSICS_INPUT) =
|
|||||||
struct
|
struct
|
||||||
open GameType
|
open GameType
|
||||||
|
|
||||||
fun run input =
|
fun getPatches input =
|
||||||
let
|
let
|
||||||
val x = Fn.getX input
|
val x = Fn.getX input
|
||||||
val y = Fn.getY input
|
val y = Fn.getY input
|
||||||
@@ -71,3 +71,26 @@ struct
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
structure PlayerPhysics =
|
||||||
|
MakePhysics
|
||||||
|
(struct
|
||||||
|
type t = GameType.player
|
||||||
|
type patch = GameType.player_patch
|
||||||
|
|
||||||
|
(* constants for physics *)
|
||||||
|
val moveBy = Constants.movePlayerBy
|
||||||
|
val floatLimit = Constants.floatLimit
|
||||||
|
val jumpLimit = Constants.jumpLimit
|
||||||
|
|
||||||
|
(* destructuring functions *)
|
||||||
|
fun getX ({x, ...}: t) = x
|
||||||
|
fun getY ({y, ...}: t) = y
|
||||||
|
|
||||||
|
fun getXAxis ({xAxis, ...}: t) = xAxis
|
||||||
|
fun getYAxis ({yAxis, ...}: t) = yAxis
|
||||||
|
|
||||||
|
val W_X = GameType.W_X
|
||||||
|
val W_Y = GameType.W_Y
|
||||||
|
val W_Y_AXIS = GameType.W_Y_AXIS
|
||||||
|
end)
|
||||||
|
|||||||
@@ -417,51 +417,6 @@ struct
|
|||||||
checkWalls (player, walls, wallCollisions, acc)
|
checkWalls (player, walls, wallCollisions, acc)
|
||||||
end
|
end
|
||||||
|
|
||||||
fun getMovePatches player =
|
|
||||||
let
|
|
||||||
val {xAxis, yAxis, x, y, ...} = player
|
|
||||||
|
|
||||||
val desiredX =
|
|
||||||
case xAxis of
|
|
||||||
STAY_STILL => x
|
|
||||||
| MOVE_LEFT => x - Constants.movePlayerBy
|
|
||||||
| MOVE_RIGHT => x + Constants.movePlayerBy
|
|
||||||
in
|
|
||||||
case yAxis of
|
|
||||||
ON_GROUND => [W_X desiredX]
|
|
||||||
| FLOATING floated =>
|
|
||||||
let
|
|
||||||
val yAxis =
|
|
||||||
if floated = Constants.floatLimit then FALLING
|
|
||||||
else FLOATING (floated + 1)
|
|
||||||
in
|
|
||||||
[W_X desiredX, W_Y_AXIS yAxis]
|
|
||||||
end
|
|
||||||
| FALLING =>
|
|
||||||
let val desiredY = y + Constants.movePlayerBy
|
|
||||||
in [W_X desiredX, W_Y desiredY]
|
|
||||||
end
|
|
||||||
| DROP_BELOW_PLATFORM =>
|
|
||||||
let val desiredY = y + Constants.movePlayerBy
|
|
||||||
in [W_X desiredX, W_Y desiredY]
|
|
||||||
end
|
|
||||||
| JUMPING jumped =>
|
|
||||||
if jumped + Constants.movePlayerBy > Constants.jumpLimit then
|
|
||||||
(* if we are above the jump limit, trigger a fall *)
|
|
||||||
let val newYAxis = FLOATING 0
|
|
||||||
in [W_X desiredX, W_Y_AXIS newYAxis]
|
|
||||||
end
|
|
||||||
else
|
|
||||||
(* jump *)
|
|
||||||
let
|
|
||||||
val newJumped = jumped + Constants.movePlayerBy
|
|
||||||
val newYAxis = JUMPING newJumped
|
|
||||||
val desiredY = y - Constants.movePlayerBy
|
|
||||||
in
|
|
||||||
[W_X desiredX, W_Y desiredY, W_Y_AXIS newYAxis]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
fun getJumpPatches (player, upHeld, downHeld, acc) =
|
fun getJumpPatches (player, upHeld, downHeld, acc) =
|
||||||
let
|
let
|
||||||
val {yAxis, jumpPressed, ...} = player
|
val {yAxis, jumpPressed, ...} = player
|
||||||
@@ -759,7 +714,7 @@ struct
|
|||||||
withPatches (player, patches)
|
withPatches (player, patches)
|
||||||
end
|
end
|
||||||
|
|
||||||
val patches = getMovePatches player
|
val patches = PlayerPhysics.getPatches player
|
||||||
val player = withPatches (player, patches)
|
val player = withPatches (player, patches)
|
||||||
|
|
||||||
val patches = getEnvironmentPatches (player, game)
|
val patches = getEnvironmentPatches (player, game)
|
||||||
|
|||||||
3
oms.mlb
3
oms.mlb
@@ -17,6 +17,7 @@ fcore/platform.sml
|
|||||||
fcore/enemy.sml
|
fcore/enemy.sml
|
||||||
fcore/game-type.sml
|
fcore/game-type.sml
|
||||||
|
|
||||||
|
fcore/physics.sml
|
||||||
fcore/player.sml
|
fcore/player.sml
|
||||||
fcore/projectile.sml
|
fcore/projectile.sml
|
||||||
|
|
||||||
@@ -38,5 +39,3 @@ shell/input-state.sml
|
|||||||
shell/gl-shaders.sml
|
shell/gl-shaders.sml
|
||||||
shell/gl-draw.sml
|
shell/gl-draw.sml
|
||||||
shell/shell.sml
|
shell/shell.sml
|
||||||
|
|
||||||
fcore/physics.sml
|
|
||||||
|
|||||||
Reference in New Issue
Block a user