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 mouse1Down = ComponentGetValue( controlsComponent, "mButtonDownFire")

GamePrint( tostring(mouse1Down))

local mouse2Down = ComponentGetValue( controlsComponent, "mButtonDownFire2")

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 + playerX + GameGetFrameNum(), y + playerY + GameGetFrameNum())


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

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

local Speed = 100

local ourVelocityComponent = EntityGetComponent(ourEntity, "VelocityComponent")

local ourProjectileComponent = EntityGetComponent(ourEntity, "ProjectileComponent")

if(ourProjectileComponent ~= nil)then
	ourProjectileComponent = ourProjectileComponent[1]
	local action_id = ComponentObjectGetValue2(ourProjectileComponent, "config", "action_id")
		local cellEaterComponent = EntityGetFirstComponent(ourEntity, "CellEaterComponent")
		if(GlobalsGetValue( tostring(ourEntity).."lifetime" ) == nil)then
			GlobalsSetValue( tostring(ourEntity).."lifetime", ComponentGetValue(ourProjectileComponent, "lifetime") )
		end
		if(cellEaterComponent ~= nil)then
			if(GlobalsGetValue( tostring(ourEntity).."radius" ) == nil)then
				GlobalsSetValue( tostring(ourEntity).."radius", ComponentGetValue(cellEaterComponent, "radius") )
			end
		end
		if(GlobalsGetValue( tostring(ourEntity).."collision" ) == nil)then
			GlobalsSetValue( tostring(ourEntity).."collision", ComponentGetValue(ourProjectileComponent, "collide_with_world"))
		end
		if(tonumber(mouse1Down) == 1 and EntityHasTag(ourEntity, "force_ignore") ~= true)then
			
			if(cellEaterComponent ~= nil)then
				ComponentSetValue(cellEaterComponent, "radius", 0)
			end

			if(ourVelocityComponent ~= nil)then
				ComponentSetValue(ourProjectileComponent, "lifetime", 600)
				ComponentSetValue(ourProjectileComponent, "collide_with_world", 0)
				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.50) + (dir_x * Speed)
					local force_y = (velocityY*0.50) + (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

			EntityAddTag(ourEntity, "force_armed")

		elseif(EntityHasTag(ourEntity, "force_armed") and EntityHasTag(ourEntity, "force_ignore") ~= true)then

			if(ourVelocityComponent ~= nil)then
				directionX = aimX

				directionY = aimY

				if(cellEaterComponent ~= nil)then
					ComponentSetValue(cellEaterComponent, "radius", 5 )
				end

				ComponentSetValue(ourProjectileComponent, "lifetime", GlobalsGetValue( tostring(ourEntity).."lifetime", 60 ))
				ComponentSetValue(ourProjectileComponent, "collide_with_world", GlobalsGetValue( tostring(ourEntity).."collision", 1))
				Speed = 1500

				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.50) + (dir_x * Speed)
					local force_y = (velocityY*0.50) + (dir_y * Speed)

					ComponentSetValueVector2( comp, "mVelocity", force_x, force_y )

				end)	
				EntityAddTag(ourEntity, "force_ignore")
			end
		else
			EntityAddTag(ourEntity, "force_ignore")
		end
end