From 544460aec7b6d44076a3e1200c494b4b3d2e34ae Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Mon, 9 Dec 2024 04:37:40 +0000 Subject: [PATCH] begin coding player --- fcore/player.sml | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 fcore/player.sml diff --git a/fcore/player.sml b/fcore/player.sml new file mode 100644 index 0000000..97f71ca --- /dev/null +++ b/fcore/player.sml @@ -0,0 +1,57 @@ +structure Player = +struct + datatype y_axis = ON_GROUND | FALLING | JUMPING of int + datatype x_axis = MOVE_LEFT | STAY_STILL | MOVE_RIGHT + + (* width/height *) + val size = 35 + + val moveBy = 5 + val jumpLimit = 55 + + type t = {yAxis: y_axis, xAxis: x_axis, health: int, x: int, y: int} + + fun mkPlayer (health, xAxis, yAxis, x, y) = + {yAxis = yAxis, xAxis = xAxis, health = health, x = x, y = y} + + fun move (player: t) = + let + val {yAxis, xAxis, x, y, health} = player + + (* todo: check for wall and platform collisions + * in case analysis for both axis + * *) + val x = + case xAxis of + MOVE_LEFT => + (* todo: check if we are trying to move left + * even though player is against wall. + * In that case, keep same action (it is a sign for us to animate), + * but don't actually move leftwards. *) + x - moveBy + | MOVE_RIGHT => (* todo: check against wall *) x + moveBy + | STAY_STILL => x + in + case yAxis of + JUMPING jumped => + (* check if we hit jump limit; + * if we did, change to falling case. + * *) + if jumped + moveBy <= jumpLimit then + let + val jumped = jumped + moveBy + val yAxis = JUMPING jumped + val y = y + moveBy + in + mkPlayer (health, xAxis, yAxis, x, y) + end + else + mkPlayer (health, xAxis, FALLING, x, y) + | FALLING => + (* todo: keep decrementing and falling down + * until we hit ground or platform + * *) + mkPlayer (health, xAxis, yAxis, x, y - moveBy) + | ON_GROUND => mkPlayer (health, xAxis, yAxis, x, y) + end +end