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

incsrc subroutinedefs_trasm.asm

DIR = $03
PROPRAM = $04
RAM_TargetsBroken = $7F9A90
EXTRA_BITS 		= $7FAB10

dcb "INIT"

	LDA EXTRA_BITS,x		; \ 
	AND #$04		;  |
	BNE NoCircle
	LDA #$1F
	STA $B6,x
NoCircle:

	RTL      

dcb "MAIN"
	PHB
	PHK
	PLB		
	JSR Run
	PLB
	RTL

XSpeedtable:		db $78,$88	;X speed table: Right, left
YSpeedtable:		db $20,$E0	;Y speed acceleration table: down, up (?)

XAcceleration:		dcb $08,$F8
YAcceleration:		dcb $01,$FF	;Y Acceleration stuff

RETURN:
	RTS

Run:
	JSR GFX

Skip:
	LDA $14C8,x	; \ 
	CMP #$08	;  | if status != 8, return
	BNE RETURN	; /
	LDA $9D		; \ if sprites locked, return
	BNE RETURN	; /



		LDA $C2,x		;Load... something
		AND #$01		;AND #$01
		TAY			;Use as index for acceleration table

		LDA EXTRA_BITS,x		; \ 
		AND #$04		;  |Become wavy if extra bit is set
	
		BEQ Vertical		; / (This was originally a sprite number check)

		LDA $B6,x
		CLC
		ADC XAcceleration,y
		STA $B6,x
		CMP XSpeedtable,y		;If it didn't reach the desired X speed
		BNE Update		;Continue processing the sprite
		INC $C2,x		;If it reached the desired speed, change Y direction
		BRA Update
Vertical:
		LDA $AA,x		;\
		CLC			; |Load Y speed
		ADC YAcceleration,y	; |Accelerate it
		STA $AA,x		;/
		CMP YSpeedtable,y		;If it didn't reach the desired Y speed
		BNE SkipperFish		;Continue processing the sprite
		INC $C2,x		;If it reached the desired speed, change Y direction
SkipperFish:		
		LDA $1626,x
		AND #$01
		TAY
		LDA $B6,x
		CLC
		ADC YAcceleration,y
		STA $B6,x
		CMP YSpeedtable,y
		BNE Update
		INC $1626,x


Update:


	JSL $81801A	;update ypos no gravity
	JSL $818022	;update xpos no gravity
	JSL $818032	;interact with other sprites
	JSR SUB_THROWN_SPRITE
RTS
	
;;;;;;;;;;;;;;;;;;;;;;;
;GFX routine
;;;;;;;;;;;;;;;;;;;;;;;

TILEMAP:	dcb $CC

YDISP:	dcb $00

GFX:
	JSL SubOffscreenX0
	JSL GetDrawInfo

	LDA $157C,x	;direction...
	STA DIR

	LDA $15F6,x	;properties...
	STA PROPRAM

;	LDX #$00

OAM_Loop:

	LDA $00
	STA $0300,y	;xpos

	LDA $01
;	CLC
;	ADC YDISP,x
	STA $0301,y	;ypos
; a displacement table with only one value in it, and that value is 00...REALLY?!

	PHX
	LDX $15E9
	LDA $1540,x	;general purpose timer
	PLX
	AND #$10
	BNE Normal

	INX
	INX
	LDA #$CC
	STA $302,y
;	DEX
;	DEX
	BRA Dunn



Normal:
	LDA #$CC
	STA $0302,y	;chr

Dunn:
	;LDA PROPRAM
	;ORA $64
	LDA #$09
	STA $0303,y	;prop

;	INY
;	INY
;	INY
;	INY
;	INX

;	CPX #$01
;	BNE OAM_Loop

	LDX $15E9	;restore sprite index
	LDY #$02	;16x16
	LDA #$00	;1 tile
	JSL $81B7B3	;reserve

	RTS



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ROUTINES FROM THE LIBRARY ARE PASTED BELOW
; You should never have to modify this code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SUB_THROWN_SPRITE:

SPRITE_INTERACT:   
	LDY #$0B
LOOP:
	LDA $14C8,y		; \ if the sprite status is..
	CMP #$09		;  | ...shell-like
	BEQ PROCESS_SPRITE	; /
	CMP #$0A		; \ ...throwned shell-like
	BEQ PROCESS_SPRITE	; /

NEXT_SPRITE:	    
	DEY		;
	BPL LOOP	; ...otherwise, loop
NoBreak:
	RTS		; return

PROCESS_SPRITE:
	LDA $9E,y
	CMP #$80
	BEQ NoBreak	    
	PHX		; push x
	TYX		; transfer x to y
	JSL $83B6E5	; get sprite clipping B routine
	PLX		; pull x
	JSL $83B69F	; get sprite clipping A routine
	JSL $83B72B	; check for contact routine
	BCC NEXT_SPRITE

	LDA #$03	; \ set sprite state
	STA $C2,x	; /
	LDA #$40	; \ set timer
	STA $1540,x	; /

	PHX                     ; push x
	TYX                     ; transfer x to y

	STZ $14C8,x             ; destroy the sprite

BLOCK_SETUP:	    
	LDA $E4,x	; \ setup block properties
	STA $9A		;  |
	LDA $14E0,x	;  |
	STA $9B		;  |
	LDA $D8,x	;  |
	STA $98		;  |
	LDA $14D4,x	;  |
	STA $99		; /

EXPLODING_BLOCK:	    
	
	PHB		; \ set the exploding block routine
	LDA #$02	;  |
	PHA		;  |
	PLB		;  |
	LDA #$FF		;  | $FF = set flashing palette
	JSL $828663	;  |
	PLB		; /
	PLX		; pull sprite index
	LDA RAM_TargetsBroken
	INC	;do stuff here
	STA RAM_TargetsBroken
	LDA #$04	;destroy
	STA $14C8,x
	LDA #$29	; \ sound effect
	STA $1DFC 	; /
REP #$10
LDA $7FA230
CLC
ADC #$02
STA $7FA230
SEP #$10
	RTS
