Separate updating a sprite's position with updating it's flags

This commit is contained in:
Lucas Scharenbroich 2021-11-20 12:16:03 -06:00
parent 0cc28f9e6e
commit b607275089
4 changed files with 59 additions and 9 deletions

View File

@ -108,9 +108,10 @@ DoLoadBG1
stz PlayerYVel stz PlayerYVel
; Add a sprite to the engine and save it's sprite ID ; Add a sprite to the engine and save it's sprite ID
SPRITE_ID equ {SPRITE_16X16+145}
jsr UpdatePlayerLocal jsr UpdatePlayerLocal
lda #$1800+145 ; 16x16 sprite, tile ID = 64 lda #SPRITE_ID ; 16x16 sprite, tile ID = 145
ldx PlayerX ldx PlayerX
ldy PlayerY ldy PlayerY
jsl AddSprite jsl AddSprite
@ -240,7 +241,7 @@ EvtLoop
lda PlayerID lda PlayerID
ldx PlayerX ldx PlayerX
ldy PlayerY ldy PlayerY
jsl UpdateSprite ; Move the sprite to this local position jsl MoveSprite ; Move the sprite to this local position
; Update the timers ; Update the timers
jsl DoTimers jsl DoTimers
@ -408,6 +409,8 @@ UpdatePlayerLocal
; Simple updates with gravity and collisions. It's important that eveything in this ; Simple updates with gravity and collisions. It's important that eveything in this
; subroutine be done against ; subroutine be done against
UpdatePlayerPos UpdatePlayerPos
stz tmp0 ; build up some flags
stz PlayerStanding stz PlayerStanding
lda PlayerYVel lda PlayerYVel
bmi :no_ground_check bmi :no_ground_check
@ -454,16 +457,20 @@ UpdatePlayerPos
lda MaxGlobalX lda MaxGlobalX
sta PlayerGlobalX sta PlayerGlobalX
ldx #0
lda PlayerXVel lda PlayerXVel
beq :no_dxv beq :no_dxv
bpl :pos_dxv bpl :pos_dxv
ldx #SPRITE_HFLIP
inc inc
bra :no_dxv bra :no_dxv
:pos_dxv :pos_dxv
dec dec
:no_dxv :no_dxv
sta PlayerXVel sta PlayerXVel
stx tmp0
ldx #0
lda PlayerStanding lda PlayerStanding
bne :too_fast bne :too_fast
@ -473,8 +480,17 @@ UpdatePlayerPos
cmp #4 cmp #4
bcs :too_fast bcs :too_fast
:is_neg :is_neg
ldx #SPRITE_VFLIP
sta PlayerYVel sta PlayerYVel
:too_fast :too_fast
txa
ora tmp0
ora #SPRITE_ID
tax
lda PlayerID
jsl UpdateSprite ; Change the tile ID and / or flags
rts rts
; X = coordinate ; X = coordinate

View File

@ -46,7 +46,8 @@ StopScript EXT
; Sprite functions ; Sprite functions
AddSprite EXT AddSprite EXT
UpdateSprite EXT MoveSprite EXT ; Set an existing sprite's position
UpdateSprite EXT ; Change an existing sprite's flags
; Direct access to internals ; Direct access to internals
DoScriptSeq EXT DoScriptSeq EXT

View File

@ -884,12 +884,11 @@ _GetSpriteVBuffAddr
pla pla
rts rts
; Move a sprite to a new location. If the tile ID of the sprite needs to be changed, then ; Update the sprite's flags. We do not allow the size fo a sprite to be changed. That required
; a full remove/add cycle needs to happen ; the sprite to be removed and re-added.
; ;
; A = sprite ID ; A = Sprite ID
; X = x position ; X = Sprite Tile ID and Flags
; Y = y position
UpdateSprite ENT UpdateSprite ENT
phb phb
phk phk
@ -898,11 +897,45 @@ UpdateSprite ENT
plb plb
rtl rtl
_UpdateSprite _UpdateSprite
cmp #MAX_SPRITES*2 ; Make sure we're in bounds cmp #MAX_SPRITES*2 ; Make sure we're in bounds
bcc :ok bcc :ok
rts rts
:ok
stx tmp0 ; Save the horizontal position
and #$FFFE ; Defensive
tax ; Get the sprite index
lda #SPRITE_STATUS_DIRTY ; Content is changing, mark as dirty
sta _Sprites+SPRITE_STATUS,x
lda tmp0 ; Update the Tile ID
sta _Sprites+SPRITE_ID,x ; Keep a copy of the full descriptor
jsr _GetTileAddr ; This applies the TILE_ID_MASK
sta _Sprites+TILE_DATA_OFFSET,x
rts
; Move a sprite to a new location. If the tile ID of the sprite needs to be changed, then
; a full remove/add cycle needs to happen
;
; A = sprite ID
; X = x position
; Y = y position
MoveSprite ENT
phb
phk
plb
jsr _MoveSprite
plb
rtl
_MoveSprite
cmp #MAX_SPRITES*2 ; Make sure we're in bounds
bcc :ok
rts
:ok :ok
stx tmp0 ; Save the horizontal position stx tmp0 ; Save the horizontal position
and #$FFFE ; Defensive and #$FFFE ; Defensive

View File

@ -25,7 +25,7 @@ _MarkDirtySprite
; Clip the sprite's extent to the screen so we can assume (mostly) position values from here on out. Note that ; Clip the sprite's extent to the screen so we can assume (mostly) position values from here on out. Note that
; the sprite width and height are _only_ used in the clip and afterward all calculation use the clip rect ; the sprite width and height are _only_ used in the clip and afterward all calculation use the clip rect
; ;
; OPTIMIZATION NODE: These values can be calculated in AddSprite/UpdateSprite once and stored in the sprite ; OPTIMIZATION NODE: These values can be calculated in AddSprite/MoveSprite once and stored in the sprite
; record since the screen size doesn't change. ; record since the screen size doesn't change.
lda _Sprites+SPRITE_ID,x ; Get an index into the height/width tables based on the sprite bits lda _Sprites+SPRITE_ID,x ; Get an index into the height/width tables based on the sprite bits