dofile( "data/scripts/lib/utilities.lua" )

local theEntity = GetUpdatedEntityID()
local projX, projY = EntityGetTransform(theEntity)
local entities = EntityGetInRadius( projX, projY, 50 )

SetRandomSeed(projX + GameGetFrameNum(), projY + GameGetFrameNum())


for k, v in ipairs(entities)do
	
	if(v ~= theEntity) then
	
		local x, y = EntityGetTransform(v)

		local directionX = (projX + Random(-5, 5)) - x

		local directionY = (projY + Random(-5, 5)) - y

		local Speed = 100

		local ourVelocityComponent = EntityGetFirstComponent(v, "VelocityComponent")
		local ourProjectileComponent = EntityGetFirstComponent(v, "ProjectileComponent")

		
		
		if(ourProjectileComponent ~= nil and EntityHasTag(v, "orbit_bolt") == false)then
			local theHerd = ComponentGetValue(ourProjectileComponent, "mShooterHerdId")
				
			if(tonumber(theHerd) == 0)then
				if(tonumber(GlobalsGetValue( "projectileEntity"..v, "0" )) == 0)then
					ComponentSetValueVector2(ourVelocityComponent, "mVelocity", 1, 1)
					
					GlobalsSetValue( "projectileEntity"..v, 1 )
				end
				
				local selfProjectileComponent = EntityGetFirstComponent(theEntity, "ProjectileComponent")
				local originalLifetime = ComponentGetValue(selfProjectileComponent, "lifetime")

				ComponentSetValue(ourProjectileComponent, "lifetime", originalLifetime + 60)
				
				edit_component( v, "VelocityComponent", function(comp,vars)
				
					local velocityX, velocityY = ComponentGetValueVector2( comp, "mVelocity")

					local vel_x = directionX

					local vel_y = directionY

					local len = math.sqrt((vel_x*vel_x) + (vel_y*vel_y))

					local dir_x = vel_x / len
					local dir_y = vel_y / len

					local force_x = (velocityX*0.98) + (dir_x * Speed)
					local force_y = (velocityY*0.98) + (dir_y * Speed)
					
					
					
					--shoot_projectile( player, "files/entities/physbeam.xml", playerX + (dir_x2 * 10), playerY + (dir_y2 * 10), dir_x2 * 20, dir_y2 * 20 )
					--[[ComponentSetValue( comp, "gravity_x", 0 )
					ComponentSetValue( comp, "gravity_y", 0 )]]
					ComponentSetValueVector2( comp, "mVelocity", force_x, force_y)
					--ComponentSetValueVector2( comp, "mVelocity", force_x, force_y)

				end)	
			end
		end
	end
end
