From 9889cd80c5f7644b7a771669d25efd528682c839 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 22 Sep 2025 09:53:11 +0200 Subject: [PATCH] collision: wip player movement --- src/actor.s | 34 ++++++++++++++++++++++++++++++++++ src/player.s | 22 ++++++++++++++++++++++ src/rectangle.s | 4 ++++ src/wram.s | 4 ++++ 4 files changed, 64 insertions(+) diff --git a/src/actor.s b/src/actor.s index 3de0ba1..b0a30cd 100644 --- a/src/actor.s +++ b/src/actor.s @@ -125,3 +125,37 @@ actor_despawn: xor a, a ld [de], a ret + + ; writes actor default collider + ; bsed on an input position + ; writes the collider to dst + ; e.g. use with actor's real rectangle + ; if real collision should be written + ; use with tmp_rect for collision tests before moving + ; inputs: + ; a: collision mask + ; b/c: y/x position of actor + ; hl: destination rectangle +actor_write_default_collider: + ld [hl+], a ; mask + ld a, b + ld [hl+], a ; y pos + ld a, c + ld [hl+], a ; x pos + + ld a, 16 ; width/height + ld [hl+], a ; height + ld [hl+], a ; width + ret + + ; tests movement against all collision rectangles + ; inputs: + ; a: collision mask + ; de: current actor (this actor is skipped) + ; tmp_rect: new collision rectangle + ; returns: + ; a == 0: no collision + ; a == 1: collision + ; hl: if collided with an actor points to the actor hit, otherwise NULL +actor_test_movement: + ret diff --git a/src/player.s b/src/player.s index c71d597..c835f17 100644 --- a/src/player.s +++ b/src/player.s @@ -53,6 +53,16 @@ player_update: call player_try_move @not_right: + ; write collider for this frame + ld a, [player+act_pos_y] + ld b, a + ld a, [player+act_pos_x] + ld c, a + ld a, RF_PLAYER + ld hl, player+act_rect + call actor_write_default_collider + + ret #define PLAYER_SPRITE_IDLE1 0x8D @@ -124,6 +134,18 @@ player_stage_move_n: ; inputs: ; b/c: new y/x position player_try_move: + ; write temporary collider + push bc + ld a, RF_PLAYER + ld hl, tmp_rect + call actor_write_default_collider + + ; test collision agains walls and enemies + ld a, RF_WALL | RF_ENEMY + ld de, player + call actor_test_movement + pop bc + ld a, b ld [player+act_pos_y], a diff --git a/src/rectangle.s b/src/rectangle.s index 63c4580..be67463 100644 --- a/src/rectangle.s +++ b/src/rectangle.s @@ -161,6 +161,10 @@ rect_point_test: and a, d ; exit if mask is no match jp z, @no_collision_mask_match + + push hl + call rect_tr + pop hl ret diff --git a/src/wram.s b/src/wram.s index 5c054d1..243b85c 100644 --- a/src/wram.s +++ b/src/wram.s @@ -68,6 +68,10 @@ player: .adv act_size actors: .adv act_size * ACTS_MAX map_objs: .adv mo_size * MAP_OBJ_MAX rectangles: .adv r_size * RECT_MAX + + ; temporary rectangle used for collision checks + ; during movement +tmp_rect: .adv r_size ; current row that is being drawn map_curr_row: .adv 1 -- 2.30.2