objanim: Added framework for makign door animations more generic
authorLukas Krickl <lukas@krickl.dev>
Tue, 12 Aug 2025 18:36:57 +0000 (20:36 +0200)
committerLukas Krickl <lukas@krickl.dev>
Tue, 12 Aug 2025 18:36:57 +0000 (20:36 +0200)
src/objanim.s

index 516c7565aba1d6c0e1b46b478030e5f32f872959..bce08f693b82e6b2eaf548e963c2b6c88189f225 100644 (file)
@@ -59,16 +59,56 @@ objanim_door_open:
        ld [gameplay_flags], a
 
        ret
+       
+       ; down animation
+objanim_door_open_down_fn:
+       ld a, [objanim+obja_timer] ; read timer before calling handler
+       push af
+       call objanim_door_open_generic_fn
+
+       pop af
+       push bc ; save next state
+       ; hl = oam ptr
+       inc hl ; skip obj1 y
+
+       ; adjust x position based on timer
+       ; offset position by counter 
+       sra a
+       ld b, a
+       ld a, [hl]
+       sub a, b
+       ; wirte adjusted obj1 x 
+       ld [hl+], a ; hl = obj1 tile
+
+       inc hl ; skip obj1 tile
+       inc hl ; skip obj1 flags
+       inc hl ; skip obj2 y 
+       
+       ; hl = obj2 x 
+       ld a, [hl]
+       add a, b
+       ; write adjusted obj2 x
+       ld [hl+], a
+       
+       pop bc ; restore next state
+       ret
 
        ; state function for door opening animation
-       ; down
+       ; is called by more specific functions that take this base setup
+       ; and set the right tiles and animations after
        ; inputs:
        ;               de: objanim
-objanim_door_open_down_fn:
+       ;       returns:
+       ;               hl: first object used
+       ;                               the objects are guaranteed to be consecutive
+       ;                               and 2 objects will have been reserved
+       ;               bc: next state
+objanim_door_open_generic_fn:
 #define TMP_Y, scratch
 #define TMP_X, scratch+1
 
        call load_unit_obj
+       push hl
        ld de, objanim+st_size
        call load_scroll
 
@@ -86,13 +126,6 @@ objanim_door_open_down_fn:
        ld a, [de] ; a = x position
        tile_to_scrn OBJ_OFF_X, c 
        ld [TMP_X], a
-       ; offset position by counter 
-       push af
-       ld a, [objanim+obja_timer]
-       sra a
-       ld b, a
-       pop af
-       sub a, b
        ld [hl+], a
 
        
@@ -114,13 +147,6 @@ objanim_door_open_down_fn:
        ; set x pos
        ld a, [TMP_X]
        add a, 8 ; move over a tile
-       ; offset position by counter
-       push af
-       ld a, [objanim+obja_timer]
-       sra a
-       ld b, a
-       pop af
-       add a, b
        ld [hl+], a
        
        ; tile 
@@ -130,7 +156,8 @@ objanim_door_open_down_fn:
        ; flags
        xor a, a
        ld [hl], a
-
+       
+       pop hl ; restore object ptr used == return value
        ; check if we are done
        ld a, [objanim+obja_timer]
        inc a