doors: doors now have collision.
authorLukas Krickl <lukas@krickl.dev>
Mon, 11 Aug 2025 15:44:56 +0000 (17:44 +0200)
committerLukas Krickl <lukas@krickl.dev>
Mon, 11 Aug 2025 15:44:56 +0000 (17:44 +0200)
The door animation now only plays when the player touches a door.
This made the code a lot simpler to deal with overall.

src/objanim.s
src/player.s
src/roompatterns.s
src/unit.s

index acd2e8bda0e18cf2be48d9befa048c8cad2e1676..c0a6736d4ef0ca12fff6e51638d1f84cd93e6645 100644 (file)
@@ -16,7 +16,7 @@ objanim_init:
        ; inputs:
        ;               a: tile flags
        ;               b: tile index
-       ;         c: previous player rt special flags
+       ;  unit_test_collision_pos_y/x
        ;        de: actor
 objanim_door_open:
        push de
@@ -26,23 +26,19 @@ objanim_door_open:
        ld bc, st_size
        call memcpy
        pop bc
-
        pop de
-       ld hl, act_pos_y
-       add hl, de ; hl = act y pos
-       
-       ; save palyer rt flags in flags
-       ; but invert CF_COVERED
-       ld a, c 
+
+       ; clear flags
        ld de, objanim+obja_flags       
+       xor a, a
        ld [de], a
        inc de ; de = y pos
        
-       ld a, [hl+]
+       ld a, [unit_test_collision_pos_y]
        ld [de], a
        inc de ; de = x pos
 
-       ld a, [hl]
+       ld a, [unit_test_collision_pos_x]
        ld [de], a
        inc de ; de = dat
        
@@ -60,11 +56,6 @@ objanim_door_open:
        or a, GPF_PAUSE_UPDATE
        ld [gameplay_flags], a
 
-       ; prevent player from drawing
-       ld de, player_unit
-       ld bc, unit_nop_draw
-       call unit_set_draw
-
        ret
 
        ; state function for door opening animation
@@ -73,7 +64,6 @@ objanim_door_open:
 objanim_door_open_fn:
 #define TMP_Y, scratch
 #define TMP_X, scratch+1
-       
 
        call load_unit_obj
        ld de, objanim+st_size
@@ -83,9 +73,6 @@ objanim_door_open_fn:
        ; de = anim flags 
        
        ; set first object
-       ; reload rt flags from backup while the animation is playing
-       ld a, [de]
-       ld [player_rt_special_flags], a
        inc de ; skip flags
        ld a, [de] ; a = y position
        inc de ; de = x postion
@@ -141,11 +128,6 @@ objanim_door_open_fn:
        and a, ~GPF_PAUSE_UPDATE & 0xFF
        ld [gameplay_flags], a
 
-       ; restore player draw call
-       ld de, player_unit
-       ld bc, player_draw
-       call unit_set_draw
-
        ld bc, st_null
        ret
 #undefine TMP_X
index 463963fe8f6d7cddad3054d4b3fd66a653cb141d..3992e531821cfbcdff47f7f2daec904e95ed068f 100644 (file)
@@ -47,17 +47,7 @@ unit_player_update:
 
        ; hl should still be tile ptr here
        
-       ; check if player is on a door tile
-       ; if so, remove door and queue a map redraw
-       and a, CF_DOOR
-       ; save CF_DOOR flag value to
-       ; return before input handling later
-       push af
-       push de
-       call nz, unit_player_remove_door
-       pop de
-  
-  ; check for exit flags
+       ; check for exit flags
   push de
   call unit_check_exit_hit
   pop de
@@ -66,29 +56,34 @@ unit_player_update:
   call unit_scroll_center
   pop de
 
-       ; return if CF_DOOR was handeled earlier
-       ; and skip inputs
-       ; load next state in case this ret is hit
-       pop af
-       jr nz, @early_ret REL
-
   push de
   call unit_handle_inputs
   pop de
 
+       push de
+       push bc
+       ; test last collision flags
+       ld a, [unit_test_collision_cf_flags]
+       and a, CF_DOOR
+       call nz, unit_player_remove_door
+       pop bc
+       pop de
+
   ret
-@early_ret:
-       ldnull bc
-       ret
        
        ; removes the door tile the player is currently
        ; standing on
        ; inputs:
+       ;       unit_test_collision_pos_y/x 
        ;               de: actor
-       ;               hl: ptr to tile player is on
-       ;                b: tile index
-       ;                a: tile flags
 unit_player_remove_door:
+       ; get tile of last collision
+       ld a, [unit_test_collision_pos_y]
+       ld b, a
+       ld a, [unit_test_collision_pos_x]
+       ld c, a
+       call map_get_tile
+
        ; schedule animation
        push_all
        call objanim_door_open
@@ -102,9 +97,7 @@ unit_player_remove_door:
        ld a, CF_COVERED
        ld [hl], a
 
-       ld hl, act_pos_y
-       add hl, de
-       ld a, [hl] ; load y offset
+       ld a, [unit_test_collision_pos_y] ; load y offset
        call map_request_redraw_at
 
        ret
index 1c4453b1ebad1c2b1b952e3be80b68e3edcba037..4eb6e7a0d8b9a7c1657b141c1e47c1a09dd21dfb 100644 (file)
@@ -125,7 +125,8 @@ room_pattern_flags_translation:
  .db CF_COLLISION, CF_COLLISION 
  .db 0x00 ; floor
  ; doors
- .db CF_DOOR | CF_COVERED, CF_DOOR | CF_COVERED, CF_DOOR | CF_COVERED, CF_DOOR | CF_COVERED 
+ .db CF_COLLISION | CF_DOOR | CF_COVERED, CF_COLLISION | CF_DOOR | CF_COVERED
+ .db CF_COLLISION | CF_DOOR | CF_COVERED, CF_COLLISION | CF_DOOR | CF_COVERED 
  ; roof
  .db CF_COVERED
 
index e592847176ae84947972f92fa12a448bf7daf32a..af77d7eba0ccf500882845288c1dab870d23b110 100644 (file)
@@ -353,13 +353,15 @@ unit_handle_inputs:
 #macro unit_test_collision
   ; perform tile collision check
   push de
+  call unit_get_pos
+  $1 
+
        ; write y and x pos
        ld a, b
        ld [unit_test_collision_pos_y], a
        ld a, c
        ld [unit_test_collision_pos_x], a
-  call unit_get_pos
-  $1 
+
   push bc
   call map_get_tile
        ; set flags result