From: Lukas Krickl Date: Thu, 27 Mar 2025 19:56:04 +0000 (+0100) Subject: state: Added proper state machine based cursor movement X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=f56dd8c60a655e683a1f9cc50032c84dc82291c5;p=gbrg%2F.git state: Added proper state machine based cursor movement --- diff --git a/src/macros.inc b/src/macros.inc index 402312c..fc5b0a0 100644 --- a/src/macros.inc +++ b/src/macros.inc @@ -101,8 +101,6 @@ #macro cursor_move_direction xor a, a ld [$2], a - ld a, CURSOR_MOVE_TIMER - ld [cursor_move_timer], a ld a, CURSOR_MOVE_SPEED * $3 ld [$1], a #endmacro diff --git a/src/player.s b/src/player.s index fd6c91d..251c25b 100644 --- a/src/player.s +++ b/src/player.s @@ -21,13 +21,15 @@ player_init: ; inputs: ; hl: pointer to player memory player_update: - ; check cursor movement - ld a, [cursor_move_timer] - cp a, 0 - call nz, cursor_move_delay - call z, handle_inputs - + call handle_inputs + push bc + call player_draw_cursor + pop bc + ret + ; updates the cursor + ; sprites +player_draw_cursor: @draw_cursor: ; draw cursor ld a, [scroll_y] @@ -63,7 +65,7 @@ handle_inputs: jr z, @notdown REL call try_abort_move_down - ret z + jp z, @done cursor_move_direction cursor_move_y, cursor_move_x, 1 @@ -71,6 +73,7 @@ handle_inputs: cursor_adjust_scroll add, scroll_move_y call cursor_move + ld bc, st_cursor_delay ret @notdown: @@ -78,7 +81,7 @@ handle_inputs: jr z, @notup REL call try_abort_move_up - ret z + jr z, @done REL cursor_move_direction cursor_move_y, cursor_move_x, NEGATE @@ -86,6 +89,7 @@ handle_inputs: cursor_adjust_scroll sub, scroll_move_y call cursor_move + ld bc, st_cursor_delay ret @notup: @@ -93,7 +97,7 @@ handle_inputs: jr z, @notleft REL call try_abort_move_left - ret z + jr z, @done REL cursor_move_direction cursor_move_x, cursor_move_y, NEGATE @@ -101,7 +105,7 @@ handle_inputs: cursor_adjust_scroll sub, scroll_move_x call cursor_move - + ld bc, st_cursor_delay ret @notleft: @@ -109,7 +113,7 @@ handle_inputs: jr z, @notright REL call try_abort_move_right - ret z + jr z, @done REL cursor_move_direction cursor_move_x, cursor_move_y, 1 @@ -117,16 +121,11 @@ handle_inputs: cursor_adjust_scroll add, scroll_move_x call cursor_move + ld bc, st_cursor_delay + ret @notright: - - ret - - ; updates cursor move delay - ; cursor move delay timer-- -cursor_move_delay: - ld a, [cursor_move_timer] - dec a - ld [cursor_move_timer], a +@done: + ldnull bc ret cursor_move: diff --git a/src/state.s b/src/state.s index 03e233c..8f103dc 100644 --- a/src/state.s +++ b/src/state.s @@ -40,12 +40,15 @@ st_update: cp a, 0 jr z, @set_next_state_default REL + ; hl = actor ptr + ; set returned state push bc pop de ; de = src ld bc, st_size - jp memcpy + call memcpy + ret @set_next_state_default: ; set default state @@ -78,6 +81,7 @@ st_null: st_cursor: st_def 0x00, player_update, st_cursor - +st_cursor_draw: + st_def 0x00, player_draw_cursor, st_cursor st_cursor_delay: - st_def CURSOR_MOVE_TIMER, st_null, st_cursor + st_def CURSOR_MOVE_TIMER, st_null_fn, st_cursor diff --git a/src/wram.s b/src/wram.s index 4a6a3b2..e93cfc5 100644 --- a/src/wram.s +++ b/src/wram.s @@ -33,7 +33,6 @@ state: cursor: cursor_y: .adv 1 cursor_x: .adv 1 -cursor_move_timer: .adv 1 cursor_move_y: .adv 1 cursor_move_x: .adv 1