Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Redraw NPCs
- ld hl, wPlayer
- ld de, wShadowOAM + 2 * 4 ; Reserve two objects for player shifting
- .redrawNPC
- call GetCameraRelativePosition
- ldh a, [hCameraRelativePosition]
- add a, (256 - SCRN_Y) / 2
- ldh a, [hCameraRelativePosition+1]
- adc a, 0
- jr nz, .npcCulledOut
- ldh a, [hCameraRelativePosition+2]
- add a, (256 - SCRN_X) / 2
- ldh a, [hCameraRelativePosition+3]
- adc a, 0
- jr nz, .npcCulledOut
- ld a, [hli] ; Read display type
- add a, a ; Double for upcoming ptr computation
- ; (assuming that the object has no more than 128 different types)
- ld c, a
- push hl ; Save this pointer to do write-back later
- ld a, [hli] ; Read display counter
- ld b, a
- ld a, [hli]
- ldh [hNPCTileID], a
- ld a, [hli]
- ldh [hNPCAttr], a
- ld a, [hli] ; Read draw struct bank
- rst bankswitch
- ld a, [hli] ; Read draw struct ptr
- ld h, [hl]
- add a, c ; Add display type offset
- ld l, a
- adc a, h
- sub l
- ld h, a
- ld a, [hli]
- ld h, [hl]
- ld l, a
- ld c, [hl] ; Read anim length (don't inc to improve upcoming loop)
- ld a, b ; Get display counter
- inc a
- sub c
- jr nc, .wrapAnimCnt
- add a, c
- .wrapAnimCnt
- ld c, a
- ; Use current value, because increment should only affect next frame
- ; Important for player tile loading to work correctly
- ld a, b
- .seekAnimFrame
- inc hl ; Advance to length
- sub [hl]
- inc hl ; Skip anim counter
- inc hl ; Skip one byte of ptr (so we can still read efficiently below)
- jr nc, .seekAnimFrame
- ld a, [hld]
- ld l, [hl]
- ld h, a
- ; Now, write the OAM entries
- ld a, [hli] ; Read nb of OAM entries
- .writeOAMEntry
- ldh [hNPCRemainingEntries], a
- ldh a, [hCameraRelativePosition]
- add a, [hl]
- inc hl
- dec a
- cp SCRN_Y + 16 - 1
- jr nc, .offscreen
- inc a
- ld [de], a
- inc e ; inc de
- ldh a, [hCameraRelativePosition+2]
- add a, [hl]
- inc hl
- dec a
- cp SCRN_X + 8 - 1
- jr nc, .offscreenCancel
- inc a
- ld [de], a
- inc e ; inc de
- ldh a, [hNPCTileID]
- add a, [hl]
- inc hl
- ld [de], a
- inc e ; inc de
- ldh a, [hNPCAttr]
- xor [hl]
- ld [de], a
- inc e ; inc de
- ld a, e
- cp $A0
- jr nc, .filledOAM
- ; Carry is set here
- .skipOffscreen ; This gets jumped to with carry set
- inc hl ; Skip attr (this is common to all code paths)
- ldh a, [hNPCRemainingEntries]
- dec a
- jr nz, .writeOAMEntry
- .filledOAM ; This gets jumped to on NC
- ; Here: NC <=> OAM is full
- pop hl
- ld [hl], c ; Write back new anim cnt
- jr nc, .OAMFull ; Prevent overflowing OAM
- .npcCulledOut ; This gets jumped to on C if the NPC isn't drawn, which can't have overflowed OAM, therefore NC <=> OAM isn't full
- ; Advance to next NPC, if it exists
- ld a, l
- or sizeof_NPC - 1
- inc a
- ld l, a
- cp LOW(wNPCArrayEnd)
- jr nz, .redrawNPC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement