Skip to content

Commit

Permalink
Always Animating Sprites
Browse files Browse the repository at this point in the history
Introducing a system to have sprites who always animate, like in-the-air birds mons, snorlax breathing, lapras chilling.
No guarantee of it being bug free.
  • Loading branch information
Engezerstorung committed Sep 14, 2024
1 parent 6d8a48d commit 9a4271d
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 23 deletions.
43 changes: 42 additions & 1 deletion color/colorplus/spritespecialproperties.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SpriteSpecialProperties::
; This function look for sprites in SpecialOAMlist and load special proporties in wSpriteStateData2
; it make use of the previously unused bytes $1, $A, and $B
; it make use of the previously unused bytes $1, $A, $B and $C
xor a
.spriteLoop
ldh [hSpriteOffset2], a
Expand Down Expand Up @@ -32,6 +32,30 @@ SpriteSpecialProperties::
ld [de], a

.nextCheck
; This part of the function look for sprites in AnimatedSpriteList and load an animation value in
; wSpriteStateData2 if found, it make use of the previously unused bytes $C of wSpriteStateData2.
ld d, HIGH(wSpriteStateData2)
ldh a, [hSpriteOffset2]
add $c
ld e, a
ld a, [wSavedSpritePictureID]
ld b, a

ld hl, AnimatedSpriteList
.loop
ld a, [hli]
cp -1
jr z, .nextsprite
cp b
jr z, .found
inc hl
jr .loop

.found
ld a, [hl]
ld [de], a

.nextsprite
ldh a, [hSpriteOffset2]
add $10
cp LOW($100)
Expand Down Expand Up @@ -157,3 +181,20 @@ SpecialOAMlist:
; 3x3 tiles Snorlax
db SPRITE_SNORLAXBIG, $40, 0, 0
db -1

AnimatedSpriteList:
; \1 Sprite to animate,
; \2 Ticks between animation frames, must be > 0.
; A value of 4 is the normal walking animation speed.
; Values > $80 are for special animation paterns.
db SPRITE_ARTICUNO, 4 ; normal speed
db SPRITE_FEAROW, 4 ; normal speed
db SPRITE_KABUTO, 12 ; slower speed
db SPRITE_LAPRAS, 12 ; slower speed
db SPRITE_MOLTRES, 4 ; normal speed
db SPRITE_OMANYTE, 12 ; slower speed
db SPRITE_PIDGEOT, 4 ; normal speed
db SPRITE_SNORLAXBIG, $80 ; snorlax breathing
db SPRITE_SWIMMER, 12 ; slower speed
db SPRITE_ZAPDOS, 4 ; normal speed
db -1
41 changes: 23 additions & 18 deletions data/sprites/facings.asm
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ SpriteFacingAndAnimationTable:
dw .MachineSides, .MachineROAM ; facing right, walk animation frame 2
dw .MachineSides, .MachineROAM ; facing right, walk animation frame 3
; $40 Snorlax 3x3
dw .Snorlax, .SnorlaxOAM ; facing down, walk animation frame 0
dw .Snorlax, .SnorlaxOAM ; facing down, walk animation frame 1
dw .Snorlax, .SnorlaxOAM ; facing down, walk animation frame 2
dw .Snorlax, .SnorlaxOAM ; facing down, walk animation frame 3
dw .Snorlax, .SnorlaxOAM ; facing up, walk animation frame 0
dw .Snorlax, .SnorlaxOAM ; facing up, walk animation frame 1
dw .Snorlax, .SnorlaxOAM ; facing up, walk animation frame 2
dw .Snorlax, .SnorlaxOAM ; facing up, walk animation frame 3
dw .Snorlax, .SnorlaxOAM ; facing left, walk animation frame 0
dw .Snorlax, .SnorlaxOAM ; facing left, walk animation frame 1
dw .Snorlax, .SnorlaxOAM ; facing left, walk animation frame 2
dw .Snorlax, .SnorlaxOAM ; facing left, walk animation frame 3
dw .Snorlax, .SnorlaxOAM ; facing right, walk animation frame 0
dw .Snorlax, .SnorlaxOAM ; facing right, walk animation frame 1
dw .Snorlax, .SnorlaxOAM ; facing right, walk animation frame 2
dw .Snorlax, .SnorlaxOAM ; facing right, walk animation frame 3
dw .Snorlax2, .SnorlaxSleepOAM ; facing down, walk animation frame 0
dw .Snorlax3, .SnorlaxOAM ; facing down, walk animation frame 1
dw .Snorlax2, .SnorlaxSleepOAM ; facing down, walk animation frame 2
dw .Snorlax1, .SnorlaxSleepOAM ; facing down, walk animation frame 3
dw .Snorlax2, .SnorlaxSleepOAM ; facing up, walk animation frame 0
dw .Snorlax3, .SnorlaxOAM ; facing up, walk animation frame 1
dw .Snorlax2, .SnorlaxSleepOAM ; facing up, walk animation frame 2
dw .Snorlax1, .SnorlaxSleepOAM ; facing up, walk animation frame 3
dw .Snorlax2, .SnorlaxSleepOAM ; facing left, walk animation frame 0
dw .Snorlax3, .SnorlaxOAM ; facing left, walk animation frame 1
dw .Snorlax2, .SnorlaxSleepOAM ; facing left, walk animation frame 2
dw .Snorlax1, .SnorlaxSleepOAM ; facing left, walk animation frame 3
dw .Snorlax2, .SnorlaxSleepOAM ; facing right, walk animation frame 0
dw .Snorlax3, .SnorlaxOAM ; facing right, walk animation frame 1
dw .Snorlax2, .SnorlaxSleepOAM ; facing right, walk animation frame 2
dw .Snorlax1, .SnorlaxSleepOAM ; facing right, walk animation frame 3

; insert custom table starting here, each table must contain 4 facings with 4 animation frames each (16 total)

Expand All @@ -101,7 +101,9 @@ SpriteFacingAndAnimationTable:
.MachineCenter:db $01, $01
.MachineSides: ; fallthrough for $00

.Snorlax: db $00, $01, $00, $02, $03, $02, $04, $05, $04
.Snorlax3: db $00, $01, $00, $06, $07, $06, $08, $09, $08
.Snorlax2: db $0a, $00, $01, $00, $04, $05, $04, $08, $09, $08
.Snorlax1: db $0b, $00, $01, $00, $02, $03, $02, $08, $09, $08

.NormalOAM:
; y, x, attributes
Expand Down Expand Up @@ -135,8 +137,11 @@ SpriteFacingAndAnimationTable:
db 12, 0, $00
db 12, 8, OAM_HFLIP | OAMFLAG_ENDOFDATA

.SnorlaxOAM:
.SnorlaxSleepOAM:
; y, x, attributes
db -10, 14, $00 ; sleep
; fallthrough
.SnorlaxOAM:
db -3, -4, $00 ; top left
db -3, 4, $00 ; top center
db -3, 12, OAM_HFLIP ; top right
Expand Down
57 changes: 55 additions & 2 deletions engine/overworld/movement.asm
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ UpdateNPCSprite:
ldh a, [hCurrentSpriteOffset]
ld l, a
inc l

ld d, h
inc d
add $c
ld e, a
ld a, [de]
ld [wAnimationStatus], a

ld a, [hl] ; x#SPRITESTATEDATA1_MOVEMENTSTATUS
and a
jp z, InitializeSpriteStatus
Expand Down Expand Up @@ -304,11 +312,34 @@ UpdateSpriteInWalkingAnimation:
ldh a, [hCurrentSpriteOffset]
add $7
ld l, a

ld c, 4
ld a, [wSpriteFlags]
bit 5, a
jr z, .gotAnimationSpeed
ld a, [wAnimationStatus]
ld c, a
cp $80
jr c, .gotAnimationSpeed

inc l
ld a, [hld]
and a
ld c, 6
jr z, .gotAnimationSpeed
cp 2
jr z, .gotAnimationSpeed
ld c, 20

.gotAnimationSpeed
ld a, [hl] ; x#SPRITESTATEDATA1_INTRAANIMFRAMECOUNTER
inc a
ld [hl], a ; [x#SPRITESTATEDATA1_INTRAANIMFRAMECOUNTER]++
cp $4
jr nz, .noNextAnimationFrame
cp c
jr c, .noNextAnimationFrame

; cp $4
; jr nz, .noNextAnimationFrame
xor a
ld [hl], a ; [x#SPRITESTATEDATA1_INTRAANIMFRAMECOUNTER] = 0
inc l
Expand All @@ -317,6 +348,11 @@ UpdateSpriteInWalkingAnimation:
and $3
ld [hl], a ; advance to next animation frame every 4 ticks (16 ticks total for one step)
.noNextAnimationFrame

ld a, [wSpriteFlags]
bit 5, a
ret nz

ldh a, [hCurrentSpriteOffset]
add $3
ld l, a
Expand Down Expand Up @@ -376,6 +412,17 @@ UpdateSpriteInWalkingAnimation:

; update [x#SPRITESTATEDATA2_MOVEMENTDELAY] for sprites in the delayed state (x#SPRITESTATEDATA1_MOVEMENTSTATUS)
UpdateSpriteMovementDelay:
ld a, [wAnimationStatus]
and a
jr z, .notAlwaysAnimating

ld hl, wSpriteFlags
set 5, [hl]
ld h, HIGH(wSpriteStateData1)

call UpdateSpriteInWalkingAnimation
.notAlwaysAnimating

ld h, HIGH(wSpriteStateData2)
ldh a, [hCurrentSpriteOffset]
add $6
Expand All @@ -397,6 +444,12 @@ UpdateSpriteMovementDelay:
ld l, a
ld [hl], $1 ; [x#SPRITESTATEDATA1_MOVEMENTSTATUS] = 1 (mark as ready to move)
notYetMoving:

ld hl, wSpriteFlags
bit 5, [hl]
res 5, [hl]
jp nz, UpdateSpriteImage
ld h, HIGH(wSpriteStateData1)
ldh a, [hCurrentSpriteOffset]
add SPRITESTATEDATA1_ANIMFRAMECOUNTER
Expand Down
Binary file modified gfx/sprites/mons/snorlax3x3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions ram/wram.asm
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ wSpriteStateData2::
; - 9: original facing direction (backed up by DisplayTextIDInit, restored by CloseTextDisplay)
; - A: Custom Y pixel offset for special sprites (see data/sprites/facings.asm)
; - B: Custom X pixel offset for special sprites (see data/sprites/facings.asm)
; - C
; - C: Custom - animation status (when always animating, ticks between frames, values > $80 are for special animation paterns )
; - D: picture ID
; - E: sprite image base offset (in video ram, player always has value 1, used to compute sprite image index)
; - F
Expand Down Expand Up @@ -1885,9 +1885,11 @@ wPlayerCoins:: dw ; BCD
wMissableObjectFlags:: flag_array $100
wMissableObjectFlagsEnd::

ds 3
ds 2
; ds 6

wAnimationStatus:: db

;; SpriteSets offsets of the current and previous sprite set
wSpriteSetOffset::
wCurSpriteSetOffset:: db
Expand All @@ -1896,6 +1898,9 @@ wPrevSpriteSetOffset:: db
;; bit 0 - is 1 if LoadMapSpriteTilePatterns called by OW spriteset loading
;; bit 1 - is 1 if the Sprite set and previous Sprite set are different
;; bit 2 - is 1 when sprite is already loaded in the same slot in vram
;; bit 3 - unused
;; bit 4 - unused
;; bit 5 - used in always animating sprites during delayed movement status
;; bit 6 - used in some Color Plus functions to check for coordinates
;; bit 7 - when set prevent VramSwap to load in the vram text space
wSpriteFlags:: db
Expand Down

0 comments on commit 9a4271d

Please sign in to comment.