animation: Added actor runtime tile offsets
authorLukas Krickl <lukas@krickl.dev>
Mon, 8 Sep 2025 15:18:46 +0000 (17:18 +0200)
committerLukas Krickl <lukas@krickl.dev>
Mon, 8 Sep 2025 15:18:46 +0000 (17:18 +0200)
src/animation.s
src/defs.s
src/macros.inc
src/unit.s

index 176e29dfd93a4ec4190e401a70afce068a11cf7c..d85e40c44f09a141a64c021e9044ab8058eb3609 100644 (file)
@@ -1,3 +1,93 @@
-; animation tables
-; each actor type has a table of all frames
-; of animation it may have
+
+
+  ; calls unit's draw function
+  ; inputs:
+  ;   hl: actor ptr
+unit_update_draw:
+  push hl
+  ld de, act_draw
+  add hl, de
+  ; hl = draw ptr
+  ld a, [hl+]
+  ld e, a
+  ld a, [hl]
+  ld d, a
+  ; de = draw function
+  push de
+  pop hl ; hl = draw function
+  pop de ; de = actor
+  call_hl
+  ret
+
+  ; gets the left tile offset
+  ; for object 1
+  ; based on the y flip of oam
+  ; inputs:
+  ;   a: oam flags
+  ; returns:
+  ;   a: x-offset
+get_left_tile_offset1:
+  and a, OAM_FXFLIP 
+  jr z, @not_set REL
+  ld a, 8
+  ret
+@not_set:
+  xor a, a
+  ret 
+
+  ; gets the left tile offset
+  ; for object 2
+  ; based on the y flip of oam
+  ; inputs:
+  ;   a: oam flags
+  ; returns:
+  ;   a: x-offset
+get_left_tile_offset2:
+  and a, OAM_FXFLIP 
+  jr z, @not_set REL
+  xor a, a
+  ret
+@not_set:
+  ld a, 8
+  ret
+       
+       ; draws the unit's tile
+       ; based on the set oam tile 
+       ; and the currently set offset 
+       ; each actor has 2 tiles drawn here
+       ; tile 1 is the selected tile
+       ; tile 2 is the tile next (+1) to the selected tile
+  ; inputs
+  ;   de: actor
+unit_draw:
+  push de
+  ld hl, act_oam_tile
+  add hl, de ; hl = tile 
+  ld a, [hl+]
+
+  ld b, a ; base tile
+  ld a, [hl+]
+  ld c, a ; flags
+       ld a, [hl] ; a = tile offset
+       BREAK
+       add a, b ; base tile + offset
+       ld b, a ; b = real tile
+
+       ld a, c ; c = oam flags for offset call
+  call get_left_tile_offset1
+  push bc
+  call unit_generic_draw
+  
+  ; draw same tile again 
+  pop bc
+  pop de
+  push de
+  inc b  
+  inc b ; b+=2
+  ld a, c ; a = flags
+  call get_left_tile_offset2
+  call unit_generic_draw 
+  pop de
+
+  ldnull bc
+  ret
index 046d450edbeabfa00ed02dab0ad038a56b6c9a86..b004e4c7e960850f72556d405226fce016089fb5 100644 (file)
   ;   tile table. This can be implemented as needed
 .de act_oam_tile, 1
 .de act_oam_flags, 1
+       
+       ; offset from the currently selected tile
+       ; animations work by applying this offset
+       ; to the selected base tile
+       ; this allows runtime configuration
+       ; of the frames
+       ; e.g. the offset could be switched to 0/2 based 
+       ; on the current frame count
+       ; to make an idel animation
+       ; note: the offset should always be an even number
+       ; due to the use of 8x16 objects
+.de act_rt_oam_tile_offset, 1
   
 .de act_size, 0
   
index 2cfc7ed6a26bcda15f03c6f8919a2e5b35a1fe53..7ad9c9898d37e33b2333353adac2757585c308d7 100644 (file)
   dw $1
   .db $2
   .db $3
+       ; tile offset
+       .db 0 
 #endmacro
 
   ; define an actor without state
index 451d710ab71a6fa8deb218d24794ae1e1d86f225..a64eff721103ff6616a1c8a2501adde4a86ea4c8 100644 (file)
@@ -46,86 +46,6 @@ units_update:
 
   ret
 
-  ; calls unit's draw function
-  ; inputs:
-  ;   hl: actor ptr
-unit_update_draw:
-  push hl
-  ld de, act_draw
-  add hl, de
-  ; hl = draw ptr
-  ld a, [hl+]
-  ld e, a
-  ld a, [hl]
-  ld d, a
-  ; de = draw function
-  push de
-  pop hl ; hl = draw function
-  pop de ; de = actor
-  call_hl
-  ret
-
-  ; gets the left tile offset
-  ; for object 1
-  ; based on the y flip of oam
-  ; inputs:
-  ;   a: oam flags
-  ; returns:
-  ;   a: x-offset
-get_left_tile_offset1:
-  and a, OAM_FXFLIP 
-  jr z, @not_set REL
-  ld a, 8
-  ret
-@not_set:
-  xor a, a
-  ret 
-
-  ; gets the left tile offset
-  ; for object 2
-  ; based on the y flip of oam
-  ; inputs:
-  ;   a: oam flags
-  ; returns:
-  ;   a: x-offset
-get_left_tile_offset2:
-  and a, OAM_FXFLIP 
-  jr z, @not_set REL
-  xor a, a
-  ret
-@not_set:
-  ld a, 8
-  ret
-
-  ; inputs
-  ;   de: actor
-unit_draw:
-  push de
-  ld hl, act_oam_tile
-  add hl, de ; hl = tile 
-  ld a, [hl+]
-
-  ld b, a ; tile
-  ld a, [hl]
-  ld c, a ; flags
-  call get_left_tile_offset1
-  push bc
-  call unit_generic_draw
-  
-  ; draw same tile again 
-  pop bc
-  pop de
-  push de
-  inc b  
-  inc b ; b+=2
-  ld a, c ; a = flags
-  call get_left_tile_offset2
-  call unit_generic_draw 
-  pop de
-
-  ldnull bc
-  ret
-
   ; adjust an object's position 
   ; based on sub-tile state
   ; inputs: