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

local ourEntity = GetUpdatedEntityID()

local ourProjectileComponent = EntityGetFirstComponentIncludingDisabled(ourEntity, "ProjectileComponent")
local player = ComponentGetValue2( ourProjectileComponent, "mWhoShot" ) or 0

local controlsComponent = EntityGetFirstComponent(player, "ControlsComponent")

local mouseX, mouseY = ComponentGetValue2(controlsComponent, "mMousePosition")
local x, y = EntityGetTransform(ourEntity)

SetRandomSeed(x + mouseX + GameGetFrameNum(), y + mouseY + GameGetFrameNum())

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

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



local Speed = 100

local ourVelocityComponent = EntityGetComponent(ourEntity, "VelocityComponent")

if(ourVelocityComponent ~= nil)then
	
	edit_component( ourEntity, "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.96) + (dir_x * Speed)
		local force_y = (velocityY*0.96) + (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

