;Lensflare Code ist nicht von mir, habe ihn nur leicht geändert

Function lensflares()
	PositionEntity lens_sun, camera_x#-100,camera_y#+100,camera_z#+100

	Local lx,ly
	Local sx,sy
	Local vx,vy
	Local nx#,ny#
	Local cx#,cy#
	Local distance#

	PointEntity sun_Light,lens_sun,pivot
	RotateEntity sun_light,0,90,0

	If EntityInView(lens_sun, camera) And EntityVisible(pivot, lens_sun) And gt_dn=1
		CameraProject camera, EntityX(lens_sun,True), EntityY(lens_sun,True), EntityZ(lens_sun,True)
		
		;position on 2d screen
	   	lx = ProjectedX()
	   	ly = ProjectedY()-(opt_sx/2)
	
		;determine center of screen
		sx = opt_sx Shr 1
		sy = opt_sy Shr 1

		;determine vector from center to sun's x/y screen position
		vx = sx - lx
		vy = sy - ly
		
		If vx=0 And vy=0
			distance#=1.0
		Else
			distance#=Sqr((vx*vx)+(vy*vy))	;determine length of vector (pythagoras)
		EndIf

		;normalise vector (determine 1 unit size in x and y coordinates)
		nx#=vx/distance#
		ny#=vy/distance#

		;calculate lens flare positions		
		For i=0 To 5
			cx#=sx - (nx# * (distance#/lens_dist#(i)))
			cy#=sy - (ny# * (distance#/lens_dist#(i)))	
			ShowEntity lens_flare(i)
			PositionEntity lens_flare(i),cx#,cy#,1
		Next
		
	Else
		For i=0 To 5
			HideEntity lens_flare(i)
		Next
	EndIf

End Function