;------------;
; laser ;
;------------;
; Initial XY positions set by generating sprite.

!DecreasePlayerHP = $7FFFED
!SpecialShot = $7FFFF2

OAMStuff:
db $00,$04,$08,$0C,$10,$14,$18,$1C,$20,$24,$28,$2C,$30,$34,$38,$3C,$40,$44,$48,$4C

DamageToDo:
db $02,$08,$0C

Return:
RTS

Main:
PHY ;just to make sure
PHX
TYX
JSL $03B664				;Get mario clipping
JSR GetClusterSpriteClipping		;Get cluster clipping
JSL $03B72B				;Check for contact
PLX
PLY
BCC Plus				;If not interacting, branch
LDA !SpecialShot
TAX
LDA.l DamageToDo,x
CLC
ADC !DecreasePlayerHP
STA !DecreasePlayerHP
LDA #$04
STA $1DF9
LDA #$01
STA $71
JMP BeforeReturn2

Plus:
JSR Graphics
LDA $9D
BNE Return
LDA $1E66,y
ASL A
ASL A
ASL A
ASL A
CLC
ADC $1E52,y
STA $1E52,y
PHP
LDX #$00
LDA $1E66,y
LSR A
LSR A
LSR A
LSR A
CMP #$08
BCC DontORA
ORA #$F0
DEX

DontORA:
PLP
ADC $1E02,y
STA $1E02,y
TXA
ADC $1E2A,y
STA $1E2A,y
LDA $0F72,y
ASL A
ASL A
ASL A
ASL A
CLC
ADC $0F4A,y
STA $0F4A,y
PHP
LDX #$00
LDA $0F72,y
LSR A
LSR A
LSR A
LSR A
CMP #$08
BCC DontORA2
ORA #$F0
DEX

DontORA2:
PLP
ADC $1E16,y
STA $1E16,y
TXA
ADC $1E3E,y
STA $1E3E,y
RTS

;------------------------------;
; Get graphics here.           ;
; Also handles killing cluster ;
;------------------------------;

CheckIfOffScreen:
LDA $1E16,y
CMP $1A
LDA $1E3E,y
SBC $1B
RTS

Graphics:                       ; OAM routine starts here.
JSR CheckIfOffScreen
BNE BeforeReturn2
JSR GetDrawInfo
STZ $03
LDA #$F1
STA $04
LDA $0F72,y
BMI NormalFlip
LDA #$40
STA $03

NormalFlip:
LDA $1E66,y
BPL NormalFlip2
LDA $03
ORA #$80
STA $03

NormalFlip2:
LDA $1E66,y
CLC
ADC #$14
CMP #$28
BCC NotIrregularTile2
LDA $0F72,y
CLC
ADC #$24
CMP #$48
BCS NotIrregularTile
LDA #$F2
STA $04
BRA NotIrregularTile2

NotIrregularTile:
LDA #$F3
STA $04

NotIrregularTile2:
LDX.w OAMStuff,y 		; Get OAM index.
LDA $01				;  |
STA $0201,x			; /
LDA $00
STA $0200,x			; /
LDA $04
STA $0202,x                     ; /
LDA $64
ORA $03
ORA #$05 			; \ Properties per spike, some are rising so this needs a seperate table.
STA $0203,x			; /

GeneralStuff:
PHX
TXA
LSR
LSR
TAX
LDA $1E8E,y
STA $0420,x
PLX
LDA $0201,x
CMP #$F0                                        ; As soon as the spike is off-screen...
BCC Return2

BeforeReturn2:
LDA #$00					; Kill sprite.
STA $1892,y					;
STA $1E02,y					; Clear all tables
STA $1E16,y
STA $1E2A,y
STA $1E3E,y
STA $1E52,y
STA $1E66,y
STA $1E8E,y
STA $0F4A,y
STA $0F72,y

Return2:
RTS

;--------------------------;
; Get cluster spr clipping ;
;--------------------------;

GetClusterSpriteClipping:
LDA $1E16,y
CLC
ADC #$04
STA $04
LDA $1E3E,y
ADC #$00
STA $0A
LDA.b #$02                ; Width of sprite clipping
STA $06
LDA $1E02,y
CLC
ADC #$03
STA $05
LDA $1E2A,y
ADC #$00
STA $0B
LDA #$02                ; Height of sprite clipping
STA $07
RTS

;-------------------------;
; Get draw info. Useful!  ;
;-------------------------;

GetDrawInfo:
LDA #$00
STA $1E8E,y
LDA $1E3E,y
XBA
LDA $1E16,y
REP #$20
SEC
SBC $1A
CLC
ADC #$0040
CMP #$0180
SEP #$20
BCS INVALID
LDA $1E3E,y
XBA
LDA $1E16,y
REP #$20
SEC
SBC $1A
CMP #$FFC0
SEP #$20
BCC NOTINVALID
LDA #$01
STA $1E8E,y

NOTINVALID:
LDA $1E3E,y
XBA
LDA $1E16,y
REP #$20
SEC
SBC $1A
SEC
SBC #$0100
CMP #$0040
SEP #$20
BCS NOTINVALID2
LDA #$01
STA $1E8E,y

NOTINVALID2:
LDA $1E16,y
SEC
SBC $1A
STA $00
LDA $1E02,y
SEC
SBC $1C
STA $01
RTS

INVALID:
PLA
PLA
JMP Return