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:
; 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: