From d8864925002e8b80c781867160f36d0466449baa Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 18 Nov 2024 14:41:05 +0100 Subject: [PATCH] Added collision check for new movement --- src/collision.s | 26 ++-------------------- src/player.s | 57 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/collision.s b/src/collision.s index 56f1869..5d84f71 100644 --- a/src/collision.s +++ b/src/collision.s @@ -31,31 +31,9 @@ .db $1, $2 #endmacro -collision_player_top: - col_head 2 - col_point 2, 0 - col_point 2, 8 - -collision_player_bot: - col_head 2 - col_point 12, 0 - col_point 12, 8 - -collision_player_right: - col_head 2 - col_point 2, 8 - col_point 12, 8 - -collision_player_left: - col_head 2 - col_point 2, 0 - col_point 12, 0 - -tile_collision_rec8x8: +collision_player: + col_head 1 col_point 0, 0 - col_point 0, 8 - col_point 8, 0 - col_point 8, 8 collision_tile_lut: .rep cti, ROOM_H, 1, .db cti * ROOM_W diff --git a/src/player.s b/src/player.s index 8dedd63..78bba70 100644 --- a/src/player.s +++ b/src/player.s @@ -54,7 +54,7 @@ player_init: ; 1) save registers push hl push de - + ; 2) hl = player_y already ; 3) load correct collision points ld de, $1 @@ -71,7 +71,7 @@ player_init: ; actor table ; inputs: ; hl: pointer to player memory -player_update: +player_update: ; update ld a, [who] cp a, WHO_PLAYER @@ -88,6 +88,15 @@ player_update: ld a, RF_WALL ld [ct_mask], a + ; tmp = y movement offset + ; tmp+1 = x movement offset + ; load tmp and +1 with the current position by + ; default + ld a, [hl+] + ld [tmp], a + ld a, [hl] + dec hl + ld [tmp+1], a ; input handling input_held BTNDOWN @@ -98,6 +107,11 @@ player_update: ld [anim_move_y], a ld a, ANIM_STEP_DOWN ld [anim_step_y], a + + ; set expected offset + ld a, ANIM_MOVE_TILE_SIZE + add a, [hl] + ld [tmp], a @notdown: input_held BTNUP @@ -108,6 +122,11 @@ player_update: ld [anim_move_y], a ld a, ANIM_STEP_UP ld [anim_step_y], a + + ; set expected offset + ld a, 0xFF - ANIM_MOVE_TILE_SIZE + 1 + add a, [hl] + ld [tmp], a @notup: @@ -119,6 +138,13 @@ player_update: ld [anim_move_x], a ld a, ANIM_STEP_LEFT ld [anim_step_x], a + + ; set expected offset + ld a, 0xFF - ANIM_MOVE_TILE_SIZE + 1 + inc hl + add a, [hl] + dec hl + ld [tmp+1], a @notleft: input_held BTNRIGHT @@ -129,6 +155,13 @@ player_update: ld [anim_move_x], a ld a, ANIM_STEP_RIGHT ld [anim_step_x], a + + ; set expected offset + ld a, ANIM_MOVE_TILE_SIZE + inc hl + add a, [hl] + dec hl + ld [tmp+1], a @notright: @action_buttons: @@ -172,6 +205,26 @@ player_update: ld a, [hl] ld e, a + ; if any collision happens now + ; we stop the animation + push hl ; need to save hl + ld a, [tmp] + add a, 8 + ld d, a ; d = target y + 8 to center on tile + ld a, [tmp+1] + add a, 8 + ld e, a ; e = target x + 8 to center on tile + call collision_tile + pop hl + jr z, @no_collision REL + ; clear anim memory + xor a, a + ld [anim_move_y], a + ld [anim_move_x], a + ld [anim_step_y], a + ld [anim_step_x], a +@no_collision: + @skip_input: ; hl should be player_y here -- 2.30.2