From e2564ecedc4dbe9bad5536c22ce3709fef50499b Mon Sep 17 00:00:00 2001 From: Forest Belton <65484+forestbelton@users.noreply.github.com> Date: Sat, 17 Jul 2021 01:00:16 -0400 Subject: [PATCH] Update collision for new map engine --- src/collision.s | 109 +++++++++++++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 39 deletions(-) diff --git a/src/collision.s b/src/collision.s index ab9a511..eeea510 100644 --- a/src/collision.s +++ b/src/collision.s @@ -3,34 +3,54 @@ INCLUDE "oam.inc" SECTION "Collision", ROM0 ; Determines if player has hit the background -; @return NZ = false, Z = true +; @return NZ = true, Z = false player_bg_collides:: - ; c = x % 8 == 0 ? x/8 : x/8 + 1 + ; C = MAP X = PLAYER X / 8 + CAMERA X ld a, [PLAYER_X] + call get_tile_coord ld c, a - and %111 - jr z, .skip_inc_c - ld a, c - add 8 + + ld a, [CURRENT_CAMERA_X] + add c ld c, a -.skip_inc_c: - srl c - srl c - srl c - ; b = y % 8 == 0 ? y/8 : y/8 + 1 + ; C < 0 + bit 7, c + ret nz + + ; C >= MAP WIDTH + ld a, [CURRENT_MAP_WIDTH] + cp c + jr nz, .load_y + + ; Set NZ + or $ff + ret + +.load_y: + ; B = MAP Y = PLAYER Y / 8 + CAMERA Y ld a, [PLAYER_Y] + call get_tile_coord ld b, a - and %111 - jr z, .skip_inc_b - ld a, b - add 8 + + ld a, [CURRENT_CAMERA_Y] + add b ld b, a -.skip_inc_b: - srl b - srl b - srl b + ; B < 0 + bit 7, b + ret nz + + ; B >= MAP HEIGHT + ld a, [CURRENT_MAP_HEIGHT] + cp b + jr nz, .check_tiles + + ; Set NZ + or $ff + ret + +.check_tiles: ; top left call can_move_to ret nz @@ -57,29 +77,20 @@ player_bg_collides:: player_in_air:: ; c = x % 8 == 0 ? x/8 : x/8 + 1 ld a, [PLAYER_X] + call get_tile_coord ld c, a - and %111 - jr z, .skip_inc_c - ld a, c - add 8 + + ld a, [CURRENT_CAMERA_X] + add c ld c, a -.skip_inc_c: - srl c - srl c - srl c - ; b = y % 8 == 0 ? y/8 : y/8 + 1 ld a, [PLAYER_Y] + call get_tile_coord ld b, a - and %111 - jr z, .skip_inc_b - ld a, b - add 8 + + ld a, [CURRENT_CAMERA_Y] + add b ld b, a -.skip_inc_b: - srl b - srl b - srl b ; check underneath player (2 tiles down from top-left sprite) inc b @@ -99,14 +110,13 @@ player_in_air:: ; @destroy a, d, e ; @note NZ = false, Z = true can_move_to: - ; todo: should be aware of scx/scy push bc push hl ld a, [CURRENT_MAP_COLLISION] - ld h, a - ld a, [CURRENT_MAP_COLLISION + 1] ld l, a + ld a, [CURRENT_MAP_COLLISION + 1] + ld h, a ld a, [CURRENT_MAP_WIDTH] ld d, 0 @@ -127,3 +137,24 @@ can_move_to: pop bc ret + +; Convert from pixel space to tile space +; @param a Screen pixel coordinate +; @destroys hl +; @returns Screen tile coordinate in A +get_tile_coord: + ld hl, sp - 1 + ld [hl], a + + and %111 + ld a, [hl] + + jr z, .skip_inc + add 8 + +.skip_inc: + srl a + srl a + srl a + + ret