; coco main
	opt	forwardrefmax
#include	coco\main_lib.txt
#memstart	28672	;$7000

	org	$3000	;mem start?



start


; no hispeed poke nessesary
;	clra
;	ldx	65497
;	sta	,x


	createsinetable()
	std	sinetable
;map start
	lda	#1
	sta	pposx
	lda	#30
	sta	pposy
	lda	#64
	sta	pangle

	lda	#32	;32x2=64....  on a 256 byte sine table 64=90deg
	sta	fov	;field of view
	clr	page

	sg4_setdisplay()	;semi graphics 4


begin_loop

	cls_ray(page,#6,#1,#0)	;prep screen with horizon blank


; check key press
; use arrow keys on this example

	keystring(#0)
	stb	presskey


	if	b,presskey=#10	;left
	lda	pangle
	suba	#16
	sta	pangle

	endif

	if	b,presskey=#93	;right
	lda	pangle
	adda	#16
	sta	pangle

	endif



; move forward
	if	b,presskey=#95	or	b,presskey=#91

	lda	#3
	sta	diam

	if	b,presskey=#91
	lda	pangle
	adda	#128
	sta	pangle
	endif

	plot(sinetable,pposx,pposy,diam,diam,pangle)
	sta	dotx
	stb	doty

	if	b,presskey=#91
	lda	pangle
	suba	#128
	sta	pangle
	endif

;check out of bounds
	if	b,dotx>#127
	clr	dotx
	endif
	if	b,dotx>#48
	lda	#48
	sta	dotx
	endif
	if	b,doty>#127
	clr	doty
	endif
	if	b,doty>#48
	lda	#23
	sta	doty
	endif
;smooth map movement
	if	b,dotx>pposx
	lda	pposx
	adda	#1
	sta	dotx
	endif
	if	b,dotx<pposx
	lda	pposx
	suba	#1
	sta	dotx
	endif
	if	b,doty>pposy
	lda	pposy
	adda	#1
	sta	doty
	endif
	if	b,doty<pposy
	lda	pposy
	suba	#1
	sta	doty
	endif
;move x first then y sliding collision
	ldx	#worldmap
	lda	#48
	ldb	pposy
	mul
	leax	d,x
	ldb	dotx
	abx
	lda	0,x
	sta	matrix
;update x smooth
	if	b,matrix=#0
	lda	dotx
	sta	pposx
	endif
	ldx	#worldmap
	lda	#48
	ldb	doty
	mul
	leax	d,x
	ldb	pposx
	abx
	lda	0,x
	sta	matrix
; update y smooth
	if	b,matrix=#0
	lda	doty
	sta	pposy
	endif

	endif
; end of movement











; draw frame

; reset loops
; for drawing
	clr	screenx


	lda	pangle
	suba	fov	;fov is based on a 256 byte sine table 32x2=64.... 64=90 deg
	sta	dangle

main_draw

	lda	#2	;start with 2 and grow out
	sta	diam

main_draw2

	plot(sinetable,pposx,pposy,diam,diam,dangle)
	sta	dotx
	stb	doty

;check out of bounds
	if	b,dotx>#127	;negative
	clr	dotx
	endif
	if	b,dotx>#48
	lda	#48
	sta	dotx
	endif

	if	b,doty>#127	;negative
	clr	doty
	endif
	if	b,doty>#48
	lda	#23
	sta	doty
	endif

	ldx	#worldmap
	lda	#48
	ldb	doty
	mul
	leax	d,x
	ldb	dotx
	abx
	lda	0,x
	sta	matrix

	lbne	found_dist

	inc	diam
	lda	diam
	cmpa	#12
	lblo	main_draw2
	lbra	not_found	;skip

	lda	#9
	sta	matrix

found_dist

	if	b,matrix=#9
	clr	matrix
	endif

; fix scaling
	ldy	#scaling
	lda	#2
	ldb	diam
	mul
	leay	d,y
	ldd	0,y
	sta	diam
	stb	diam2	



found_drawlp

;fix fisheye
	ldx	#fisheye
	ldb	screenx
	abx
	lda	diam
	suba	0,x
	sta	diam
	lda	diam2
	adda	0,x
	sta	diam2




	if	b,diam>#127
	clr	diam
	endif
	if	b,diam2>#29
	lda	#29
	sta	diam2
	endif


	fast_hline2(page,screenx,diam,diam2,matrix)	;draw horizontal line


not_found


	lda	dangle
	adda	#2	; jump 2 as screen is 32 chars not 64
	sta	dangle
	lda	screenx
	adda	#2	;jump 2 with angle
	sta	screenx

	cmpa	#64	;we done?
	lblo	main_draw

; print map pos info

	bytestring(pposx,#myword)
	print(page,#1,#0,#myword)
	bytestring(pposy,#myword)
	print(page,#1,#5,#myword)


	sg4_nosync(page)	;update to graphic page.... odly im using 3 but will change that on anything built with this

; double buffer.... using 3 pages defined
	inc	page
	if	b,page>#2
	clr	page
	endif

	lbra	begin_loop	;big loop



; end of program
cocoromend
	end	start










