From 5bfa8d0458b15edcec8bca6cda33b00f5fd0ce31 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 15 Dec 2025 13:48:29 +0100 Subject: [PATCH] player: wip movement on grid --- src/actor.s | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/debug.s | 49 +++++++++++++++++++++++++++++ src/levels.s | 2 +- src/map.s | 5 ++- src/player.s | 19 ++++++++++- src/tiles.s | 33 ++++++++++++++++++- src/ui.s | 2 +- 7 files changed, 191 insertions(+), 8 deletions(-) diff --git a/src/actor.s b/src/actor.s index ad76094..d49ee35 100644 --- a/src/actor.s +++ b/src/actor.s @@ -11,8 +11,30 @@ act_dir_vector: .db 0x10 ; NORTH .db 0x09 ; WEST .db 0x01 ; EAST + + ; reverse direction +act_dir_reverse: + .db NORTH ; SOUTH + .db SOUTH ; NORTH + .db EAST ; WEST + .db WEST ; EAST + + + ; tile flags to check if moving forward based on direction +act_dir_forward: + .db TF_SE ; SOUTH + .db TF_NE ; NORTH + .db TF_WE ; WEST + .db TF_EE ; EAST + + ; tile flags to check if moving back based on direction +act_dir_back: + .db TF_NE ; SOUTH + .db TF_SE ; NORTH + .db TF_EE ; WEST + .db TF_WE ; EAST - + ; updates and draws an actor ; calls different routines fro combat and map state @@ -22,6 +44,71 @@ act_update: ; TODO: read type, get ptr from table ; and call ret + + ; checks if the selected actor can move forward + ; inputs: + ; de: actor + ; hl: act_dir_forward or act_dir_back + ; returns: + ; zero flag: -> move not possible + ; not zero flag: -> move possible +act_can_move: + push hl + + ld hl, act_pos_y + add hl, de + + ld b, [hl] + inc hl + ld c, [hl] + + push de + call map_get_tile + ; hl = tile + ld de, t_flags + add hl, de + ld a, [hl] + ld b, a ; b = tile flags + + pop de + + ld hl, act_dir + add hl, de + ld a, [hl] + and a, ACT_DIR_MASK + ; a = direction + + ld d, 0 + ld e, a ; de = direction offset + pop hl ; hl = flags table + add hl, de + ld a, [hl] ; a = required flag + + and a, b + + ret + + ; moves the actor forward + ; does not perform collision checks + ; inputs: + ; de: actor +act_move_forward: + ld hl, act_dir + ld a, [hl] + and a, ACT_DIR_MASK + ld b, 0 + ld c, a ; bc = direction offset + + ld hl, act_dir_vector + add hl, bc ; hl = direction vector + ret + + ; moves the actor back + ; does not perform collision checks + ; inputs: + ; de: actor +act_move_back: + ret act_bat: diff --git a/src/debug.s b/src/debug.s index e69de29..d0e6309 100644 --- a/src/debug.s +++ b/src/debug.s @@ -0,0 +1,49 @@ +#define DEBUG_FONT_START 0xD0 + +debug_draw_player_pos: + ld a, 2 + call oamalloc + + ld de, player+act_pos_y + + ; draw y + + ; y pos + ld a, 128 + ld [hl+], a + + ; x pos + ld a, 32 + ld [hl+], a + + ; tile + ld a, [de] + add a, DEBUG_FONT_START + ld [hl+], a + + ; flags + xor a, a + ld [hl+], a + + ; draw x + + ; y pos + ld a, 128 + ld [hl+], a + + ; x pos + ld a, 40 + ld [hl+], a + + ; tile + inc de + ld a, [de] + add a, DEBUG_FONT_START + ld [hl+], a + + ; flags + xor a, a + ld [hl+], a + + + ret diff --git a/src/levels.s b/src/levels.s index efaf4cd..49a23c8 100644 --- a/src/levels.s +++ b/src/levels.s @@ -10,7 +10,7 @@ l1: mapdef MAP_F_DO_FULL_REDRAW, map_r_nop, 0, tile_banks_default, tile_id_table - .db 0, 0, 0, 0, 0, 0 + .db 2, 0, 0, 0, 0, 0 .db 0, 0, 0, 0, 0, 0 .db 0, 0, 0, 0, 0, 0 .db 0, 0, 0, 0, 0, 0 diff --git a/src/map.s b/src/map.s index a30f518..2831848 100644 --- a/src/map.s +++ b/src/map.s @@ -282,9 +282,8 @@ map_get_tile: ld hl, tile_null ret - - ; draws a full page of the currently selected map - ; into the map render buffer + ; draws a full map copy into the current map view buffer + ; bsed on the current location the player is facing ; the map render buffer is then written to the screen ; inputs: ; [map] diff --git a/src/player.s b/src/player.s index 1fcb7ce..fb4ac6e 100644 --- a/src/player.s +++ b/src/player.s @@ -20,7 +20,6 @@ player_direction_turn_left: player_init: xor a, a ld [player+act_pos_y], a - ld a, 0x47 ld [player+act_pos_x], a ret @@ -71,5 +70,23 @@ player_handle_move: or a, b ld [player+act_dir], a @not_right: + + ld b, DIRUP + input_just + jr z, @not_up REL + ld hl, act_dir_forward + ld de, player + call act_can_move + call nz, act_move_forward +@not_up: + + ld b, DIRDOWN + input_just + jr z, @not_down REL + ld hl, act_dir_back + ld de, player + call act_can_move + call nz, act_move_back +@not_down: ret diff --git a/src/tiles.s b/src/tiles.s index f7acae0..79d390f 100644 --- a/src/tiles.s +++ b/src/tiles.s @@ -4,8 +4,22 @@ ; maps from tile ids ; to tile presets (tile index) tile_id_table: - ; 0 all exit flags set + ; 0 no exits + dw tile_no_exits + ; 1 all exit flags set dw tile_all_exit + ; 2 south exit + dw tile_south_exit + ; 3 + dw tile_north_exit + ; 4 + dw tile_west_exit + ; 5 + dw tile_east_exit + ; 6 + dw tile_north_south_exit + ; 7 + dw tile_east_west_exit ; fallback tile @@ -16,3 +30,20 @@ tile_null: tile_all_exit: tiledef TT_WALL, TF_SE | TF_NE | TF_EE | TF_WE, 0 +tile_no_exits: + tiledef TT_WALL, 0, 0 +tile_south_exit: + tiledef TT_WALL, TF_SE, 0 +tile_north_exit: + tiledef TT_WALL, TF_NE, 0 +tile_west_exit: + tiledef TT_WALL, TF_WE, 0 +tile_east_exit: + tiledef TT_WALL, TF_EE, 0 + +tile_north_south_exit: + tiledef TT_WALL, TF_NE | TF_SE, 0 +tile_east_west_exit: + tiledef TT_WALL, TF_EE | TF_WE, 0 + + diff --git a/src/ui.s b/src/ui.s index 28dc93e..2656018 100644 --- a/src/ui.s +++ b/src/ui.s @@ -71,5 +71,5 @@ compass_draw: xor a, a ld [hl], a - + call debug_draw_player_pos ret -- 2.30.2