; gameover.asm

GameOver:

    	;xor a
    	;ld hl, wOAMBuffer
    	;ld bc, 8*5*4
    	;call FillMemory

    call DelayFrame
    call HideSprites
    			;call CutToBlack
    call $2233    ;StopAllMusic
    call LoadGameOverGfx

    			;call ResetPalettes

    ld a, $80
    coord hl, 0, 0
    ld bc, 20 * 18
    call FillMemory
    
    coord hl, 7, 3
    ld e, $A4
    ld bc, DebbieGfxInstructions
.drawDebbieLoop
    ld a, [bc]
    ld d, a
    call DrawIncTiles

    inc bc
    ld a, [bc]
    cp $FF
    jr z, .exitDrawDebbieLoop

    push bc
    ld b, 0
    ld c, a
    add hl, bc
    pop bc
    inc bc
    jr .drawDebbieLoop

.exitDrawDebbieLoop

    ld a, $98
    ldh [$FFBD], a
    ldh [$FFBA], a

    call Delay3

    coord hl, $00, $00

    
    	;coord hl, $00, $0B
    lb bc, 5, 18
    
    push hl
    ld a, $9C
    ld [hli], a
    inc a ; ─
    call NPlaceChar
    inc a ; ┐
    ld [hl], a
    pop hl

    ld de, SCREEN_WIDTH
    add hl, de

.next

    push hl
    ld a, $9F
    ld [hli], a
    ld a, $81
    call NPlaceChar
    ld [hl], $A0
    pop hl

    ld de, SCREEN_WIDTH
    add hl, de
    dec b
    jr nz, .next
    
    ld a, $A1
    ld [hli], a
    inc a
    call NPlaceChar
    inc a
    ld [hli], a

    ld c,SCREEN_WIDTH
    ld a,$80
    call NPlaceChar

    
    	;coord hl, $01, $03	; $01, $0E
    	;lb de, $05, $92
    	;call DrawIncTiles

    			;coord hl, $06, $0E
    coord hl, $06, $03
    lb de, $08, $82
    call DrawIncTiles

    	;lb de, $05, $92
    	;call DrawIncTiles

    	;coord hl, $01, $04	; $01, $0F
    	;lb de, $05, $97
    	;call DrawIncTiles

    			;coord hl, $06, $0E
    coord hl, $06, $04
    lb de, $08, $8A
    call DrawIncTiles

    	;lb de, $05, $97
    	;call DrawIncTiles
    

    				;coord hl, $06, $0F
    					;lb de, $08, $8A
    				;ld d, $08
    				;call DrawIncTiles


    	;ld hl, wDrawSpriteInd
    	;ld a, 0
    	;ld [hli], a
    	;ld a, $0F
    	;ld [hli], a
    	;ld a, $7B
    	;ld [hli], a
    	;ld a, $52
    	;ld [hli], a
    	;ld a, $92
    	;ld [hli], a
    	;xor a
    	;ld [hl], a
    	;call DrawSprite_M

    	;ld hl, wDrawSpriteInd
    	;ld a, 10
    	;ld [hli], a
    	;ld a, $79
    	;ld [hli], a
    	;ld a, $7B
    	;ld [hli], a
    	;ld a, $52
    	;ld [hli], a
    	;ld a, $92
    	;ld [hli], a
    	;xor a
    	;ld [hl], a
    	;call DrawSprite_M

    ld hl, SmallDebbieOAMData.mid
    ld bc, 16
    ld de, wOAMBuffer
    call CopyData


    ld hl, SmallDebbieOAMData.wing
    call DrawSingleWing

    ld hl, SmallDebbieOAMData.wing + 6
    call DrawSingleWing

    ld hl, SmallDebbieOAMData.wing + 12
    call DrawSingleWing

    ld hl, SmallDebbieOAMData.wing + 18
    call DrawSingleWing
    





    		;ld hl, wOAMBuffer
    		;lb bc, $7B, $0F
    		;lb de, $92, $03
    		;push bc

		;.innerLoop
    		;ld a, b
    		;ld [hli], a
    		;ld a, c
    		;ld [hli], a
    		;ld a, d
    		;ld [hli], a
    		;xor a
    		;ld [hli], a

    		;inc d
    		;ld a, 8
    		;add c
    		;ld c, a
    
    		;dec e
    		;jr nz, .innerLoop

    		;pop bc


    ld a, $9C
    ldh [$FFBD], a
    ldh [$FFBA], a

    xor a
    ldh [$FFBB], a    ;H_AUTOBGTRANSFERPORTION

    call Delay3

    xor a
    ldh [$FFBA], a
    ldh [$FFAE], a
    ldh [$FFAF], a

    ld c, $44
    call DelayFrames

    ld a, $57
    ldh [$FFB0], a

    call GBFadeInFromBlack

    call PlaySong_M2

.forever

    ld c, 16
    call DelayFrames

    ld hl, wOAMBuffer + 18
    ld a, [hl]
    cp $96
    jr nc, .ninetwo
    ld a, $96
    jr .gotValue
.ninetwo
    ld a, $92
.gotValue
    ld e, 4
.outerLoop
    ld bc, 16
    push hl
    ld d, 4
.innerLoop
    ld [hl], a
    add bc
    dec d
    jr nz, .innerLoop
    pop hl
    ld bc, 4
    add bc
    inc a
    dec e
    jr nz, .outerLoop
    

    jr .forever
    
    

DebbieGfxInstructions:
    db 5, SCREEN_WIDTH - 5
    db 7, SCREEN_WIDTH - 7
    db 3, 1, 3, SCREEN_WIDTH - 7
    db 7, SCREEN_WIDTH - 7
    db 2, 1, 4, SCREEN_WIDTH - 7
    db 2, 1, 4, SCREEN_WIDTH - 7
    db 2, 1, 3, $FF

SmallDebbieOAMData:
.mid
    db $7B, $1F, $9A, $00, $83, $1F, $9B, $00
    db $7B, $89, $9A, $00, $83, $89, $9B, $00
.wing
    db $04, $0F, $7B, $22, $92, $00
    db $08, $27, $7B, $22, $92, $02
    db $0C, $79, $7B, $22, $92, $00
    db $10, $91, $7B, $22, $92, $02

DrawIncTiles:
    ld a, e
.loop
    ld [hli], a
    inc a
    dec d
    jr nz, .loop
    ld e, a
    ret

DrawSingleWing:
    ld de, wDrawSpriteInd
    ld bc, 6
    call CopyData
    jp DrawSprite_M