TheTower:
  db 10, "THE TOWER "
  db 10, "          "
  db 9, "calamity "
  db 9, "collapse "
  db 9, "change   "
  dw .BackgroundCopyEnd - .BackgroundCopy
  dw .BackgroundCopy
  dw .KeyArtTilesEnd - .KeyArtTiles
  dw .KeyArtTiles
  dw .SpriteTilesEnd - .SpriteTiles
  dw .SpriteTiles
  dw .fInit
  dw .fUpdate
  dw .fDraw
  
.fInit: 
  ld hl, CVS
  ld a, 0 
  ld [hl+], a ; CVS timer for lightning bolt
  ld [hl+], a 
  ld [hl+], a ; CVS+2 frame of lightning bolt
  ld [hl+], a ; CVS+3 timer for flame
  ld [hl+], a 
  ld [hl+], a ; CVS+5 frame of flame cycle
  
  ; past the 18 sprites needed for the lightning bolt
  ; and past the 16 sprites needed for the ground flash  
  ; we need to set sprites to use palette 1, not palette 0
  ld hl, MY_OAM+(18+16)*4
  inc hl ; now poining at x
  inc hl ; now pointing at tile id
  inc hl ; now pointing at attributes
  set OAMB_PAL1, [hl]
  inc hl ; now pointing at y for next sprite
  inc hl ; now poining at x
  inc hl ; now pointing at tile id
  inc hl ; now pointing at attributes
  set OAMB_PAL1, [hl]
  inc hl ; now pointing at y for next sprite
  inc hl ; now poining at x
  inc hl ; now pointing at tile id
  inc hl ; now pointing at attributes
  set OAMB_PAL1, [hl]
  inc hl ; now pointing at y for next sprite
  inc hl ; now poining at x
  inc hl ; now pointing at tile id
  inc hl ; now pointing at attributes
  set OAMB_PAL1, [hl]
  
  ld a, %11_10_00_00
  ld [rOBP0], a 
  ld a, %00_10_01_00
  ld [rOBP1], a 
  
  ret
.fUpdate:
  ld hl, CVS
  call IncrementTimer
  
  ld a, [CVS+1] ; checking the high byte of the timer
  cp a, $40 ; $10 = 1 second, $02 = 1/8 of a second
  jp c, :+ 
  
  ld a, 0 
  ld [CVS], a 
  ld [CVS+1], a 
  
  ld a, 1 
  ld [CVS+2], a 
:

  ld a, [CVS+2]
  cp a, 0
  jp z, :+
  
  ld b, 20
  inc a
  call ArrayClampLoopingB
  ld [CVS+2], a 
  
  
  ld hl, .bolt
  ld de, MY_OAM
  ld b, 8*(2+1)
  ld c, 8*(1+1+3)
  ld a, $55
  call BuildMetaSpritePacked
  
  ld hl, .doorAndFloor
  ld de, MY_OAM + 4*18
  ld b, 8*(2+1+8)
  ld c, 8*(1+1)
  ld a, $58
  call BuildMetaSpritePacked
  
:

  ld a, [CVS+2]
  cp a, 0
  call z, .clearBolts
  ld a, [CVS+2]
  cp a, 6
  call z, .clearBolts
  ld a, [CVS+2]
  cp a, 7
  call z, .clearBolts
  ld a, [CVS+2]
  cp a, 8
  call z, .clearBolts
  ld a, [CVS+2]
  cp a, 9
  call z, .clearBolts


  ld hl, CVS+3
  call IncrementTimer
  
  ld a, [CVS+3+1] ; checking the high byte of the timer
  cp a, $01 ; $10 = 1 second, $02 = 1/8 of a second
  jp c, :+ 
  
  ld a, 0 
  ld [CVS+3], a 
  ld [CVS+3+1], a 
  
  ld b, 12
  ld a, [CVS+5]
  inc a 
  call ArrayClampLoopingB
  ld [CVS+5], a 

  call .cycle
:

  ret  
.clearBolts:
  ld hl, ZEROES
  ld de, MY_OAM
  ld bc, 4*(16+18)
  call CopyRange
  ret
.cycle:
  ld hl, .flames
  ld a, [CVS+5]
  and a, %0000_0011
  sla a 
  sla a
  ld c, a 
  ld b, 0 
  add hl, bc
  ; hl now points to the correct frame of .flames
  
  ld de, MY_OAM + (18+16)*4
  ld b, (6+2+1)*8
  ld c, (1+1+1)*8
  ld a, $22
  call BuildMetaSprite
  ret
.fDraw: 
  ld a, [CVS+5]
  sra a
  sra a
  cp a, 0
  jp z, .frame0
  cp a, 1 
  jp z, .frame1
  cp a, 2
  jp z, .frame2

.frame0
  ld a, %10_00_01_00
  ld [rOBP1], a
  ret
.frame1
  ld a, %01_10_00_00
  ld [rOBP1], a
  ret
.frame2
  ld a, %00_01_10_00
  ld [rOBP1], a
  ret



.bolt: ; $55, 18 active sprites
  db $00, $00, $00, $00, $01
	db $00, $0a, $0b, $02, $03
	db $1c, $20, $04, $05, $06
	db $21, $07, $08, $0c, $09
	db $22, $0e, $0f, $0d, $00
.doorAndFloor: ; $58, 16 active sprites
  db $00, $00, $00, $00, $00, $1d, $00, $00
	db $00, $00, $00, $00, $10, $1f, $00, $00
	db $00, $00, $00, $00, $11, $12, $00, $00
	db $15, $16, $17, $17, $13, $14, $19, $1e
	db $18, $00, $00, $00, $00, $00, $1a, $1b
.burningWindow: ; $21, 2 active sprites
  db $21
	db $22
.flames: ; $22, stride of four, four sprites
  db $2a, $2b
  db $31, $2f
  
  db $24, $25
  db $28, $30
  
  db $26, $27
  db $29, $2e
  
  db $2c, $2d
  db $23, $32

.SpriteTiles:
  db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $30,$00,$30,$00,$38,$00,$18,$00,$18,$00,$3c,$00,$ce,$00,$07,$00
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$3c,$00,$ff,$00,$67,$00
	db $06,$00,$06,$00,$0e,$00,$0e,$00,$3c,$00,$7c,$00,$f8,$00,$e0,$00
	db $00,$00,$01,$00,$01,$00,$03,$00,$03,$00,$03,$00,$03,$00,$03,$00
	db $ff,$00,$e0,$00,$a0,$00,$a0,$00,$20,$00,$10,$00,$10,$00,$00,$00
	db $e0,$00,$20,$00,$20,$00,$10,$00,$08,$00,$08,$00,$10,$00,$20,$00
	db $01,$00,$00,$00,$00,$00,$00,$00,$00,$00,$01,$3c,$3f,$40,$3f,$40
	db $03,$00,$83,$00,$47,$00,$3e,$00,$1c,$00,$f8,$00,$f0,$00,$20,$00
	db $e0,$00,$10,$00,$08,$00,$08,$00,$04,$00,$04,$00,$04,$00,$00,$00
	db $00,$00,$04,$00,$03,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $08,$00,$08,$00,$38,$00,$c8,$00,$04,$00,$02,$00,$01,$00,$00,$00
	db $00,$00,$80,$00,$80,$00,$80,$00,$80,$00,$40,$00,$20,$00,$10,$00
	db $1e,$00,$10,$00,$30,$00,$20,$00,$20,$00,$40,$00,$40,$00,$00,$00
	db $3e,$41,$3e,$41,$3e,$41,$3e,$41,$3e,$41,$3e,$41,$00,$7f,$00,$3f
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$80,$00,$80
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$07,$00,$0d
	db $00,$0d,$00,$0d,$00,$1d,$00,$15,$00,$15,$00,$15,$00,$15,$00,$15
	db $00,$60,$00,$70,$00,$50,$00,$50,$00,$50,$60,$10,$00,$70,$00,$50
	db $00,$15,$00,$15,$00,$15,$00,$ff,$00,$ff,$00,$ff,$00,$00,$00,$00
	db $00,$50,$00,$50,$00,$50,$00,$50,$00,$fe,$00,$ff,$00,$1f,$00,$01
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$07,$00,$1f,$00,$f0
	db $00,$00,$00,$00,$00,$00,$00,$03,$00,$ff,$00,$ff,$00,$f0,$00,$00
	db $00,$00,$00,$00,$00,$00,$00,$ff,$00,$ff,$00,$ff,$00,$00,$00,$00
	db $00,$c0,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$c0,$00,$fc,$00,$ff
	db $00,$0f,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $00,$c0,$00,$f0,$00,$1c,$00,$07,$00,$00,$00,$00,$00,$00,$00,$00
	db $00,$00,$00,$00,$00,$78,$00,$80,$00,$00,$00,$00,$00,$00,$00,$00
	db $00,$00,$00,$00,$00,$00,$00,$e0,$00,$1c,$00,$02,$00,$00,$00,$00
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$80
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$c0,$00,$60
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$0e,$00,$01
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$06,$00,$00,$00,$00,$00,$07
	db $00,$00,$00,$00,$00,$03,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $06,$01,$07,$00,$03,$00,$01,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $00,$00,$00,$00,$0f,$00,$0f,$00,$24,$03,$36,$01,$37,$00,$3f,$00
	db $00,$00,$3c,$00,$fe,$00,$fe,$00,$3e,$c0,$0e,$f0,$42,$fc,$10,$fe
	db $00,$00,$00,$00,$08,$00,$2c,$00,$47,$20,$05,$c2,$84,$43,$84,$43
	db $00,$00,$00,$00,$0c,$00,$1e,$00,$fe,$00,$fe,$00,$e0,$1e,$00,$fe
	db $1f,$00,$1e,$01,$0f,$00,$03,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $85,$43,$44,$03,$06,$01,$03,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $3f,$3f,$1f,$10,$0f,$08,$0f,$08,$07,$04,$07,$04,$07,$04,$07,$04
	db $f8,$f8,$fc,$04,$fe,$02,$fe,$02,$0e,$f0,$86,$78,$82,$7c,$c2,$3c
	db $40,$00,$70,$00,$7e,$00,$7f,$00,$30,$0f,$18,$07,$1e,$01,$0e,$01
	db $00,$00,$00,$00,$00,$00,$fe,$00,$0e,$f0,$00,$fe,$c0,$fe,$fc,$fe
	db $be,$fe,$e2,$fe,$02,$fe,$e2,$1e,$e0,$00,$00,$00,$00,$00,$00,$00
	db $c2,$3c,$e0,$1e,$f0,$0e,$f8,$06,$e0,$e0,$00,$00,$00,$00,$00,$00
	db $88,$7e,$06,$fe,$e2,$1e,$f2,$0e,$e0,$00,$00,$00,$00,$00,$00,$00
	db $07,$04,$07,$04,$03,$02,$01,$01,$00,$00,$00,$00,$00,$00,$00,$00
	db $7c,$fe,$0e,$fe,$86,$7e,$e2,$1e,$e0,$00,$00,$00,$00,$00,$00,$00
.SpriteTilesEnd:
; original export script by gabriel reis, modified by shoofle
 
 

 
.KeyArtTiles:
	db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	db $ff,$00,$55,$aa,$ff,$00,$55,$aa,$ff,$00,$5f,$a7,$df,$3f,$7f,$ff
	db $ff,$c0,$fd,$f2,$ff,$fc,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
	db $ff,$00,$55,$aa,$ff,$00,$55,$aa,$ff,$00,$5d,$a2,$ff,$00,$d5,$aa
	db $9f,$7f,$1f,$ff,$3f,$df,$1f,$ff,$9f,$7f,$1f,$ff,$3f,$ff,$3f,$ff
	db $ff,$7f,$7f,$ff,$ff,$7f,$7f,$ff,$ff,$7f,$7f,$ff,$ff,$7f,$ff,$ff
	db $ab,$55,$45,$bb,$ab,$55,$11,$ef,$ab,$55,$45,$bb,$ab,$55,$11,$ef
	db $ab,$55,$57,$ab,$ab,$57,$53,$af,$ab,$57,$57,$ab,$ab,$57,$57,$ab
	db $eb,$17,$57,$ab,$af,$57,$57,$af,$af,$57,$57,$af,$af,$57,$57,$af
	db $ef,$17,$57,$af,$bf,$4f,$5f,$af,$ef,$1f,$5f,$af,$bf,$4f,$5f,$af
	db $ea,$15,$55,$aa,$aa,$55,$55,$aa,$ae,$51,$55,$aa,$aa,$d5,$d5,$aa
	db $ee,$91,$d5,$aa,$bb,$c4,$d5,$aa,$ee,$91,$d5,$ea,$fb,$c4,$d5,$ea
	db $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
	db $80,$7f,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
	db $ff,$3f,$7f,$bf,$ff,$3f,$7f,$bf,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
	db $fe,$c1,$d5,$ea,$fb,$c4,$d5,$ea,$ff,$e0,$f5,$ea,$ff,$e0,$f5,$ea
	db $ff,$e0,$f5,$ea,$ff,$e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a,$55,$00,$ff
	db $aa,$55,$44,$bb,$aa,$55,$10,$ef,$aa,$55,$44,$bb,$aa,$55,$01,$fe
	db $aa,$55,$44,$bb,$aa,$55,$11,$ee,$aa,$55,$45,$ba,$aa,$55,$11,$ee
	db $aa,$55,$55,$aa,$aa,$55,$51,$ae,$aa,$55,$55,$aa,$aa,$55,$55,$aa
	db $ea,$15,$55,$aa,$aa,$55,$55,$aa,$ae,$51,$55,$aa,$aa,$55,$55,$aa
	db $ee,$11,$55,$aa,$bb,$44,$55,$aa,$ee,$11,$55,$aa,$bb,$44,$55,$aa
	db $fe,$01,$55,$aa,$bb,$44,$55,$aa,$ff,$00,$55,$aa,$bf,$40,$55,$aa
	db $ff,$ff,$ff,$fe,$fe,$fe,$fe,$fc,$fc,$fc,$fd,$fc,$f8,$f8,$ff,$ff
	db $7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$3f,$3f,$ff,$ff
	db $ff,$ff,$f8,$f8,$fd,$fc,$fc,$fc,$fe,$fc,$fe,$fe,$ff,$fe,$ff,$ff
	db $ff,$ff,$00,$00,$f5,$e4,$e4,$e4,$ee,$e4,$4e,$4c,$5e,$4e,$3f,$1e
	db $ff,$ff,$00,$00,$fb,$f2,$f3,$f2,$f7,$f2,$f7,$f2,$67,$66,$6f,$66
	db $ff,$ff,$3f,$3f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f,$7f
	db $9f,$1e,$5f,$0f,$4f,$4f,$ef,$e7,$e7,$e7,$f7,$e7,$00,$00,$ff,$ff
	db $6f,$66,$6f,$6e,$0f,$0e,$9f,$0e,$9f,$9e,$9f,$9e,$00,$00,$ff,$ff
	db $ff,$ff,$ff,$ff,$f1,$ff,$ce,$ff,$de,$ff,$de,$ff,$be,$ff,$be,$ff
	db $be,$ff,$be,$ff,$be,$ff,$be,$ff,$a0,$ff,$00,$ff,$01,$ff,$1f,$ff
.KeyArtTilesEnd:

.BackgroundCopy: ; tiles start at 26
	db $36, $36, $36, $36, $36, $36, $36, $36
	db $37, $37, $33, $27, $35, $34, $37, $37
	db $38, $38, $1e, $26, $26, $32, $38, $38
	db $39, $39, $31, $26, $26, $2c, $39, $39
	db $3a, $3a, $1f, $26, $26, $2d, $3a, $3a
	db $3b, $3b, $26, $26, $26, $2e, $3b, $3b
	db $3c, $20, $49, $26, $26, $2f, $3c, $3c
	db $3d, $21, $4a, $26, $26, $30, $3d, $3d
	db $3e, $22, $26, $26, $26, $26, $24, $3e
	db $3f, $23, $26, $26, $26, $26, $25, $3f
	db $40, $2b, $26, $26, $26, $26, $29, $40
	db $1b, $28, $26, $26, $26, $26, $2a, $1d
	db $26, $26, $26, $26, $26, $26, $26, $1c
	db $26, $26, $26, $26, $26, $26, $26, $26
	db $26, $26, $43, $44, $45, $46, $26, $26
	db $26, $26, $41, $47, $48, $42, $26, $26
.BackgroundCopyEnd: