From 3ba93306049c577dd5325ea841ad4a64466a5669 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Wed, 3 Dec 2025 08:04:31 +0100 Subject: [PATCH] player: wip collision checks --- src/actor.s | 61 ++++++++++++++------------ src/player.s | 122 +++++++++++++++++++++++++-------------------------- src/wram.s | 12 ++++- 3 files changed, 101 insertions(+), 94 deletions(-) diff --git a/src/actor.s b/src/actor.s index 5290782..796c39c 100644 --- a/src/actor.s +++ b/src/actor.s @@ -84,23 +84,43 @@ _act_test_col_x: pop af ret + ; stores current position in tmp_prev_pos + ; inputs: + ; de: actor +act_backup_pos: + ld hl, act_pos_y + add hl, de + ld a, [hl+] + ld [tmp_prev_pos], a ; y + + ld a, [hl] + ld [tmp_prev_pos+1], a ; x + ret + + ; restores prev x pos + ; inputs: + ; de: actor +act_rollback_pos_x: + ret + + ; restores prev y pos + ; inputs: + ; de: actor +act_rollback_pos_y: + ret + ; test collision based on an actor ; and a direction ; inputs: ; de: actor - ; a: direction - ; returns: - ; a: new direction - ; a: 0 if move should be aborted -act_test_collision: - push af + ; col_point_xx: collision points + ; if collision occures rolls back to + ; previous position +act_test_tile_collision: ld hl, act_pos_y add hl, de - call _act_test_col_y - call _act_test_col_x - ; b/c = y/x call map_get_tile @@ -108,43 +128,26 @@ act_test_collision: add hl, de ld a, [hl] and a, TF_WALL - jr nz, @collision REL - pop af - ret -@collision: - pop af - ld a, 0 ret ; update routines for each actor -actor_map_update_table: +act_map_update_table: ; draw routine for each actor -actor_map_draw_table: +act_map_draw_table: - ; combat update table -actor_combat_update_table: - - ; combat draw table -actor_combat_draw_table: ; updates and draws an actor ; calls different routines fro combat and map state ; inputs: ; hl: actor ptr -actors_map_update: +act_update: ; TODO: read type, get ptr from table ; and call ret - - ; calls combat update and draw routines -actors_combat_update: - ; TODO: read type, get ptr from table - ; and call - ret act_bat: actdef ACT_T_BAT, 0, 10, 10, 0, 0 diff --git a/src/player.s b/src/player.s index 8d97d6f..de85d0c 100644 --- a/src/player.s +++ b/src/player.s @@ -10,75 +10,71 @@ player_init: ret + ; writes collision points for player + ; inputs: + ; de: actor +player_col_write_points: + ld hl, act_pos_y + add hl, de + + ld a, [hl+] + ld b, a ; b = y + ld a, [hl] + ld c, a ; c = x + + ; top left point + ld a, b + ld [col_point_tl], a + ld a, c + ld [col_point_tl+1], a + + ; top right point + ld a, b + ld [col_point_tr], a + + ld a, c + add a, 16 + ld [col_point_tr+1], a + + ; bottom left point + ld a, b + add a, 16 + ld [col_point_bl], a + ld a, c + ld [col_point_bl+1], a + + ; bottom right points + ld a, b + add a, 16 + ld [col_point_br], a + ld a, c + add a, 16 + ld [col_point_br+1], a + + ret + ; updates the special player actor player_update: + ld de, player ; call scroll_center_player + call act_backup_pos - call player_inputs - call player_handle_move - - ret - - ; shcedules a player move -player_inputs: - ; no direction inputs if timer is > 0 - ld a, [player_move_timer] - cp a, 0 - ret nz - - ld b, BTNUP - input_held - jr z, @notup REL - ld a, DIRUP - jp @set -@notup: + call player_col_write_points - ld b, BTNDOWN - input_held - jr z, @notdown REL - ld a, DIRDOWN - jp @set -@notdown: + push de + call player_handle_move - ld b, BTNLEFT - input_held - jr z, @notleft REL - ld a, DIRLEFT - jp @set -@notleft: + pop de + call act_test_tile_collision - ld b, BTNRIGHT - input_held - jr z, @notright REL - ld a, DIRRIGHT - jp @set -@notright: ret -@set: - ld de, player - call act_test_collision - cp a, 0 - ret z - - ld [player_direction], a - ld a, 16 ; tile size - ld [player_move_timer], a - ret - - ; moves the player cursor + ; moves the player ; does not move out of bounds ; based on the last queued input player_handle_move: - ; remove 1 from timer - ld a, [player_move_timer] - cp a, 0 - ret z - dec a - ld [player_move_timer], a - - ld a, [player_direction] - and a, BTNDOWN + ld b, BTNDOWN + input_held jr z, @not_down REL ld a, [player+act_pos_y] cp a, 0x60 ; max value @@ -87,8 +83,8 @@ player_handle_move: ld [player+act_pos_y], a @not_down: - ld a, [player_direction] - and a, BTNUP + ld b, BTNUP + input_held jr z, @not_up REL ld a, [player+act_pos_y] cp a, 0 ; min value @@ -97,8 +93,8 @@ player_handle_move: ld [player+act_pos_y], a @not_up: - ld a, [player_direction] - and a, BTNLEFT + ld b, BTNLEFT + input_held jr z, @not_left REL ld a, [player+act_pos_x] cp a, 0 ; min value @@ -107,8 +103,8 @@ player_handle_move: ld [player+act_pos_x], a @not_left: - ld a, [player_direction] - and a, BTNRIGHT + ld b, BTNRIGHT + input_held jr z, @not_right REL ld a, [player+act_pos_x] cp a, 0x90 ; max value diff --git a/src/wram.s b/src/wram.s index 9acbc11..a75c721 100644 --- a/src/wram.s +++ b/src/wram.s @@ -71,7 +71,6 @@ srand: .adv 2 ; stores the last move direction player_direction: .adv 1 -player_move_timer: .adv 1 actors: player: .adv act_size @@ -90,7 +89,16 @@ map_tmp_tile_id_table: .adv 2 ; ptr to current tile to be updated tiles: .adv t_size * MAP_TILES tiles_end: .adv 0 - + + ; collision related data + ; y/x positions +col_point_tl: .adv 2 +col_point_tr: .adv 2 +col_point_bl: .adv 2 +col_point_br: .adv 2 + + ; y/x previous position +tmp_prev_pos: .adv 2 ; space for 4 tile ids -- 2.30.2