Moved rec collision check to a macro to avoid copy-paste issues
authorLukas Krickl <lukas@krickl.dev>
Fri, 1 Nov 2024 17:44:41 +0000 (18:44 +0100)
committerLukas Krickl <lukas@krickl.dev>
Fri, 1 Nov 2024 17:44:41 +0000 (18:44 +0100)
src/collision.s

index 6c4ff8c3c9b010b0a0444d0c0e6c9baf79b0907f..c02df97238f0c207b39fe9596d5501b176462e05 100644 (file)
@@ -173,6 +173,25 @@ collision_tile:
 
   ret
 
+  ; this macro executes the copy-pasted code for each point of the rectangle 
+  ; inputs:
+  ;   $1: c/nc -> indicate greater or less than for y 
+  ;   $2: c/nc -> indicate greater or less than for x 
+  ;   $3: jump target if no collision occured (relative label) 
+#macro collision_pt_rec_check
+  ; -> y pos 
+  ld a, [hl+]
+  add a, b ; a = rec y + y offset 
+  cp a, d ; cp tl  py?
+  jr $1, $3 REL 
+
+  ; -> x pos
+  ld a, [hl+]
+  add a, c ; a = rec x + x offset 
+  cp a, e ; cp t1  px?
+  jr $2, $3 REL
+#endmacro 
+
   ; check collision between
   ; a point and a provided rectangle 
   ; inputs:
@@ -189,39 +208,13 @@ collision_tile:
   ;    a = 1 -> collision
 collision_pt_rec:
 @top_left:
-  ; check top left
-  ; -> y pos 
-  ld a, [hl+]
-  add a, b ; a = rec y + y offset 
-  cp a, d ; tl > py?
-  jr nc, @no_collision REL
-
-  ; -> x pos
-  ld a, [hl+]
-  add a, c ; a = rec x + x offset 
-  cp a, e ; t1 > px?
-  jr nc, @no_collision REL 
-
+  collision_pt_rec_check nc, nc, @no_collision
 @top_right:
-  ; check top right 
-  ; y pos
-  ld a, [hl+]
-  add a, b ; a = rec y + y offset 
-  cp a, d  ; tr > py?
-  jr nc, @no_collision REL
-
-  ; x pos
-  ld a, [hl+]
-  add a, c ; a = rec x + x offset 
-  cp a, e ; tr < px
-  jr c, @no_collision REL
-  
-  ; TODO check bottom left 
-  @bottom_left:
-
-  ; TODO check bottom right
+  collision_pt_rec_check nc, c, @no_collision
+@bottom_left:
+  collision_pt_rec_check c, nc, @no_collision
 @bottom_right:
-
+  collision_pt_rec_check c, c, @no_collision
 @collision:
   ld a, 1
 @no_collision: