From 67fb9aa9a6f2b19f2eb354a1def86d6ca084dab9 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 8 Sep 2025 17:18:46 +0200 Subject: [PATCH] animation: Added actor runtime tile offsets --- src/animation.s | 96 +++++++++++++++++++++++++++++++++++++++++++++++-- src/defs.s | 12 +++++++ src/macros.inc | 2 ++ src/unit.s | 80 ----------------------------------------- 4 files changed, 107 insertions(+), 83 deletions(-) diff --git a/src/animation.s b/src/animation.s index 176e29d..d85e40c 100644 --- a/src/animation.s +++ b/src/animation.s @@ -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 diff --git a/src/defs.s b/src/defs.s index 046d450..b004e4c 100644 --- a/src/defs.s +++ b/src/defs.s @@ -238,6 +238,18 @@ ; 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 diff --git a/src/macros.inc b/src/macros.inc index 2cfc7ed..7ad9c98 100644 --- a/src/macros.inc +++ b/src/macros.inc @@ -163,6 +163,8 @@ dw $1 .db $2 .db $3 + ; tile offset + .db 0 #endmacro ; define an actor without state diff --git a/src/unit.s b/src/unit.s index 451d710..a64eff7 100644 --- a/src/unit.s +++ b/src/unit.s @@ -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: -- 2.30.2