;*********************************************************************
;	mario kart	
;*********************************************************************
	INCLUDE	../../SFX/RP5C77
		INCLUDE	../../SFX/RP5A22
		INCLUDE	../../DSP/D77C25
		INCLUDE	../../../kimura/join/work.def
		INCLUDE	../../../kimura/join/buffer.def
		INCLUDE	../DEF/object.def
;----------------------------------------------------------------------
	glb	open_shot
;----------------------------------------------------------------------
	COMN
shot_time	ds	2
target_x	ds	2
target_y	ds	2
dist_x		ds	2
dist_y		ds	2
;----------------------------------------------------------------------
bank84	sect	rel
	extend
	mem16
	idx16
;*********************************************************************
;	バナナミサイル攻撃システム
;*********************************************************************
;
;	x = player register
;
;
;- - - - - - - - - - - - - - - - - - - - 
msp	equ	0600h
;- - - - - - - - - - - - - - - - - - - - 
Xoffset		dw	6,6,-6,-6
Yoffset		dw	-6,6,-6,6
vector_dataY	dw	-msp,msp,-msp,msp
vector_dataX	dw	-msp,-msp,msp,msp
;- - - - - - - - - - - - - - - - - - - - 
open_shot
		PHB
		PHK
		PLB
;- - - - - - - - - - - - - - - - - - - -  ; どの場所にアイテムをほおるか？


;- - - - - - - - - - - - - - - - - - - - ; エリアをめざして投げる！ - - -



		ldy	<_fire_register,x	; Y = player register
;- - - - - - - - - - - - - - - - - - - - 
		lda	!_lap,y
		and	#00ffh
		clc
		adc	#4			; プレイヤーの４エリア先 !
		cmp	!area_number
		bcc	sk$		; sec
		sbc	!area_number
sk$		asl	a
		tay

		phx				;>>
		lda	<frame_counter
		and	#06h
		tax
		lda	!target_bufferX,y
		clc
		adc	!Xoffset,x
		sta	<target_x
		lda	!target_bufferY,y
		clc
		adc	!Yoffset,x
		sta	<target_y

		plx				;<<
;- - - - - - - - - - - - - - - - - - - - 



;- - - - - - - - - - - - - - - - - - - - 
		ldy	#0			; y = flag
		lda	<_situationXH,x
		sec
		sbc	<target_x
		bpl	skip$
		ldy	#4
		EOR	#1111111111111111B
		INC	A
skip$		sta	<dist_x
		lda	<_situationYH,x
		sec
		sbc	<target_y
		bpl	skip1$
		iny
		iny
		EOR	#1111111111111111B
		INC	A
skip1$		sta	<dist_y
		cmp	<dist_x
		bcs	skip_x$
;- - - - - - - - - - - - - - - - - - -  ; X の絶対値の方が大きい時
		lda	<dist_x
		jsr	Div
		sta	<shot_time	;到達時間
		lda	<dist_y
		jsr	calc_vector
		cpy	#2
		beq	skip_y1$
		cpy	#6
		beq	skip_y1$
		EOR	#1111111111111111B
		INC	A
skip_y1$	sta	<_velocityY,x
		lda	!vector_dataX,y
		sta	<_velocityX,x
		bra	skip_z$
;- - - - - - - - - - - - - - - - - - - - ; Y の絶対値の方が大きい時
skip_x$		jsr	Div
		sta	<shot_time	;到達時間
		lda	<dist_x
		jsr	calc_vector
		cpy	#4	
		bcs	skip_x1$
		EOR	#1111111111111111B
		INC	A		
skip_x1$	sta	<_velocityX,x
		lda	!vector_dataY,y
		sta	<_velocityY,x
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
skip_z$					; Z=0 の時の初速度
		lda	<shot_time
		and	#00ffh		; 1/2 * g * t
		xba
		lsr	a		; 1/2

		lsr	a		; 重力
		lsr	a
		lsr	a
		lsr	a

		lsr	a

		sta	<_velocityZ,x

		plb
		rtl
;----------------------------------------------------------------------
;	別軸のベクトルを計算する
;----------------------------------------------------------------------
;	入力　A 	= 距離データの絶対値 16bit
;	      shot_time	= 到達時間	      8bit	
;	出力  A		= 別軸のベクトル値   16bit
;- - - - - - - - - - - - - - - - - - -  - - - - - - - - - 
calc_vector	asl	a
		asl	a
		asl	a
		asl	a
		sta	!4204h			; **** **** ****.****
		SEP	#00100000B
		MEM8
		lda	<shot_time		; **** ****
		sta	!4206h
		REP	#00100000B
		MEM16
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		lda	!4214h
		asl	a
		asl	a
		asl	a
		asl	a			; **** ****.**** ****
		rts
;----------------------------------------------------------------------
Div		sta	!4204h
		SEP	#00100000B
		MEM8
		lda	#msp/100h
		sta	!4206h
		REP	#00100000B
		MEM16
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		lda	!4214h
		rts
;----------------------------------------------------------------------
		end
