player: Added basic movement
authorLukas Krickl <lukas@krickl.dev>
Sat, 20 Sep 2025 07:10:16 +0000 (09:10 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sat, 20 Sep 2025 07:10:16 +0000 (09:10 +0200)
src/player.s

index 4a3147e1d2238b0e2b701c6b7e6bce7d4b8ca1ce..31e966c394e729a88d2bbe48aac48ad6e8ff51ea 100644 (file)
@@ -1,3 +1,5 @@
+#define PLAYER_SPEED 1
+
        ; sets up the player actor
 player_init:
        ; initial position
@@ -12,9 +14,108 @@ player_init:
        
        ; updates the special player actor
 player_update:
+       ld b, BTNUP
+       input_held
+       jr z, @not_up REL
+               ld b, PLAYER_SPEED
+               ld c, 0
+               call player_stage_move_n
+               call player_try_move
+@not_up:
+
+       ld b, BTNDOWN
+       input_held
+       jr z, @not_down REL
+               ld b, PLAYER_SPEED
+               ld c, 0
+               call player_stage_move_p
+               call player_try_move
+
+@not_down:
+
+       ld b, BTNLEFT
+       input_held
+       jr z, @not_left REL
+               ld b, 0 
+               ld c, PLAYER_SPEED 
+               call player_stage_move_n
+               call player_try_move
+@not_left:
+
+       ld b, BTNRIGHT
+       input_held
+       jr z, @not_right REL
+
+               ld b, 0
+               ld c, PLAYER_SPEED
+               call player_stage_move_p
+               call player_try_move
+@not_right:
+
        ret
 
 #define PLAYER_SPRITE_IDLE1 0x8D
+       
+       ; stages a move in a positive directon
+       ; inputs:
+       ;               b/c: y/x movement
+       ;       returns:
+       ;               b/c: new y/x position
+       ;                 d: new y hi position
+player_stage_move_p:
+       ld a, [player+act_pos_y]
+       add a, b
+       ld b, a
+
+       ld a, [player+act_pos_y_hi]
+       adc a, 0
+       ld [player+act_pos_y_hi], a
+       ld d, a
+
+       ld a, [player+act_pos_x]
+       add a, c
+       ld c, a
+       
+       ret
+
+       ; stages a move in a negative directon
+       ; inputs:
+       ;               b/c: y/x movement
+       ;       returns:
+       ;               b/c: new y/x position
+       ;                 d: new y hi position
+player_stage_move_n:
+       ld a, [player+act_pos_y]
+       sub a, b
+       ld b, a
+
+       ld a, [player+act_pos_y_hi]
+       sbc a, 0
+       ld [player+act_pos_y_hi], a
+       ld d, a
+
+       ld a, [player+act_pos_x]
+       sub a, c
+       ld c, a
+       
+       ret
+       
+       ; moves the player
+       ; performs a collision check
+       ; moves scroll and performs map loads if needed
+       ; inputs:
+       ;               b/c: new y/x position
+       ;               d: new y hi position
+player_try_move:
+       ld a, b
+       ld [player+act_pos_y], a
+
+       ld a, c
+       ld [player+act_pos_x], a
+
+       ld a, d
+       ld [player+act_pos_y_hi], a
+       ret
 
        ; draws the special player actor
 player_draw: