actor: Added left tile collision code
authorLukas Krickl <lukas@krickl.dev>
Wed, 3 Dec 2025 08:27:40 +0000 (09:27 +0100)
committerLukas Krickl <lukas@krickl.dev>
Wed, 3 Dec 2025 08:27:40 +0000 (09:27 +0100)
src/actor.s
src/player.s
src/wram.s

index 796c39cd726b265b4857ba4e9a5eaec9906fdcf7..4157470728f1d4e547f76738ad0e4e72cb27321e 100644 (file)
-       ; 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:
index de85d0c7f50cf274879b9cc00aeffa8b61be6414..6846e37a835eae5854b32e68445f80aa1a185ae2 100644 (file)
@@ -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
        
index a75c72195470a9eb379567daca89a85179600e41..bc45e76ead16c5c22e7ecbc4b9aa11871f949563 100644 (file)
@@ -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