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

local ourEntity = GetUpdatedEntityID()

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

local controlsComponent = EntityGetFirstComponent(player, "ControlsComponent")

local playerX, playerY = EntityGetTransform(player)

playerY = playerY - 4

local aimX, aimY = ComponentGetValueVector2	(controlsComponent, "mAimingVector")

local length = math.sqrt((aimX*aimX) + (aimY*aimY))

aimX = aimX / length

aimY = aimY / length


local distance = 18

local mouseX = playerX + (aimX * distance)

local mouseY = playerY + (aimY * distance)

local x, y = EntityGetTransform(ourEntity)

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


local aimdir = math.atan2(aimX, aimY)

EntitySetTransform(x, y, aimdir)

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

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

local Speed =600

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 = (dir_x * Speed)
		local force_y = (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
