;==========
;New Sprite
;==========

incsrc subroutinedefs_xkas.asm

!Flag = $7C

print "INIT ", pc
	RTL

Return:
	RTS

print "MAIN ", pc
	PHB
	PHK
	PLB
	JSR Run
	PLB
	RTL
	
Run:
	JSR GFX

	JSL !SubOffscreenX0
	LDA $14C8,x		;If the sprite is dead..
	CMP #$08
	BNE Return		;..return
	LDA $9D
	BNE Return		;Also return if sprites are locked.
	
	JSL $81A7DC
	BCC Return
	
	LDA $C2,x
	PHX
	ASL A
	TAX
	JMP (Ptr,x)
	
Ptr:
	dw Hide	;00
	dw Wait	;01
	dw Spawn	;02

Spawn:
	PLX
	LDA #$10
	STA $1DF9
	JSR DrawSmoke
	JSR SpawnYoshi
	LDA #$01
	STA !Flag
	STZ $14C8,x
	RTS
Wait:
	PLX
	LDA $1540,x
	BEQ NextState	
	LDA #$FF
	STA $78
	STZ $7D
	STZ $7B
	LDA $E4,x
	STA $94
	LDA $14E0,x
	STA $95
	LDA $D8,x
	STA $96
	LDA $14D4,x
	STA $97
	LDA $13
	AND #$03
	TAY
	BNE +
	LDA #$22
	STA $1DF9	
	LDA Movement,y
	STA $B6,x
	STZ $AA,x
	JSL $81802A
+
	RTS
NextState:
	INC $C2,x
	RTS
Hide:
	PLX
	LDA #$FF
	STA $78
	STZ $7D
	STZ $7B
	LDA #$12
	STA $1540,x	; time to wait
	INC $C2,x
	RTS

Movement: 
	db $02,$FE,$03,$FC

SpawnYoshi:
	LDA $186C,x
	BNE EndSpawn
	JSL $82A9DE
	BMI EndSpawn
	LDA #$01
	STA $14C8,y
	LDA #$35
	STA $009E,y
	LDA $94
	STA $00E4,y
	LDA $95
	STA $14E0,y
	LDA $96
	STA $00D8,y
	LDA $97
	STA $14D4,y
	PHX
	TYX
	JSL $87F7D2
	PLX
EndSpawn:
	RTS
	
DrawSmoke:
	LDY #$03
-
	LDA $17C0,y
	BEQ +
	DEY
	BPL -
	RTS
+
	LDA #$01
	STA $17C0,y
	LDA #$1C     
	STA $17CC,y
	LDA $D8,x
	STA $17C4,y
	LDA $E4,x
	STA $17C8,y
	RTS

;Sprite Routines
  
TILEMAP:	
	db $80,$82
	db $A0,$A2
	
XDISP:	
	db $00,$10
	db $00,$10
	
YDISP:	
	db $00,$00
	db $10,$10
	
GFX:
	JSL !GetDrawInfo
	LDA $157C,x
	STA $02
	
	LDA $15F6,x
	STA $04
	
	LDX #$00
	
OAM_Loop:
	LDA $00
	CLC
	ADC XDISP,x
	STA $0300,y
	
	LDA $01
	CLC
	ADC YDISP,x
	STA $0301,y
	
	LDA TILEMAP,x
	STA $0302,y
	
	LDA $04
	ORA $64
	STA $0303,y
	
	INY
	INY
	INY
	INY
	
	INX
	CPX #$04
	BNE OAM_Loop
	
	LDX $15E9
	LDY #$02
	LDA #$03
	JSL $81B7B3
	RTS

