From: Lukas Krickl Date: Wed, 26 Mar 2025 17:21:21 +0000 (+0100) Subject: cursor: preventing camera from scrolling out of bounds in top/left X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=87bb6f3e253054ceb00a6256619b73f8295b5b74;p=gbrg%2F.git cursor: preventing camera from scrolling out of bounds in top/left direction --- diff --git a/src/player.s b/src/player.s index bd45c11..9e89a25 100644 --- a/src/player.s +++ b/src/player.s @@ -22,7 +22,7 @@ player_update: ; check cursor movement ld a, [cursor_move_timer] cp a, 0 - call nz, cursor_move + call nz, cursor_move_delay call z, handle_inputs @@ -61,37 +61,40 @@ handle_inputs: cursor_move_direction cursor_move_y, cursor_move_x, 1 - ld a, [cursor_y] - cp a, 0 ; adjust scroll cursor_adjust_scroll add, scroll_move_y + call cursor_move ret @notdown: input_held BTNUP jr z, @notup REL + call try_abort_move_up + ret z + cursor_move_direction cursor_move_y, cursor_move_x, NEGATE - ld a, [cursor_y] - cp a, 0 ; adjust scroll cursor_adjust_scroll sub, scroll_move_y + call cursor_move ret @notup: input_held BTNLEFT jr z, @notleft REL + call try_abort_move_left + ret z + cursor_move_direction cursor_move_x, cursor_move_y, NEGATE - ld a, [cursor_x] - cp a, 0 ; adjust scroll cursor_adjust_scroll sub, scroll_move_x + call cursor_move ret @notleft: @@ -101,81 +104,58 @@ handle_inputs: cursor_move_direction cursor_move_x, cursor_move_y, 1 - ld a, [cursor_x] - cp a, 0 ; adjust scroll cursor_adjust_scroll add, scroll_move_x - + + call cursor_move @notright: ret -cursor_move: - push af - - + ; updates cursor move delay + ; cursor move delay timer-- +cursor_move_delay: ld a, [cursor_move_timer] dec a ld [cursor_move_timer], a - cp a, 0 - jr nz, @done REL + ret - ; if timer is 0 apply movement - ; and jump to next tile - call try_abort_move - jp z, @done +cursor_move: + ld a, [cursor_y] + ld b, a - ld a, [cursor_y] - ld b, a + ld a, [cursor_move_y] + add a, b + ld [cursor_y], a - ld a, [cursor_move_y] - add a, b - ld [cursor_y], a + ld a, [cursor_x] + ld b, a - ld a, [cursor_x] - ld b, a + ld a, [cursor_move_x] + add a, b + ld [cursor_x], a - ld a, [cursor_move_x] - add a, b - ld [cursor_x], a - - call scroll_update -@done: - pop af + call scroll_update ret -try_abort_move: +try_abort_move_left: ld b, CURSOR_MIN_X - ld c, CURSOR_MOVE_SPEED * NEGATE ld hl, cursor_x - ld de, cursor_move_x - call try_abort_move_at - ret z + jp try_abort_move_at - ; TODO check other directions here - ret +try_abort_move_up: + ld b, CURSOR_MIN_Y + ld hl, cursor_y + jp try_abort_move_at ; aborts a move direciton ; inputs: ; b: compare variable (e.g. min or max value) - ; c: move direction compare value (e.g. move + or -) ; hl: position ptr - ; de: move variable to set to 0 ; returns: z == 0 if move was aborted try_abort_move_at: ; do not abort if position is wrong ld a, [hl] cp a, b - ret nz - - ; do not abort if direction is wrong - ld a, [de] - cp a, c - ret nz - - xor a, a - ld [de], a - - ; z = 0 due to xor a, a ret diff --git a/src/video.s b/src/video.s index a7c1e62..9a2ca42 100644 --- a/src/video.s +++ b/src/video.s @@ -28,12 +28,7 @@ vblank: ret ; updates scroll based on scroll_move_y/x - ; do nothing if scroll would move oob scroll_update: - ld a, [cursor_move_timer] - cp a, 0 - ret nz - ld a, [scroll_move_y] cp a, 0x0F jr nz, @no_scroll_down REL @@ -63,7 +58,6 @@ scroll_update: jr @done REL @no_scroll_up: - ld a, [scroll_move_x] cp a, 0x14 jr nz, @no_scroll_right REL