collision: wip player movement
authorLukas Krickl <lukas@krickl.dev>
Mon, 22 Sep 2025 07:53:11 +0000 (09:53 +0200)
committerLukas Krickl <lukas@krickl.dev>
Mon, 22 Sep 2025 07:53:11 +0000 (09:53 +0200)
src/actor.s
src/player.s
src/rectangle.s
src/wram.s

index 3de0ba1068f32dec6593cb444fbd45f4816308a6..b0a30cd8104be418a4cd1e006277874edd9eb75c 100644 (file)
@@ -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
index c71d59751ed4d29dede488c40b19710c2d314755..c835f17f01536da673d22361821d81eb4d270aae 100644 (file)
@@ -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
 
index 63c458093307cbbc486137e6d06fc442d859bfbb..be67463d92e1fc25db2f1a92f4c470c9a82e835b 100644 (file)
@@ -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
index 5c054d165e43f93c3264d2c705ef36a73aec595c..243b85cbfb239ac5193dbb0bbe14774bd57ae817 100644 (file)
@@ -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