bullets = {}
rays = {}

--CreateSimpleBullet( 0, 0, 2, {1, 1, 1, 1}, -math.pi/2 )
--CreateSimpleBullet( 0, 0, {0, 2}, {1, 1, 1, 1} )
function CreateSimpleBullet( x, y, vel, color, angle, friendly, size )
	local spr = CreateSprite( "shot", x, y )
	SetObjSpriteColor( spr, color )
	local l_angle = angle
	local l_vel = vel
	if not angle then
		local l_vell = math.sqrt( vel[1]*vel[1] + vel[2]*vel[2] )
		--local v_vel = { vel[1]/l_vel, vel[2]/l_vel }
		l_angle = math.atan2( vel[2], vel[1] )
	else
		l_vel = { vel * math.cos(angle), -vel * math.sin(angle) }
		l_angle = -l_angle
	end
	SetObjSpriteAngle( spr, l_angle - math.pi/2 )
	local tab = 
			{
				x = x,
				y = y,
				sprite = spr,
				angle = l_angle,
				vel = l_vel,
				friendly = friendly or 0,
				size = size or 4,
				type = "normal"
			}
		
	table.insert( bullets, tab )
	CreateLightSource( tab, {color[1], color[2], color[3], -0.11}, 64 )
end

--CreateSimpleBullet( 0, 0, {0, 2}, {1, 1, 1, 1} )
function CreateSpriteBullet( x, y, sprite, frame, vel, color, angle, friendly, size )
	local spr = CreateSprite( sprite, x, y )
	SetObjAnim( spr, frame, false )
	local l_angle = angle
	local l_vel = vel
	if not angle then
		local l_vell = math.sqrt( vel[1]*vel[1] + vel[2]*vel[2] )
		--local v_vel = { vel[1]/l_vel, vel[2]/l_vel }
		l_angle = math.atan2( vel[2], vel[1] )
	else
		l_vel = { vel * math.cos(angle), -vel * math.sin(angle) }
		l_angle = -l_angle
	end
	SetObjSpriteAngle( spr, l_angle - math.pi/2 )
	local tab = 
			{
				x = x,
				y = y,
				sprite = spr,
				angle = l_angle,
				vel = l_vel,
				friendly = friendly or 0,
				size = size or 4,
				type = "normal"
			}
		
	table.insert( bullets, tab )
	CreateLightSource( tab, {color[1], color[2], color[3], -0.11}, 64 )
end

function CreateSineBullet( x, y, vel, color, angle, friendly, size )
	local spr = CreateSprite( "shot", x, y )
	SetObjSpriteColor( spr, color )
	local l_angle = angle
	local l_vel = vel
	local normal
	if not angle then
		local l_vell = math.sqrt( vel[1]*vel[1] + vel[2]*vel[2] )
		--local v_vel = { vel[1]/l_vel, vel[2]/l_vel }
		normal = { vel[2]/l_vell, vel[1]/l_vell }
		l_angle = math.atan2( vel[2], vel[1] )
	else
		l_vel = { vel * math.cos(angle), -vel * math.sin(angle) }
		normal = { math.sin( angle ), math.cos(angle) }
		l_angle = -l_angle
	end
	SetObjSpriteAngle( spr, l_angle - math.pi/2 )
	local tab = 
			{
				x = x,
				y = y,
				sprite = spr,
				angle = l_angle,
				vel = l_vel,
				friendly = friendly or 0,
				size = size or 4,
				normal = normal,
				param = 0,
				amp = 3,
				type = "sine"
			}
		
	table.insert( bullets, tab )
	CreateLightSource( tab, {color[1], color[2], color[3], -0.11}, 64 )
end

useless_widgets = {}

function ProcessBullets()
	for key, value in pairs( bullets ) do
		value.x = value.x + value.vel[1]
		value.y = value.y + value.vel[2]
		if value.type == "sine" then
			value.x = value.x + value.amp * math.sin( value.param ) * value.normal[1]
			value.y = value.y + value.amp * math.sin( value.param ) * value.normal[2]
			value.param = value.param + 0.1
		end
		if math.abs( value.x ) > 300 or math.abs( value.y ) > 400 then
			delete( bullets, key )
		else
			SetObjPos( value.sprite, value.x, value.y )
			if value.friendly > 0 then
				for k, v in pairs( enemies ) do
					if not v.dead and not value.dead and intersect( value, v ) then
						EnemyHit( k, players[value.friendly] )
						delete( bullets, key, true )
					end
				end
			else
				for i=1, players_num do
				     	if intersect( value, players[i] ) and
					    players[i].mortal and players[i].sprite then
						PlayerHit( i, value )
						delete( bullets, key, true )
					end
				end
			end
		end
	end
	ProcessRays()
end

function CreateSimpleRay( x, y, color )
	local ray = {}
	ray.x = x
	ray.y = y
	local sprites = {}
	local spr = CreateSprite( "ray1", x, y )
	SetObjPos( spr, x, y )
	SetObjAnim( spr, 8, false )
	SetObjSpriteColor( spr, color )
	local raycolor = { color[1], color[2], color[3], -0.25 }
	CreateRayLight( x, y, ray, raycolor, 40 )
	table.insert( sprites, spr )
	for j=y+32,416,32 do
		CreateRayLight( x, j, ray, raycolor, 40 )
		spr = CreateSprite( "ray1", x, j )
		SetObjPos( spr, x, j )
		SetObjAnim( spr, 0, false )
		SetObjSpriteColor( spr, color )
		table.insert( sprites, spr )
	end
	ray.color = raycolor
	ray.time = 0
	ray.sprites = sprites
	ray.num = 0
	table.insert( rays, ray )
end

function CreateAttachedRay( obj, color )
	local x,y = obj.x, obj.y
	local ray = { type = "attached", obj=obj }
	ray.x = x
	ray.y = y
	local sprites = {}
	local spr = CreateSprite( "ray1", x, y )
	SetObjPos( spr, x, y )
	SetObjAnim( spr, 8, false )
	SetObjSpriteColor( spr, color )
	local raycolor = { color[1], color[2], color[3], -0.25 }
	local tsp = { sprite = spr, x = x, y = y }
	CreateLightSource( tcp, raycolor, 40 )
	table.insert( sprites, {spr} )
	for j=y+32,416,32 do
		spr = CreateSprite( "ray1", x, j )
		SetObjPos( spr, x, j )
		SetObjAnim( spr, 0, false )
		SetObjSpriteColor( spr, color )
		tsp = { sprite=spr, x = x, y = j }
		CreateLightSource( tsp, raycolor, 40 )
		table.insert( sprites, tsp )
	end
	ray.color = raycolor
	ray.time = 0
	ray.sprites = sprites
	ray.num = 0
	table.insert( rays, ray )
end

function ProcessRays()
	for k, v in pairs( rays ) do
		v.time = v.time + 1
		if v.time > 50 then
			local player
			for i=1,players_num do
				player = players[i]
				if player.sprite and player.mortal and player.y > v.y and math.abs( player.x - v.x ) < 5 + player.size then
					PlayerHit( i )
				end
			end
			if (v.time - 50) % 5 == 0 then
				v.num = v.num + 1
				if v.num == 8 then
					for i=1,#v.sprites do
						SetObjDead(v.sprites[i])
					end
					delete( rays, k )
				else
					for i=2,#v.sprites do
						SetObjAnim(v.sprites[i], v.num, false)
					end
					SetObjAnim(v.sprites[1], v.num+8, false)
				end
			end
		elseif v.time == 50 then
			v.num = v.num + 1
			for i=2,#v.sprites do
				SetObjAnim(v.sprites[i], v.num, false)
			end
			SetObjAnim(v.sprites[1], v.num+8, false)
		else
			for key, value in pairs( v.lightsources or {} ) do
				SetObjSpriteColor(value.sprite, {value.color[1], value.color[2], value.color[3], -0.01*v.time/2})
			end
		end
	end
end

function ResetBullets()
	bullets = {}
	rays = {}
end
