actor: Added basic collision check when moving
authorLukas Krickl <lukas@krickl.dev>
Thu, 27 Nov 2025 11:41:10 +0000 (12:41 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 27 Nov 2025 11:41:10 +0000 (12:41 +0100)
src/actor.s
src/defs.s
src/player.s
src/tiles.s

index 74bf10ed962ea0da8dd16950955e2a3e3d1088e0..e19477542c9c048ad7e13ea5c4ba6e5799676ea0 100644 (file)
@@ -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:
        
index 6234d0943317346cde6a447ef7a722e52ab7303e..0343f359ee6479bd60a8018333953be24f71839c 100644 (file)
 
        ; tile flags
 .se 1
+.de TF_WALL, 1
 
        ; tile struct
 .se 0 
index ff68ce323c4c61ea2f76b51de20a832d32771b32..536f234515014d03ebb20124b29c3daeab68808c 100644 (file)
@@ -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
index 9014bfc48e8f19e96d65976f91b1b8b262925caa..e383c9e79d3de620b2f5a90dd37e57db1a343bdf 100644 (file)
@@ -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