;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; INIT and MAIN JSL targets
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

incsrc subroutinedefs_xkas.asm

!FrameNum = $C2
!Direction = $157C

                    print "INIT ",pc
					STZ !FrameNum,x
					LDA #$00
					STA !Direction,x
					RTL
	                print "MAIN ",pc			
                    PHB
                    PHK				
                    PLB
                    JSR SPRITE_ROUTINE			;Jump to the routine to keep organized
                    PLB
                    RTL     


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; SPRITE_ROUTINE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

XSpeeds:
db $E0,$20
SendReturn:
JMP return

SPRITE_ROUTINE:	  	
					LDA $9D
					BNE SendReturn
					LDA $14C8,x
					CMP #$08
					BNE SendReturn
					JSL $81802A
					PHX
					LDA !Direction,x
					TAX
					LDA XSpeeds,x
					PLX
					STA $B6,x
					STZ !FrameNum,x
					LDA $15B8,x
					BEQ .NoSlope
					BPL .RightSlope
					LDA !Direction,x
					BNE +
					LDA #$04
					STA !FrameNum,x
					BRA .NoSlope
					+
					LDA #$02
					STA !FrameNum,x
					BRA .NoSlope
					.RightSlope
					LDA !Direction,x
					BNE +
					LDA #$02
					STA !FrameNum,x
					BRA .NoSlope
					+
					LDA #$04
					STA !FrameNum,x
					.NoSlope
					
					return:
					JSL !SubOffscreen
					LDY $15EA,x             ; get offset to sprite OAM
					STY $04
					JSR SUB_GFX					;Draw the graphics					
                    RTS							;End the routine
					

TileSize:
db $02,$02,$02,$02,$00,$00
					
TilePointers:
dw Straight
dw Up1
dw Up2
dw Down1
dw Down2

XOffPointers:
dw StraightX
dw Up1X
dw Up2X
dw Down1X
dw Down2X

YOffPointers:
dw StraightY
dw Up1Y
dw Up2Y
dw Down1Y
dw Down2Y

Straight:
db $90,$92,$A0,$A2,$80,$80
Up1:
db $94,$96,$A4,$A6,$80,$80
Up2:
db $98,$9A,$A8,$AA,$80,$80
Down1:
db $C4,$C6,$E4,$E6,$80,$80
Down2:
db $C8,$CA,$E8,$EA,$80,$80

StraightX:
db $F8,$08,$F8,$08,$FC,$08
Up1X:
db $F8,$08,$F8,$08,$00,$0C
Up2X:
db $F8,$08,$F8,$08,$06,$0E
Down1X:
db $F8,$08,$F8,$08,$FC,$08
Down2X:
db $F8,$08,$F8,$08,$FA,$02

StraightY:
db $F4,$F4,$FC,$FC,$08,$08
Up1Y:
db $F4,$F4,$FC,$FC,$08,$00
Up2Y:
db $F4,$F4,$FC,$FC,$06,$FE
Down1Y:
db $F4,$F4,$04,$04,$00,$08
Down2Y:
db $F0,$F0,$00,$00,$FC,$04

SUB_GFX:            
					LDA !Direction,x
					STA $0C
					JSL !GetDrawInfo       	;Get all the drawing info, duh
					LDY $04
					PHX
					LDA $14
					LSR #2
					AND #$03
					ASL #6
					STA $0B
					LDA !FrameNum,x
					ASL
					TAX
					REP #$20
					LDA TilePointers,x
					STA $05
					LDA XOffPointers,x
					STA $07
					LDA YOffPointers,x
					STA $09
					SEP #$20
					LDX #$05
					.GFXLoop
					
					PHY
					TXY
					LDA ($07),y
					STA $0D
					LDA $00
					LDY $0C
					BEQ +
					CLC
					ADC $0D
					BRA ++
					+
					SEC
					SBC $0D
					CPX #$04
					BCC ++
					CLC
					ADC #$08
					++
					PLY
					STA $0300,y
					
					PHY
					TXY
					LDA ($09),y
					PLY
					CLC
					ADC $01
					STA $0301,y
					
					PHY
					TXY
					LDA ($05),y
					PLY
					STA $0302,y
					PHX
					LDX $15E9
					LDA $15F6,x
					PLX
					CPX #$04
					BCC +
					ORA $0B
					+
					PHX
					LDX $0C
					BNE +
					ORA #$40
					+
					PLX
					ORA $64
					STA $0303,y
					PHY
					TYA
					LSR #2
					TAY
					LDA TileSize,x
					PHX
					LDX $15E9
					ORA $15A0,x	; horizontal offscreen flag
					PLX
					STA $0460,y	;
					PLY
					INY
					INY
					INY
					INY
					DEX
					BPL .GFXLoop
					PLX
					LDA #$05
					LDY #$FF
					JSL $81B7B3					;/and then draw em
					EndIt:
					RTS
					
