;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Archer Bute
; by Sonikku
; Description: Stands where you place it and shoots arrows. If Mario is above
; this sprite, it uses different frames and aims a bit higher. Like the flying
; Bute, if killed it falls to the ground and disappears in a cloud of smoke.
PROJECT		= $03 ; custom sprite number of ButeArrow.cfg
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; INIT and MAIN JSL targets
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

incsrc subroutinedefs_trasm.asm

                    dcb "INIT"
                    JSL SubHorizPos
                    TYA
                    STA $157C,x
                    LDA #$B0
                    STA $1540,x
                    RTL

	            dcb "MAIN"			
                    PHB
                    PHK				
                    PLB				
                    JSR SPRITE_ROUTINE	
                    PLB
                    RTL     


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; SPRITE_ROUTINE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SPRITE_ROUTINE	JSR SUB_GFX
	JSL SubOffscreenX0
	LDA $C2,x
	JSL $8086DF
	dcw STATE0&$FFFF 
	dcw STATE1&$FFFF
STATE0	LDA $9D

	BNE RETURN
	JSL $81802A
	JSL SubVertPos
	LDA $14C8,x

	CMP #$08

	BNE DEAD
	JSL $818032
	JSL $81A7DC
	LDA $1528,x
	BEQ NOFIRE
	LDA #$01
	JSL $82ACE5
DEAD	INC $C2,x
	LDA #$D0
	STA $AA,x
RETURN	RTS
NOFIRE	LDA $1540,x
	CMP #$80
	BCS NOFRM
	CMP #$10
	BEQ SHOT
	BMI WAIT
	CMP #$18
	BCC RETURN
	BRA CHARGE
CHKBEL	LDA $D8,x
	SEC
	SBC $96
	STA $0F
	LDA $14D4,x
	SBC $97
	BMI NOPROX
	BNE NOPROX
	LDA $0F
	CMP #$20
NOPROX	RTS
SHOTFRM	dcb $01,$02,$04,$05
CHARGE	JSL SubHorizPos
	TYA
	STA $157C,x
	LDA #$70
	CMP $1540,x
	BCC CHARGIN
	INC $1570,x
CHARGIN	LDA $1570,x
	LSR A
	AND #$01
	TAY
	JSR CHKBEL
	BCC ABO1
	INY
	INY
ABO1	LDA SHOTFRM,y
	STA $1602,x
	RTS
NOFRM	STZ $1602,x
	RTS
SHOT	JSR GENSPRITE
	RTS
WAIT	CMP #$01
	BCC RESET
	JSR CHKBEL
	BCC ABO2
	LDA #$06
	BRA STORE
ABO2	LDA #$03
STORE	STA $1602,x
	RTS
RESET	LDA #$B0
	STA $1540,x
	RTS
KILLDSPD	dcb $08,$F8
KILLDFRM	dcb $08,$09
STATE1	LDA $9D

	BNE RETURN1
	JSL $81802A
	LDA $1588,x
	AND #$08
	BEQ NOCEIL
	STZ $AA,x
NOCEIL	LDA #$18
	STA $1686,x
	LDA #$A1
	STA $167A,x
	LDA $167A,x
	ORA #$04
	STA $167A,x
	LDA #$08
	STA $14C8,x
	STA $154C,x
	STA $1FE2,x
	STZ $1528,X
	LDA $1588,x
	AND #$04
	BEQ INAIR
	STZ $AA,x
	STZ $B6,x
	LDA $1570,x
	LSR A
	LSR A
	AND #$01
	TAY
	LDA KILLDFRM,y
	STA $1602,x
	INC $1594,x
	LDA $1594,x
	CMP #$20
	BCC RETURN1
	INC $1570,x
	LDA $1570,x
	CMP #$50
	BCC RETURN1
	LDA #$04
	STA $14C8,x
	LDA #$1F
	STA $1540,x
	JSL $87FC3B
	LDA #$08
	STA $1DF9
	RTS
INAIR	LDY $157C,x
	LDA KILLDSPD,y
	STA $B6,x
	LDA #$07
	STA $1602,x
RETURN1	RTS
GENSPRITE
	LDA $15A0,x
	ORA $186C,x
	BNE RTNSPWN
	JSL $82A9DE
	BMI RTNSPWN
	LDA #$01
	STA $14C8,y
	PHX
	LDA #PROJECT
	TYX
	STA $7FAB9E,x
	PLX
	LDA $E4,x
	STA $00E4,y
	LDA $14E0,x
	STA $14E0,y
	LDA $D8,x
	SEC
	SBC #$03
	STA $00D8,y
	LDA $14D4,x
	SBC #$00
	STA $14D4,y
	PHX
	TYX
	JSL $87F7D2
	JSL $8187A7
	LDA #$08
	STA $7FAB10,x
	PLX
	LDA $157C,x
	STA $157C,y
	JSR CHKBEL
	BCC ABO3
	LDA #$E0
	BRA STRSPD
ABO3	LDA #$00
STRSPD	STA $00AA,y
	STA $1594,y
	LDA #$30
	STA $1540,y
RTNSPWN	RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; GRAPHICS ROUTINE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TILEMAP	dcb $0A,$0C,$0E,$28,$2A,$2C,$2E,$04,$06,$08
SUB_GFX	    JSL GetDrawInfo       ; sets y = OAM offset
		    LDA $1602,x
		    STA $03		 ;  | $03 = index to frame start (0 or 1)
		    LDA $157C,x             ; $02 = sprite direction
		    STA $02
		    PHX		     ; /
                    LDA $00		 ; \ tile x position = sprite x location ($00)
		    STA $0300,y	     ; /

		    LDA $01		 ; \ tile y position = sprite y location ($01)
		    STA $0301,y	     ; /

		    LDA $15F6,x	     ; tile properties xyppccct, format
		    LDX $02		 ; \ if direction == 0...
		    BNE NO_FLIP	     ;  |
		    ORA #$40		; /    ...flip tile
NO_FLIP	                  ORA $64		 ; add in tile priority of level
		    STA $0303,y	     ; store tile properties

		    LDX $03		 ; \ store tile
		    LDA TILEMAP,x	   ;  |
		    STA $0302,y	     ; /

		    INY		     ; \ increase index to sprite tile map ($300)...
		    INY		     ;  |    ...we wrote 1 16x16 tile...
		    INY		     ;  |    ...sprite OAM is 8x8...
		    INY		     ; /    ...so increment 4 times

		    PLX		     ; pull, X = sprite index
		    LDY #$02		; \ 460 = 2 (all 16x16 tiles)
		    LDA #$00		;  | A = (number of tiles drawn - 1)
		    JSL $81B7B3	     ; / don't draw if offscreen
		    RTS		     ; return


