player: moved collision check to subroutine
authorLukas Krickl <lukas@krickl.dev>
Mon, 27 Jan 2025 13:32:24 +0000 (14:32 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 27 Jan 2025 13:32:24 +0000 (14:32 +0100)
src/player.s

index 531213e70047e46d33a4be2fa580e16d7944ed37..b0f7ae85816506e224a818f271cbabe766453cbc 100644 (file)
@@ -210,48 +210,14 @@ player_update:
   ; sotre x in e
   ld a, [hl]
   ld e, a
-
-  ; if any collision happens now 
-  ; we stop the animation
-  push hl ; need to save hl
+  
   ld a, [anim_target_y] 
   add a, 8
   ld d, a ; d = target y + 8 to center on tile 
   ld a, [anim_target_x]
   add a, 8
   ld e, a ; e = target x + 8 to center on tile 
-  call collision_tile 
-  ld h, a ; store original a in h for now 
-
-  and a, RF_DOOR
-  jr z, @no_door_hit REL
-
-  ; set room load flag is door was hit
-  ld a, [engine_flags]
-  or a, EG_FLOAD_ROOM 
-  ld [engine_flags], a
-  
-
-@no_door_hit:
-
-  ld a, h ; restore original a for next check
-  and a, RF_ACTOR
-  jr z, @no_enemy_hit REL
-
-  ; TODO add enemy hit logic 
-
-@no_enemy_hit:
-
-  ld a, h ; restore original a for next check  
-  and a, RF_WALL | RF_ACTOR
-  pop hl
-  
-  jr z, @no_collision REL
-    ; clear anim memory
-    call anim_clear
-    jr @skip_input REL
-@no_collision:
-  ; call z, player_tile_update_rf_flags 
+  call player_collision_check
 
 @skip_input:
   ; hl should be player_y here 
@@ -350,6 +316,49 @@ player_update:
 
   ret
 
+  ; performs player collision checks
+  ; inputs:
+  ;   hl: player ptr
+  ;   de: y/x position used for check
+player_collision_check:
+  ; if any collision happens now 
+  ; we stop the animation
+  push hl ; need to save hl 
+  call collision_tile 
+  ld h, a ; store original a in h for now 
+
+  and a, RF_DOOR
+  jr z, @no_door_hit REL
+
+  ; set room load flag is door was hit
+  ld a, [engine_flags]
+  or a, EG_FLOAD_ROOM 
+  ld [engine_flags], a
+  
+
+@no_door_hit:
+
+  ld a, h ; restore original a for next check
+  and a, RF_ACTOR
+  jr z, @no_enemy_hit REL
+
+  ; TODO add enemy hit logic 
+
+@no_enemy_hit:
+
+  ld a, h ; restore original a for next check  
+  and a, RF_WALL | RF_ACTOR
+  pop hl
+  
+  jr z, @no_collision REL
+    ; clear anim memory
+    call anim_clear
+    jr @skip REL
+@no_collision:
+  ; call z, player_tile_update_rf_flags 
+@skip:
+  ret
+
   ; uses a resource such as 
   ; hp, mp, atk or def down to 0
   ; inputs: