cursor: preventing camera from scrolling out of bounds in top/left
authorLukas Krickl <lukas@krickl.dev>
Wed, 26 Mar 2025 17:21:21 +0000 (18:21 +0100)
committerLukas Krickl <lukas@krickl.dev>
Wed, 26 Mar 2025 17:21:21 +0000 (18:21 +0100)
direction

src/player.s
src/video.s

index bd45c1177f4815df47891337166c6de62aa09860..9e89a25deff86e63fb5d1c8c869d1470dde1a7df 100644 (file)
@@ -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
index a7c1e624d4efd9861261c2d6f425209515074102..9a2ca42a42f6117cf18afea6120d98c24e65ae04 100644 (file)
@@ -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