

--OUTPUTPARSED



function KeyHappen( inVal, inDown )	--also registers mouse clicks
	if ( inVal == bx_KeyFire0 ) then
		gMouse = 1 - gMouse
	end
	if ( inDown == 1 ) then
		if ( inVal == bx_KeyFire0 ) then
			if ( #gParticleList < gMaxPart ) then
				r = .3 * 3
				for i = 1, gNumPartAdd do
					pct = i / gNumPartAdd
					xspd = r * sin( PI2 * pct )
					yspd = r * cos( PI2 * pct )
					insert( gParticleList, { gMouseX, gMouseY, xspd, yspd, .03, 0 } )
				end
			end
			
			
			--	x, y, spdx, spdy, life
			--insert( gParticleList, { gMouseX, gMouseY, gSpd[ 1 ], gSpd[ 2 ], 1 } )
			
		end
		--print ( inVal, inDown, chVal )
	end
	
end



function Init()
	require( "lib/bxeGeneral" )
	require( "lib/bxeMath" )
	
	gLineList = {}
	gParticleList = {}

	gMouseX = 0
	gMouseY = 0

	gSpd = { 0, 0 }
	gMouse = 0

	gNumPartAdd = 1200
	gMaxPart = 3000
	
	AddGeom( "geom/cube.obj" )
	AddGeom( "geom/quad.obj" )
	AddTex( "textures/dot.png" )
	SetSpaceMapper( bx_SPACEMAPPER2D )
	--AddSpaceObject( "line2d", 0, 0, .5, .5 )
	--insert( gLineList, { .5, 0, 1.5, 1 } )
	
	if false then
	insert( gLineList, { 1.5, 1, .5, 0 } )
	insert( gLineList, { 1, 0, 1, .2 } )
	insert( gLineList, { 1, 1, -1, .2 } )
	insert( gLineList, { -1, 1, 0, 1.2 } )
	insert( gLineList, { 0, 0, 1, 0 } )
	insert( gLineList, { 0, 0, 0, 2 } )
	end
	
	if true then
	insert( gLineList, { -1, -1, -1, 1 } )
	insert( gLineList, { -1, 1, 1, 1 } )
	insert( gLineList, { 1, 1, 1, -1 } )
	insert( gLineList, { 1, -1, -1, -1 } )
	--insert( gLineList, { 0, 0, 1, 0 } )
	--insert( gLineList, { 0, 0, 0, -1 } )
	end
	if not true then
	insert( gLineList, { 1.1, -1, -.81, -1 + .5 } )
	end
	if not true then
	insert( gLineList, { -.3, 0, .3, .1 } )
	end
	
	if false then
	insert( gLineList, { -1 + 1.7, -1.1, -1, 1.1 } )
	--insert( gLineList, { -1.1, 1, 1.1, 1 } )
	insert( gLineList, { 1.1, 1, -1.1, 1 } )
	insert( gLineList, { 1, 1.1, 1, -1.1 } )
	insert( gLineList, { 1.1, -1, -1.1, -1 } )
	end
	
	if false then
	insert( gLineList, { 1.1, 1, -1.1, .6 } )
	insert( gLineList, { 1.1, .8, -1.1, .4 } )
	
	end
	
	
	if false then
	
	--insert( gLineList, { 1, 1, 1.3, 1 } )
	--insert( gLineList, { 1.3, 1.3, 1.3, 1 } )
	--insert( gLineList, { 1.3, 1.3, 1, 1.3 } )
	--insert( gLineList, { 1, 1, 1, 1.3 } )
	
	last = { 0, 1 }
	num = 150.0
	for j = 0, 1 do
		r = .5 + .7 * j
		last[ 1 ] = 0
		last[ 2 ] = r
		
		direc = 1 - 2 * j
		for i = 1, num do
			pct = direc * i / num
			new = { r * sin( pct * PI2 ), r * cos( pct * PI2 ) }
			if ( i == 6 or true ) then
			insert( gLineList, { new[ 1 ], new[ 2 ], last[ 1 ], last[ 2 ] } )
			end
			--pct -= .03
			new = { r * sin( pct * 6.24 ), r * cos( pct * 6.24 ) }
			last[ 1 ] = new[ 1 ]
			last[ 2 ] = new[ 2 ]
		end
	end
	end
	
	--print( 'numLines do', #gLineList )
	for i, v in pairs( gLineList ) do
		AddSpaceObject( "line2d", v[ 1 ], v[ 2 ], v[ 3 ], v[ 4 ] )
	end
	
	
end


function Draw( t, dt )	--time and deltaTime ( time since last frame )
	gMouseX, gMouseY = GetMouseTotal()
	gMouseX = gMouseX * 1.3
	gMouseY = gMouseY * 1.3
	--gMouseY += .03 * t
	pos = { 1, 2 }
	r = 4
	spd = { r * sin( .2 * t ), r * cos( .2 * t ) }
	--spd = { -1 * r, 1 * r }
	
	gSpd[ 1 ] = spd[ 1 ]
	gSpd[ 2 ] = spd[ 2 ]
	
	
	if ( gMouse == 1 and #gParticleList < 2000 and false ) then
--	and #gParticleList == 0 ) then
		r = .0--.02 + .01 * sin( 4 * t )
		insert( gParticleList, { gMouseX, gMouseY, gSpd[ 1 ], gSpd[ 2 ], r } )
		--if ( #gParticleList % 100 == 0 ) then
		--	print( #gParticleList )
		--end
	end
	
	pos[ 1 ] = gMouseX
	pos[ 2 ] = gMouseY
	
	Push()
	Move( pos[ 1 ], pos[ 2 ], 0 )
	Scale( .01 )
	Geom( 'cube' )
	Pop()
	--collides, collision point, new point, new speed
	
	SetCam( 2, 2 )
	Scale( .57 )
	
	
	colPt = { 0, 0 }
	newPt = { 0, 0 }
	newSpd = { 0, 0 }
	if ( false ) then
	pos[ 1 ] = 0.43798828125 
	pos[ 2 ] = 0.78076171875
	pos[ 1 ] = -0.98598140478134
	pos[ 2 ] = 0.62073069810867
	end
	startPos = { pos[ 1 ], pos[ 2 ] }
	endPos = { pos[ 1 ] + spd[ 1 ], pos[ 2 ] + spd[ 2 ] }
	if ( false ) then
	endPos[ 1 ] = -1.0848122835159
	endPos[ 2 ] = 0.40276139974594
	end
	collides = 1
	maxCol = 11-- + int( 2 * t )
	Color( 0, 1, 0, 1 )
	--DrawLine( startPos[ 1 ], startPos[ 2 ], 0, endPos[ 1 ], endPos[ 2 ], 0 )
	ci = 0
	Push()
	Move( startPos[ 1 ], startPos[ 2 ] )
	Scale( .01 )
	Geom( 'quad' )
	Pop()
	numCol = 0
	while collides == 1 and maxCol > 0 do
		maxCol = maxCol - 1
		if ( ci == 0 ) then
			Color( 1, 0, 0 )
		elseif ( ci == 1 ) then
			Color( 0, 1, 0 )
		elseif ( ci == 2 ) then
			Color( 0, 0, 1 )
		end
		
		r = .1
		collides = MapLine( startPos, endPos, r, colPt, newPt )
		if ( collides == 0 ) then
			Color( 0, 0, .3 )
		end
			---
			--print( numCol, startPos[ 1 ], startPos[ 2 ] )
			--print( '\t\t', colPt[ 1 ], colPt[ 2 ] )
			d0 = colPt[ 1 ] - startPos[ 1 ]
			d1 = colPt[ 2 ] - startPos[ 2 ]
			--print( 'dist', sqrt( d0 * d0 + d1 * d1 ), 'collides', collides )
			---
		--DrawLine( newPt[ 1 ], newPt[ 2 ], 0, colPt[ 1 ], colPt[ 2 ], 0 )
		numCol = numCol + collides
		
		Color( .5, .5, .5 )
		if ( collides == 0 or true ) then
			DrawLine( startPos[ 1 ], startPos[ 2 ], 0, colPt[ 1 ], colPt[ 2 ], 0 )
		end
		ci = ( ci + 1 ) % 3
		for j = 1, 2 do
			startPos[ j ] = colPt[ j ]
			endPos[ j ] = newPt[ j ]
		end
		
	end
	--print 'lastCollides', collides, numCol
	--print 'SUPAH ', numCol
	
	
	Color( 1 )
	
	for i, v in pairs( gLineList ) do
		DrawLine( v[ 1 ], v[ 2 ], 0, v[ 3 ], v[ 4 ], 0 )
	end
	
	newPt = { 0, 0 }
	newSpd = { 0, 0 }
	colPt = { 0, 0 }
	Color( .3, .3, 1, 1 )
	Tex( 'dot' )
	Depth( 0, 0 )
	Blend( 1 )
	for i, v in pairs( gParticleList ) do
		
		pos = { v[ 1 ], v[ 2 ] }
		spd = { v[ 3 ], v[ 4 ] }
		r = v[ 5 ]--.05 + .04 * sin( .1 * i )
		
		v[ 6 ] = v[ 6 ] - dt
		collides = 0
			
		if ( v[ 6 ] <= .1 ) then
			--do real collision test
			collides = MapPoint( pos, spd, r, colPt, newPt, newSpd )
			
		end
		if ( v[ 6 ] <= 0 ) then
			--do line test
		end
		
		v[ 1 ] = newPt[ 1 ]
		v[ 2 ] = newPt[ 2 ]
		
		v[ 3 ] = newSpd[ 1 ]
		v[ 4 ] = newSpd[ 2 ]
		if ( collides > 0 and false ) then
			v[ 3 ] = v[3] + newSpd[ 1 ]
			v[ 4 ] = v[4] + newSpd[ 2 ]
		end
		if ( collides > 0 and false ) then
			v[ 3 ] = v[ 3 ] * .9
			v[ 4 ] = v[ 4 ] * .9
		end
		
		
		Push()
		Move( v[ 1 ], v[ 2 ], 0 )
		Scale( r )
		Geom( 'quad' )
		
		Pop()
		
	end
	Color( 1 )
	Tex( -1 )
	
end