From 424b80e28ce3e9993d88f66a6ee68418a0fca46c Mon Sep 17 00:00:00 2001 From: Forest Belton <65484+forestbelton@users.noreply.github.com> Date: Tue, 20 Jul 2021 18:02:01 -0400 Subject: [PATCH] Cap velocity at 7px/frame --- inc/util.inc | 9 +++++++++ src/player.s | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/inc/util.inc b/inc/util.inc index d67cce3..4d381c6 100644 --- a/inc/util.inc +++ b/inc/util.inc @@ -77,3 +77,12 @@ MACRO CP16 cp LOW(\1) .done\@ ENDM + +; A = MIN(A, x) +; \1 Other value +MACRO MIN + cp \1 + jr c, .skip\@ + ld a, \1 +.skip\@: +ENDM diff --git a/src/player.s b/src/player.s index a98dc1a..839e913 100644 --- a/src/player.s +++ b/src/player.s @@ -32,7 +32,8 @@ DEF TILE_SIZE EQU 16 ; Total length of tile in bytes DEF GRAVITY EQU (0 << 8) | $2e ; 0.18 DEF INIT_VY EQU (3 << 8) | $99 ; 3.60 -DEF INIT_FALL_VY EQU ($ff << 8) | $1a ; a.b +DEF INIT_FALL_VY EQU ($ff << 8) | $1a ; -1.1015625 +DEF MAX_VY EQU ($f9 << 8) | $00 ; -7.0 Player_Init:: ; Clear player data (X/Y initialized by map engine) @@ -91,6 +92,19 @@ Player_Update:: sbc HIGH(GRAVITY) ld b, a + ; we only need to clamp when moving down + bit 7, b + jr z, .no_clamp + + ; vy = MIN(MAX_VY, vy) + ld a, b + cp HIGH(MAX_VY) + jr c, .no_clamp + + ld b, HIGH(MAX_VY) + ld c, LOW(MAX_VY) + +.no_clamp: ld hl, PLAYER_VY STORE16 bc