movement: Fixed tile collision checks and exits
authorLukas Krickl <lukas@krickl.dev>
Fri, 19 Sep 2025 05:00:04 +0000 (07:00 +0200)
committerLukas Krickl <lukas@krickl.dev>
Fri, 19 Sep 2025 05:00:04 +0000 (07:00 +0200)
src/player.s
src/unit.s

index 4a6c82d88fe86b4e4d9ca6e314c0d3dedf3640ee..314363fa827bf5f4436acd96209ce12d0c484642 100644 (file)
@@ -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]
index da6fcf96f4ef68dab2a4252ca47bef6835d0869e..ac1f0ddcc9506623b1180bf2f7f0b2376eb7d31e 100644 (file)
@@ -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