From: Lukas Krickl Date: Mon, 12 Jan 2026 13:38:58 +0000 (+0100) Subject: player: Added basic movement and wall collisions X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=7ef5da51f84785f4011d0be5eb32188dacb3c8bd;p=gbrg%2F.git player: Added basic movement and wall collisions --- diff --git a/src/actor.s b/src/actor.s index 3b4a447..cc22617 100644 --- a/src/actor.s +++ b/src/actor.s @@ -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 diff --git a/src/map.s b/src/map.s index d632449..10f4e21 100644 --- 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 + diff --git a/src/player.s b/src/player.s index 375a7f7..9c96a46 100644 --- a/src/player.s +++ b/src/player.s @@ -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 diff --git a/src/wram.s b/src/wram.s index 0e8da63..085e22b 100644 --- a/src/wram.s +++ b/src/wram.s @@ -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