-- @Context Server
-- Given a weapon definition, position, and direction. A throwable projectile will be simulated on all clients. 
-- @param WeaponDefinition weaponDefinition
-- @param Vector3 position
-- @param Vector3 direction
-- @param Player _owner

function ProjectileReplication.ReplicateThrowable(weaponDefinition,position,direction,sendingPlayer)
	if RunService:IsServer() then
		
		local spawnedThrowableAsset = weaponDefinition:GetSpawnedThrowable()
		local clonedThrowable = spawnedThrowableAsset:Clone()
		
		-- Set the owner of this grenade for the combat api
		clonedThrowable:SetAttribute("OwnerID",sendingPlayer.UserId)
		
		local PrimaryPart = clonedThrowable.PrimaryPart
		
		-- Spawn the grenade infront of the player
		clonedThrowable:SetPrimaryPartCFrame( CFrame.new(position + direction * 2,position + direction * 3) )

		clonedThrowable.Parent = workspace
		
		-- Disable velocity force the next frame
		spawn(function()
			if PrimaryPart then
				PrimaryPart.VectorForce.Enabled = false
			end
		end)
	
		-- Server owns the grenade.
		-- Don't want exploiters to teleport the grenade at creation.
		-- Or make a "Grenade shield" hack.
		clonedThrowable.PrimaryPart:SetNetworkOwner(nil)
		
		-- The throwable should destroy itself, but just in case we'll clean up after some time
		DebrisService:AddItem(clonedThrowable,60)
	end	
end
