incsrc subroutinedefs_trasm.asm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; INIT and MAIN JSL targets
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	dcb "INIT"
	RTL

	dcb "MAIN"
	PHB
	PHK
	PLB
	JSR SPRITE_ROUTINE
	PLB
	RTL

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; SPRITE_ROUTINE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
carried
CMP #$0B
BEQ notre
LDA #$08
STA $14C8,x
RTS
notre
LDA #$05
STA $1602,x
RETURN	RTS
SPRITE_ROUTINE
LDA $010B
CMP #$0A
BNE NotIntro
LDA $010C
CMP #$01
BNE NotIntro
JSL NEWSTUFF
RTS
NotIntro
	JSR SUB_GFX
	LDA $9D
	BNE RETURN
	LDA $14C8,x
	CMP #$08
	BNE RETURN
	JSL $81802A
	LDA $1510,x
	JSL $8086DF
	dcw STATE0&$FFFF
	dcw STATE1&$FFFF
	dcw STATE2&$FFFF
	dcw STATE3&$FFFF
	dcw STATE2&$FFFF
STATE0	JSL $81A7DC
	JSL SubOffscreenX0
	LDA $1588,x
	AND #$04
	BEQ RETURN0
	LDA $14
	LSR
	AND #$01
	TAY
	STA $1602,x
	LDA #$40
	STA $B6,x
RETURN0	RTS
XMAX	dcb $60,$F0
XACCEL	dcb $02,$FE
STATE1	JSL $81A7DC
	LDA #$09
	STA $15F6,x
	LDA $1588,x
	AND #$04
	BEQ RETURN1
	LDA $14
	LSR
	AND #$01
	TAY
	STA $1602,x
	LDA $C2,x
	BNE NORMAL1
	LDA $1540,x
	AND #$0F
	BEQ NORMAL1
	JSL SubHorizPos
	TYA
	CMP $157C,x
	BEQ RETURN1
	STA $157C,x
	LDA #$01
	STA $C2,x
	BRA NORMAL1_1
NORMAL1	JSL SubOffscreenX0
NORMAL1_1
	LDY $157C,x
	LDA $B6,x
	CMP XMAX,y
	BEQ RETURN1
	LDA $B6,x
	CLC
	ADC XACCEL,y
	STA $B6,x
RETURN1	RTS
STATE2	LDA $1510,x
	CMP #$04
	BEQ EXPLODE
	JSL $81A7DC
	JSL SubOffscreenX0
	LDA #$02
	STA $1602,x
	LDA $1588,x
	AND #$04
	BEQ RETURN2
EXPLODE	LDA #$09
	STA $1DFC
	LDA #$0D
	STA $9E,x
	JSL $87F7D2
	LDA #$08
	STA $14C8,x
	LDA #$01
	STA $1534,x
	LDA #$40
	STA $1540,x
	LDA #$1B
	STA $167A,x
RETURN2	RTS
STATE3
	JSL SubOffscreenX0
	LDA #$05
	STA $1602,x
	JSL $81A7DC
	STZ $1656,x
	LDA $1588,x
	AND #$04
	BEQ nocon
	LDA $AA,x
	CMP #$08
	BCS NOGND2
	STZ $AA,x
	BRA stillgnd
NOGND2	LDA $AA,x
	LSR A
	EOR #$FF
	INC A
	STA $AA,x
	LDA #$01
	STA $1DF9
stillgnd
	LDA $B6,x
	BEQ nocon
	BMI decspd
	LDA $B6,x
	SEC
	SBC #$04
	STA $B6,x
decspd	INC $B6,x
	INC $B6,x
nocon	RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; GRAPHICS ROUTINE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TILEMAP	dcb $C8,$CA,$88
	dcb $AC,$AE,$CC
SUB_GFX	    JSL GetDrawInfo      ; sets y = OAM offset
LDA #$F0
STA $0309,y
		    LDA $1602,x
		    STA $03		 ;  | $03 = index to frame start (0 or 1)
		    LDA $157C,x             ; $02 = sprite direction
		    STA $02
		    PHX		     ; /
		    LDA #$02
		    CMP $14C8,x
		    BNE NOTDEAD
		    LDA $15F6,x
		    ORA #$80
		    STA $15F6,x
 NOTDEAD                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
; no, that's not why we increment 4 times, and no, you don't need to increment at all when drawing just 1 tile
; I blame mikeyk for this misconception
		    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


NEWSTUFF
	JSR SUB_GFX2
	LDA $C2,x
	JSL $8086DF
	dcw INTRO0&$FFFF
	dcw INTRO1&$FFFF
INTRO0	LDA $1540,x
	BEQ NORM0
	CMP #$01
	BEQ INCST
	JSL $81802A
	LDA #$F8
	STA $AA,x
	LDA $1540,x
	CMP #$D8
	BCC RETURNI0
NORM0	LDA $94
	CMP #$98
	BCC RETURNI0
	LDA $95
	BNE RETURNI0
	STZ $7B
	LDA $94
	SEC
	SBC #$01
	STA $94
	LDA $95
	SBC #$00
	STA $95
	LDA $1540,x
	BNE RETURNI0
	LDA #$F0
	STA $1540,x
	LDA #$0F
	STA $1DFC
RETURNI0
	RTL
INCST	LDA #$01
	STA $C2,x
INTRO1	RTL


TILEMAP2	dcb $80,$82,$84,$86
		dcb $A0,$C2,$C4,$A6
		dcb $C0,$C2,$C4,$C6
		dcb $C0,$C2,$C4,$C6
		dcb $C0,$C2,$C4,$C6
		dcb $C0,$C2,$C4,$C6
		dcb $C0,$C2,$C4,$C6
		dcb $C0,$C2,$C4,$C6
		dcb $D0,$D2,$D4,$D6

X_OFFSET2	dcb $00,$10,$20,$30
		dcb $00,$10,$20,$30
		dcb $00,$10,$20,$30
		dcb $00,$10,$20,$30
		dcb $00,$10,$20,$30
		dcb $00,$10,$20,$30
		dcb $00,$10,$20,$30
		dcb $00,$10,$20,$30
		dcb $00,$10,$20,$30

Y_OFFSET2	dcb $00,$00,$00,$00
		dcb $10,$10,$10,$10
		dcb $20,$20,$20,$20
		dcb $30,$30,$30,$30
		dcb $40,$40,$40,$40
		dcb $50,$50,$50,$50
		dcb $60,$60,$60,$60
		dcb $70,$70,$70,$70
		dcb $78,$78,$78,$78

SUB_GFX2
	JSL GetDrawInfo       ; sets y = OAM offset

	PHX
	LDX #$23
LOOP_START2          PHX
	LDA X_OFFSET2,x
	CLC
	ADC $00                 ; \ tile x position = sprite y location ($01)
	STA $0300,y             ; /

	LDA Y_OFFSET2,x
	CLC
	ADC $01                 ; \ tile y position = sprite x location ($00)
	DEC : DEC
	STA $0301,y             ; /

	LDA TILEMAP2,x
	STA $0302,y 

	PHX
	LDX $15E9
	LDA #$03
	PLX
	ORA $64                 ; add in tile priority of level
	STA $0303,y             ; store tile properties
	PLX

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

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

