player: Added basic movement and wall collisions
authorLukas Krickl <lukas@krickl.dev>
Mon, 12 Jan 2026 13:38:58 +0000 (14:38 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 12 Jan 2026 13:38:58 +0000 (14:38 +0100)
src/actor.s
src/map.s
src/player.s
src/wram.s

index 3b4a447b490a7288df1d85b00c150ff38e6bdfdf..cc22617695f7438369cd6d38955aef537d40cc07 100644 (file)
@@ -319,3 +319,72 @@ act_load_attr_table:
        ; bc = attribute table
 
        ret
+
+       ; attempts to move an actor
+       ; to a new y/x position.
+       ; aborts the move if the position 
+       ; is inside a wall tile or the tile is already occupied
+       ; unsets t_act on current tile. sets t_act on new tile
+       ; inputs:
+       ;               de: actor ptr
+       ;               bc: new position
+       ;       returns:
+       ;               a: 0 -> moved
+       ;               a: 1 -> not moved 
+       ;               col_tile: ptr to new tile
+act_move_to:
+       ld hl, act_pos_y
+       add hl, de
+
+       push hl
+       push bc
+       
+       call map_get_tile
+       ; write col tile
+       ld a, h
+       ld [col_tile], a
+       ld a, l
+       ld [col_tile+1], a
+
+       ; hl = tile
+       push hl
+
+       ; check flags
+       ld de, t_flags0
+       add hl, de
+       ld a, [hl]
+       ; if wall flag do not move
+       and a, TF0_WALL
+       jp nz, @hit
+       pop hl
+
+       ; check actor ptr
+       ld de, t_act
+       add hl, de
+
+       ; if not NULL do not move
+       ld a, [hl+]
+       or a, [hl]
+       jp nz, @hit
+
+
+       pop bc
+       pop de ; de = actor ptr
+       
+
+       ; write new position
+       ld a, b
+       ld [de], a
+       inc de
+       ld a, c
+       ld [de], a
+
+       ld a, 0
+       ret
+@hit:
+       ; clean up stack
+       pop de
+       pop de 
+       pop de
+       ld a, 1
+       ret
index d6324492d70b67baa48030a39dead9eb5e1fef20..10f4e21ec8e938cd9271564cc517adc7143929cc 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -33,9 +33,12 @@ map_load:
        call vblank_wait
        call enableinterrupts
        
+       call map_uncover
+
        call map_full_draw
        call update_render
 
+
        ret
        
        ; loads all map related data that 
@@ -478,8 +481,14 @@ map_full_draw:
        call update_render
        ret
        
+       ; uncovers tiles starting at player position
+       ; and moving in all non-uncovered tiles until walls are hit
+map_uncover:
+       ret
+       
        ; nop map rotuine
 map_r_nop:
        ret
 
 
+
index 375a7f7ed704fb1b493cef16b1b47d2f5fb7f50e..9c96a46b918c065dcf5150d37cc402e60584c786 100644 (file)
@@ -2,7 +2,7 @@
 
        ; sets up the player actor
 player_init:
-       xor a, a
+       ld a, 2
        ld [player+act_pos_y], a
        ld [player+act_pos_x], a
 
@@ -107,8 +107,18 @@ player_handle_move:
        jr z, @not_left REL
                ld a, [player+act_pos_x]
                dec a
-               ld [player+act_pos_x], a
-               call map_full_draw
+               ld c, a
+
+               ld a, [player+act_pos_y]
+               ld b, a
+
+
+               ld de, player
+               call act_move_to
+
+               cp a, 0
+               jp z, player_moved
+               jp player_collided
 @not_left:
 
        ld b, DIRRIGHT
@@ -116,8 +126,17 @@ player_handle_move:
        jr z, @not_right REL
                ld a, [player+act_pos_x]
                inc a
-               ld [player+act_pos_x], a
-               call map_full_draw
+               ld c, a
+
+               ld a, [player+act_pos_y]
+               ld b, a
+
+               ld de, player
+               call act_move_to
+               
+               cp a, 0
+               jp z, player_moved
+               jp player_collided
 @not_right:
 
        ld b, DIRUP
@@ -125,8 +144,17 @@ player_handle_move:
        jr z, @not_up REL
                ld a, [player+act_pos_y]
                dec a
-               ld [player+act_pos_y], a
-               call map_full_draw
+               ld b, a
+
+               ld a, [player+act_pos_x]
+               ld c, a
+
+               ld de, player
+               call act_move_to
+
+               cp a, 0
+               jp z, player_moved
+               jp player_collided
 @not_up:
 
        ld b, DIRDOWN
@@ -134,8 +162,48 @@ player_handle_move:
        jr z, @not_down REL
                ld a, [player+act_pos_y]
                inc a
-               ld [player+act_pos_y], a
-               call map_full_draw
+               ld b, a
+               
+               ld a, [player+act_pos_x]
+               ld c, a
+               
+               ld de, player
+               call act_move_to
+
+               cp a, 0
+               jp z, player_moved
+               jp player_collided
 @not_down:
        ret
        
+       ; code to run when player has collided
+player_collided:
+       ld a, [col_tile]
+       ld d, a
+       ld a, [col_tile+1]
+       ld e, a
+       ; de = collision tile
+
+       ld a, [de] 
+       cp a, TT_DOOR ; if its a door run special code
+       jp z, @open_door
+       
+       ret
+@open_door:
+       ld a, TT_FLOOR
+       ld [de], a
+
+       ld hl, t_flags0
+       add hl, de 
+       ; clear all flags
+       xor a, a
+       ld [hl], a
+
+       call map_uncover
+       call map_full_draw
+       ret
+
+       ; code to run when player has moved
+player_moved:
+       call map_full_draw
+       ret
index 0e8da633459e248eafed7f8977282c060b1cf65f..085e22b276d4ce645d76b19914e629d7f690b69d 100644 (file)
@@ -116,6 +116,9 @@ combat: .adv combat_size
 
 render_buffer: .adv RENDER_BUF_TILES
 
+col_actor: .adv 2
+col_tile: .adv 2
+
 #ifdef DEBUG_CANARY
 render_canary: .adv 4
 #endif