From 468d62dc27508a2c921a1ed1b285ed6ae43ca3c0 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Thu, 27 Nov 2025 12:41:10 +0100 Subject: [PATCH] actor: Added basic collision check when moving --- src/actor.s | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/defs.s | 1 + src/player.s | 5 +++ src/tiles.s | 2 +- 4 files changed, 125 insertions(+), 1 deletion(-) diff --git a/src/actor.s b/src/actor.s index 74bf10e..e194775 100644 --- a/src/actor.s +++ b/src/actor.s @@ -1,4 +1,122 @@ + ; fixes y position + ; inputs: + ; a: direction + ; hl: ptr to y pos + ; returns: + ; fixed y pos + ; a is unchanged + ; hl: += 1 + ; b: y pos adjusted / 16 +_act_test_col_y: + push af + and a, DIRUP + jr nz, @up REL + + pop af + push af + and a, DIRDOWN + jr nz, @down REL + + ; no change + ld a, [hl+] + div16 a + ld b, a + + pop af + ret +@up: + ld a, [hl+] + div16 a + dec a + ld b, a + + pop af + ret +@down: + ld a, [hl+] + div16 a + inc a + ld b, a + + pop af + ret + + ; fixes x positions + ; inputs: + ; a: direction + ; hl: ptr to x pos + ; returns: + ; fixed x pos + ; a is unchanged + ; c: x pos adjusted / 16 +_act_test_col_x: + push af + + and a, DIRLEFT + jr nz, @left REL + + pop af + push af + and a, DIRRIGHT + jr nz, @right REL + + ; no change + ld a, [hl] + div16 a + + ld c, a + pop af + ret +@left: + ld a, [hl] + div16 a + dec a + ld c, a + pop af + ret +@right: + ld a, [hl] + div16 a + inc a + ld c, a + + pop af + ret + + ; test collision based on an actor + ; and a direction + ; inputs: + ; de: actor + ; a: direction + ; returns: + ; a: new direction + ; a: 0 if move should be aborted +act_test_collision: + push af + + ld hl, act_pos_y + add hl, de + + call _act_test_col_y + call _act_test_col_x + + ; b/c = y/x + call map_get_tile + + ld de, t_flags + add hl, de + ld a, [hl] + and a, TF_WALL + jr nz, @collision REL + + pop af + ret +@collision: + pop af + ld a, 0 + ret + ; update routines for each actor actor_map_update_table: diff --git a/src/defs.s b/src/defs.s index 6234d09..0343f35 100644 --- a/src/defs.s +++ b/src/defs.s @@ -104,6 +104,7 @@ ; tile flags .se 1 +.de TF_WALL, 1 ; tile struct .se 0 diff --git a/src/player.s b/src/player.s index ff68ce3..536f234 100644 --- a/src/player.s +++ b/src/player.s @@ -54,6 +54,11 @@ player_inputs: @notright: ret @set: + ld de, player + call act_test_collision + cp a, 0 + ret z + ld [player_direction], a ld a, 16 ; tile size ld [player_move_timer], a diff --git a/src/tiles.s b/src/tiles.s index 9014bfc..e383c9e 100644 --- a/src/tiles.s +++ b/src/tiles.s @@ -15,4 +15,4 @@ tile_grass: tiledef TT_EMPTY, 0, GFX_GRASS tile_wall: - tiledef TT_WALL, 0, GFX_WALL + tiledef TT_WALL, TF_WALL, GFX_WALL -- 2.30.2