;***********************
;*** Fire circular laser ***
;***********************
; BC = position ; A = direction
;
.CIRC_Fire
	ex AF,AF'
	ld A,(CIRC_State)
	or A
	ret nz
	
	ex AF,AF'
	
	ld HL,SPNS_CallBack
	
	cp 1
	jr z,CIRC_FireForward
	ld HL,SPMS_CallBack
	
.CIRC_FireForward	
	ld (CIRC_DrawDirection),HL	
	
	ld (CIRC_Pos),BC
	ld A,A
	ld (CIRC_State),A
	ld A,#FF
	ld (CIRC_Slot),A
	ld A,130
	ld (CIRC_Spr),A
	
	ld A,sfx_circular
	call SND_SfxPlay
	
	ret

.CIRC_Update
	ld A,(CIRC_State)
	or A
	ret z
	
;move X and check if on screen
	ld BC,(CIRC_Pos) ;x,y reverse C = Y , B = Y
	add C
	cp 32
	jr nc,CIRC_Kill

	ld C,A	
	ld (CIRC_Pos),BC
	
;check if we encountered something
	push BC
	inc B	;center of the wave shot
	inc B
	call COLL_CheckPosition
	pop BC
	jr nc,CIRC_NotSolid
	or A
	jr nz,CIRC_NotSolid	
	
.CIRC_Kill
	xor A
	ld (CIRC_State),A
	ret
	
.CIRC_NotSolid	
	ld A,(CIRC_Slot)
	inc A
	and 7	;max 8 slots
	ld (CIRC_Slot),A	
;compute slot
	add A
	add A	;each slot is 4 byte
	add CircularBuffer_Low
	ld L,A
	ld H,CircularBuffer_High
;compute current sprite
	ld A,(CIRC_Spr)
	inc A
	cp 141
	jr c,CIRC_RunValidSpr
	ld A,135
.CIRC_RunValidSpr
	ld (CIRC_Spr),A	
;write to slot
	ld (HL),4	;circular width
	inc L
	ld (HL),A	;spr
	inc L
	ld (HL),C	;X
	inc L
	ld (HL),B	;Y
	
	ret

;*********************************
;*** circular segments management ***
;*********************************
.CIRC_DrawCallback
	bit 6,(HL)
	jr nz,CIRC_DrawCallbackKillEgg
	push AF
	push HL
	inc H
	inc H
	inc H
	inc H
	
	ld A,(HL)
	or A
	jr z,CIRC_DrawCallbackDone		;not solid wall
	cp 32
	jr nc,CIRC_DrawCallbackDone		;not enemy
	push BC
	ld B,1
	call THEM_MakeDamage
	pop BC	
.CIRC_DrawCallbackDone	
	pop HL
	pop AF
	ret
.CIRC_DrawCallbackKillEgg
	xor A
	ret

.CIRC_Draw
	LD A,(BALLPOW)	;check if we use weapon 2
	cp 2
	ret nz
	
	SetMaster NeutralMaster
	SetCallback CIRC_DrawCallback
	
	ld B,8	;eight slots
	ld C,4
	ld HL,CircularBuffer
	
.CIRC_DrawLoop
	ld A,(HL)
	or A
	jr z,CIRC_DrawEmpty
	dec A
	ld (HL),A
	
	push BC
	inc L
	ld A,(HL) ;get spr
	inc L
	ld C,(HL)
	inc L
	ld B,(HL)
	inc L
	push HL
	call USPGADD
CIRC_DrawDirection	EQU $+1
	call SPNS
	pop HL
	pop BC
	djnz CIRC_DrawLoop
	jr CIRC_DrawDone
	
.CIRC_DrawEmpty
	ld A,C
	add L
	ld L,A
	djnz CIRC_DrawLoop
.CIRC_DrawDone
	ret

	
	

CIRC_State	db 0	;direction 1 or -1
CIRC_Slot	db 0
CIRC_Spr	db 0	;current sprite (131-140 cycle 135-140)
CIRC_Pos	dw 0	;position x,y