From 1feaf3c684dcc84c2ba2eb45159a5c63442c76fe Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Fri, 1 Nov 2024 18:44:41 +0100 Subject: [PATCH] Moved rec collision check to a macro to avoid copy-paste issues --- src/collision.s | 55 +++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/src/collision.s b/src/collision.s index 6c4ff8c..c02df97 100644 --- a/src/collision.s +++ b/src/collision.s @@ -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: -- 2.30.2