From cc064d97b87eb0628f9f74b92bcbfce0208adef2 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Fri, 19 Sep 2025 07:00:04 +0200 Subject: [PATCH] movement: Fixed tile collision checks and exits --- src/player.s | 17 +++++++++++++---- src/unit.s | 20 ++++++++++++-------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/player.s b/src/player.s index 4a6c82d..314363f 100644 --- a/src/player.s +++ b/src/player.s @@ -117,6 +117,7 @@ unit_player_remove_door: ld hl, act_rt_collision_pos_y add hl, de ld a, [hl] ; load y offset + div16 a ; if y is at bottom of map go back up a tile... cp a, MAP_H-1 ; @@ -161,18 +162,22 @@ unit_check_exit_hit: ; hl = act_pos_y ld a, [hl] + div16 a cp a, 0 ; top jp z, unit_exit_top ld a, [hl+] + div16 a cp a, MAP_H-1 jp z, unit_exit_bottom ld a, [hl] ; hl = x pos + div16 a cp a, 0 jp z, unit_exit_left ld a, [hl] + div16 a cp a, MAP_W-1 jp z, unit_exit_right @@ -221,7 +226,8 @@ unit_exit_top: add hl, de ; move player down - ld a, MAP_H-1 + ld a, MAP_H-2 + mul16 a ld [hl], a ld a, [player_map_cursor] @@ -247,7 +253,8 @@ unit_exit_bottom: add hl, de ; move player up - ld a, 0 + ld a, 1 + mul16 a ld [hl], a ld a, [player_map_cursor] @@ -271,7 +278,8 @@ unit_exit_right: add hl, de ; move player left - ld a, 0 + ld a, 1 + mul16 a ld [hl], a ld a, [player_map_cursor] @@ -295,7 +303,8 @@ unit_exit_left: add hl, de ; move player right - ld a, MAP_W-1 + ld a, MAP_W-2 + mul16 a ld [hl], a ld a, [player_map_cursor] diff --git a/src/unit.s b/src/unit.s index da6fcf9..ac1f0dd 100644 --- a/src/unit.s +++ b/src/unit.s @@ -155,8 +155,9 @@ unit_handle_inputs: ; before a move is attempted ; ret on nz ; inputs: - ; $1: dec/inc instruction - ; $2: collision mask for tile + ; $1: register containing coordinate + ; $2: change in register (e.g. +16/-16) + ; $3: collision mask for tile ; de: actor ; returns: ; unit_test_collision_cf_flags: 0 if not collided with a tile @@ -166,7 +167,10 @@ unit_handle_inputs: ; perform tile collision check push de call unit_get_pos - $1 + + ld a, $1 + add a, ($2 & 0xFF) + ld $1, a ; write y and x pos ld hl, act_rt_collision_pos_y @@ -182,7 +186,7 @@ unit_handle_inputs: ; set flags result pop hl ld [hl], a ; store last cf - and a, $2 + and a, $3 pop bc pop de ret nz @@ -296,7 +300,7 @@ unit_collides_with_any_other: ; actor position unit_try_move_up: ; y - 1 - unit_test_collision dec b, CF_COLLISION + unit_test_collision b, -16 & 0xFF, CF_COLLISION push de ld hl, act_pos_y @@ -317,7 +321,7 @@ unit_try_move_up: unit_try_move_down: ; y + 1 - unit_test_collision inc b, CF_COLLISION + unit_test_collision b, 16, CF_COLLISION ld hl, act_pos_y add hl, de @@ -335,7 +339,7 @@ unit_try_move_down: unit_try_move_left: ; x - 1 - unit_test_collision dec c, CF_COLLISION + unit_test_collision c, -16 & 0xFF, CF_COLLISION ld hl, act_pos_x add hl, de @@ -353,7 +357,7 @@ unit_try_move_left: unit_try_move_right: ; x + 1 - unit_test_collision inc c, CF_COLLISION + unit_test_collision c, 16, CF_COLLISION ld hl, act_pos_x add hl, de -- 2.30.2