From: Lukas Krickl Date: Wed, 3 Dec 2025 08:27:40 +0000 (+0100) Subject: actor: Added left tile collision code X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=62edddbd60d3908385f844d94b08acdcbe04c863;p=gbrg%2F.git actor: Added left tile collision code --- diff --git a/src/actor.s b/src/actor.s index 796c39c..4157470 100644 --- a/src/actor.s +++ b/src/actor.s @@ -1,112 +1,35 @@ - ; 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 - ; stores current position in tmp_prev_pos + ; stores current position in col_prev_pos ; inputs: ; de: actor act_backup_pos: ld hl, act_pos_y add hl, de ld a, [hl+] - ld [tmp_prev_pos], a ; y + ld [col_prev_pos], a ; y ld a, [hl] - ld [tmp_prev_pos+1], a ; x + ld [col_prev_pos+1], a ; x ret ; restores prev x pos ; inputs: ; de: actor act_rollback_pos_x: + ld hl, act_pos_x + add hl, de + ld a, [col_prev_pos+1] + ld [hl], a ret ; restores prev y pos ; inputs: ; de: actor act_rollback_pos_y: + ld hl, act_pos_y + add hl, de + ld a, [col_prev_pos] + ld [hl], a ret ; test collision based on an actor @@ -114,9 +37,27 @@ act_rollback_pos_y: ; inputs: ; de: actor ; col_point_xx: collision points + ; col_direction: set to direction moved in + ; col_prev_pos: set to previous positions ; if collision occures rolls back to ; previous position act_test_tile_collision: + ld a, [col_direction] + and a, DIRUP + call nz, _act_test_tile_up_collision + + ld a, [col_direction] + and a, DIRDOWN + call nz, _act_test_tile_down_collision + + ld a, [col_direction] + and a, DIRLEFT + call nz, _act_test_tile_left_collision + + ld a, [col_direction] + and a, DIRRIGHT + call nz, _act_test_tile_right_collision + ld hl, act_pos_y add hl, de @@ -130,6 +71,68 @@ act_test_tile_collision: and a, TF_WALL ret + + ; loads a collision point into bc + ; inputs: + ; $1: point name + ; returns: + ; b/c: y/x tile position +#macro _act_tile_col_load_pt + ld a, [$1] + div16 a + ld b, a + ld a, [$1+1] + div16 a + ld c, a +#endmacro + + ; test direction routines: + ; preserve: de +_act_test_tile_left_collision: + push de + + ; top left point + _act_tile_col_load_pt col_point_tl + + call map_get_tile + ld de, t_flags + add hl, de + ld a, [hl] + and a, TF_WALL + jr nz, @collision REL + + ; bottom left point + _act_tile_col_load_pt col_point_bl + + call map_get_tile + ld de, t_flags + add hl, de + ld a, [hl] + and a, TF_WALL + jr nz, @collision REL + + pop de + ret +@collision: + pop de + call act_rollback_pos_x + ret + +_act_test_tile_right_collision: + push de + + pop de + ret + +_act_test_tile_up_collision: + push de + pop de + ret + +_act_test_tile_down_collision: + push de + pop de + ret ; update routines for each actor act_map_update_table: diff --git a/src/player.s b/src/player.s index de85d0c..6846e37 100644 --- a/src/player.s +++ b/src/player.s @@ -73,6 +73,9 @@ player_update: ; does not move out of bounds ; based on the last queued input player_handle_move: + xor a, a + ld [col_direction], a + ld b, BTNDOWN input_held jr z, @not_down REL @@ -81,6 +84,10 @@ player_handle_move: jr z, @not_down REL inc a ld [player+act_pos_y], a + + ld a, [col_direction] + or a, DIRDOWN + ld [col_direction], a @not_down: ld b, BTNUP @@ -91,6 +98,10 @@ player_handle_move: jr z, @not_up REL dec a ld [player+act_pos_y], a + + ld a, [col_direction] + or a, DIRUP + ld [col_direction], a @not_up: ld b, BTNLEFT @@ -101,6 +112,10 @@ player_handle_move: jr z, @not_left REL dec a ld [player+act_pos_x], a + + ld a, [col_direction] + or a, DIRLEFT + ld [col_direction], a @not_left: ld b, BTNRIGHT @@ -111,6 +126,10 @@ player_handle_move: jr z, @not_right REL inc a ld [player+act_pos_x], a + + ld a, [col_direction] + or a, DIRRIGHT + ld [col_direction], a @not_right: ret diff --git a/src/wram.s b/src/wram.s index a75c721..bc45e76 100644 --- a/src/wram.s +++ b/src/wram.s @@ -96,9 +96,13 @@ col_point_tl: .adv 2 col_point_tr: .adv 2 col_point_bl: .adv 2 col_point_br: .adv 2 + ; direction the movement is happening + ; in. this is required so that the collision code + ; can handle pushbacks in case of wall collision +col_direction: .adv 2 ; y/x previous position -tmp_prev_pos: .adv 2 +col_prev_pos: .adv 2 ; space for 4 tile ids