player: Added underflow and overflow protection to movement
authorLukas Krickl <lukas@krickl.dev>
Wed, 24 Sep 2025 04:33:33 +0000 (06:33 +0200)
committerLukas Krickl <lukas@krickl.dev>
Wed, 24 Sep 2025 04:33:33 +0000 (06:33 +0200)
src/player.s

index f7064a2af20a7ef4e0ea336b30e954f1f5de6a8b..2a840240cfe31aaa43628d2501c4e1053b065b06 100644 (file)
@@ -112,7 +112,12 @@ player_stage_move_p:
        add a, b
        ld [player_sub_pixel_y], a
        ld a, [player+act_pos_y]
-       adc a, 0 
+       adc a, 0
+
+       jr nc, @no_overflow_y REL
+               ld a, 0xFF
+@no_overflow_y:
+
        ld b, a
 
        ld a, [player_sub_pixel_x]
@@ -120,6 +125,10 @@ player_stage_move_p:
        ld [player_sub_pixel_x], a
        ld a, [player+act_pos_x]
        adc a, 0 
+
+jr nc, @no_overflow_x REL
+               ld a, 0xFF
+@no_overflow_x:
        ld c, a
        
        ret
@@ -135,6 +144,11 @@ player_stage_move_n:
        ld [player_sub_pixel_y], a
        ld a, [player+act_pos_y]
        sbc a, 0 
+
+       jr nc, @no_underflow_y REL
+               xor a, a
+@no_underflow_y:
+
        ld b, a
 
        ld a, [player_sub_pixel_x]
@@ -142,6 +156,11 @@ player_stage_move_n:
        ld [player_sub_pixel_x], a
        ld a, [player+act_pos_x]
        sbc a, 0 
+       
+       jr nc, @no_underflow_x REL
+               xor a, a
+@no_underflow_x:
+
        ld c, a
        
        ret