player: Added basic cursor movement
authorLukas Krickl <lukas@krickl.dev>
Sun, 16 Mar 2025 20:55:10 +0000 (21:55 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 16 Mar 2025 20:55:10 +0000 (21:55 +0100)
src/macros.inc
src/player.s
src/wram.s

index 408fd4e2c14540c126ac83cfb14ce4039bbe9d06..be23d698014ff4bb51e81c1f9aa97b16af5022f6 100644 (file)
@@ -33,6 +33,8 @@
 ; relative jump: jr <label> RELB 
 #define REL - $ - 2 & 0xFF 
 
+#define NEGATE -1 & 0xFF
+
 #macro dblo 
 .db $1 & 0xFF
 #endmacro 
index 694502a2684aa2d83ee3c3cd25d7daf04ef1af4d..8df7fedee192b9ef9812279963421bad9f90117f 100644 (file)
@@ -3,6 +3,8 @@
 
 .def int CURSOR_TILE = 0x04
 
+#define CURSOR_MOVE_TIMER TILE_SIZE
+#define CURSOR_MOVE_SPEED 1
 
   ; init the player 
 player_init:
@@ -17,6 +19,13 @@ player_init:
   ;   hl: pointer to player memory
 player_update:
 
+  ; check cursor movement
+  ld a, [cursor_move_timer] 
+  cp a, 0
+  call nz, cursor_move
+  call z, handle_inputs 
+
+
 @draw_cursor:
   ; draw cursor
 
@@ -40,4 +49,77 @@ player_update:
 
   ret
 
+handle_inputs:
+  input_held BTNDOWN
+  jr z, @notdown REL
+  
+    xor a, a
+    ld [cursor_move_x], a
+    ld a, CURSOR_MOVE_TIMER
+    ld [cursor_move_timer], a
+    ld a, CURSOR_MOVE_SPEED
+    ld [cursor_move_y], a
+    ret
+@notdown:
+  
+  input_held BTNUP
+  jr z, @notup REL
+
+    xor a, a
+    ld [cursor_move_x], a
+    ld a, CURSOR_MOVE_TIMER
+    ld [cursor_move_timer], a
+    ld a, CURSOR_MOVE_SPEED * NEGATE
+    ld [cursor_move_y], a
+    ret
+@notup:
+
+  input_held BTNLEFT
+  jr z, @notleft REL
+
+    xor a, a
+    ld [cursor_move_y], a
+    ld a, CURSOR_MOVE_TIMER
+    ld [cursor_move_timer], a
+    ld a, CURSOR_MOVE_SPEED * NEGATE
+    ld [cursor_move_x], a
+    ret
+@notleft:
+
+  input_held BTNRIGHT 
+  jr z, @notright REL
+
+    xor a, a
+    ld [cursor_move_y], a
+    ld a, CURSOR_MOVE_TIMER
+    ld [cursor_move_timer], a
+    ld a, CURSOR_MOVE_SPEED
+    ld [cursor_move_x], a
+@notright:
+
+  ret
+
+cursor_move:
+  push af
+  ld hl, cursor
+  ; hl = cursor_y
+
+  ld b, [hl]
+  ld a, [cursor_move_y]
+  add a, b
+  ld [cursor_y], a
 
+  ; hl = cursor_x
+  inc hl
+  ld b, [hl]
+  ld a, [cursor_move_x]
+  add a, b
+  ld [cursor_x], a
+  inc hl ; hl = cursor_move_timer
+
+  ld a, [hl]
+  dec a
+  ld [hl], a
+
+  pop af
+  ret
index 4fa2a5e80845495128efb2c32f2e3bf843e89fe2..491afc9fdcece6bf53dafd6bb19a827e9856a609 100644 (file)
@@ -25,8 +25,12 @@ srand: .adv 2
   ; contained here
 state:
 
-cursor_x: .adv 1
+cursor:
 cursor_y: .adv 1
+cursor_x: .adv 1
+cursor_move_timer: .adv 1
+cursor_move_y: .adv 1
+cursor_move_x: .adv 1
 
 money: .adv 3
 debt: .adv 3