From f7d65b5cfe9f5b4e68b19104652385dfd04eb0cf Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 15 Dec 2025 08:31:08 +0100 Subject: [PATCH] player: Added turning mechanics --- src/defs.s | 7 ++++--- src/jmp.inc | 1 + src/macros.inc | 2 +- src/player.s | 54 +++++++++++++++++++++++++++++++++++++++++++++----- src/ui.s | 42 +++++++++++++++++++++++++++++++++++++++ src/video.s | 2 +- 6 files changed, 98 insertions(+), 10 deletions(-) diff --git a/src/defs.s b/src/defs.s index 06ceff7..a9e4585 100644 --- a/src/defs.s +++ b/src/defs.s @@ -36,6 +36,8 @@ .de ACT_T_NULL, 1 .de ACT_T_PLAYER, 1 .de ACT_T_BAT, 1 + +.def int ACT_DIR_MASK = 0b00000011 ; actor struct ; stats are defined by looking up the type in a table @@ -105,9 +107,8 @@ .de t_type, 1 .de t_flags, 1 ; actor currently - ; standing on tile - ; 0 = player, 1-N = map actors -.de t_actor, 1 + ; standing on tile (ptr) +.de t_actor, 2 .de t_p0, 1 .de t_size, 0 diff --git a/src/jmp.inc b/src/jmp.inc index e5eafac..7d6cc71 100644 --- a/src/jmp.inc +++ b/src/jmp.inc @@ -41,6 +41,7 @@ vec_vblank: ; STA 0x48 ;============= vec_stat: + reti ; disable objects push af ld a, [RLCD] diff --git a/src/macros.inc b/src/macros.inc index 04bfd61..0d2c263 100644 --- a/src/macros.inc +++ b/src/macros.inc @@ -173,7 +173,7 @@ $1: #macro tiledef .db $1, $2 ; t_actor - .db 0 + .db 0, 0 .db $3 #endmacro diff --git a/src/player.s b/src/player.s index 1aac9b6..1fcb7ce 100644 --- a/src/player.s +++ b/src/player.s @@ -1,5 +1,20 @@ #define PLAYER_CURSOR_L 0x88 #define PLAYER_CURSOR_R 0x8A + + ; table of direction to pick on right turn +player_direction_turn_right: + .db EAST ; SOUTH + .db WEST ; NORTH + .db SOUTH ; EAST + .db NORTH ; WEST + + ; table of directions to pick on left turn +player_direction_turn_left: + .db WEST ; SOUTH + .db EAST ; NORTH + .db NORTH ; EAST + .db SOUTH ; WEST + ; sets up the player actor player_init: @@ -19,13 +34,42 @@ player_update: ret - ; draws the players current facing direction - ; as a compass -compass_draw: - ret - ; moves the player ; does not move out of bounds ; based on the last queued input player_handle_move: + ld b, DIRLEFT + input_just + jr z, @not_left REL + ld hl, player_direction_turn_left + ld a, [player+act_dir] + and a, ACT_DIR_MASK + ld d, 0 + ld e, a + add hl, de ; hl = direction table offset + + ld b, [hl] ; new direction + ld a, [player+act_dir] + and a, ~ACT_DIR_MASK & 0xFF + or a, b + ld [player+act_dir], a +@not_left: + + ld b, DIRRIGHT + input_just + jr z, @not_right REL + ld hl, player_direction_turn_right + ld a, [player+act_dir] + and a, ACT_DIR_MASK + ld d, 0 + ld e, a + add hl, de ; hl = direction table offset + + ld b, [hl] ; new direction + ld a, [player+act_dir] + and a, ~ACT_DIR_MASK & 0xFF + or a, b + ld [player+act_dir], a +@not_right: + ret diff --git a/src/ui.s b/src/ui.s index 4507858..28dc93e 100644 --- a/src/ui.s +++ b/src/ui.s @@ -1,5 +1,10 @@ #define UI_TILE_NAME SCRN1+33 +#define TILE_SOUTH 0xEC +#define TILE_NORTH 0xE7 +#define TILE_EAST 0xDE +#define TILE_WEST 0xF0 + ; one tile after 'Z' #define UI_WINDOW_BACKGROUND 0xF4 @@ -31,3 +36,40 @@ ui_update: ui_draw: ret +compass_tiles: + .db TILE_SOUTH, TILE_NORTH, TILE_EAST, TILE_WEST + + ; draws the players current facing direction + ; as a compass +compass_draw: + ld a, 1 + call oamalloc + + + ; y + ld a, 136 + ld [hl+], a + + ; x + ld a, 64 + ld [hl+], a + + ; tile + push hl + ld a, [player+act_dir] + and a, ACT_DIR_MASK + + ld d, 0 + ld e, a + ld hl, compass_tiles + add hl, de + ld a, [hl] ; a = tile + pop hl + ld [hl+], a + + ; flags + xor a, a + ld [hl], a + + + ret diff --git a/src/video.s b/src/video.s index 3de459d..c6e8612 100644 --- a/src/video.s +++ b/src/video.s @@ -115,7 +115,7 @@ lcd_off: ; turns on the lcd lcd_on: - ld a, LCDCF_ON | LCDCF_BGON | LCDCF_OBJON | LCDF_WINDOWON | LCDF_WINBANKSELECT | LCDF_OBJ_SIZE + ld a, LCDCF_ON | LCDCF_BGON | LCDCF_OBJON | LCDF_WINDOWON | LCDF_WINBANKSELECT ld [RLCD], a ret -- 2.30.2