Однако я столкнулся с проблемой: может показаться, что мне, что если я хочу использовать члены и функции из другого класса, то мне придется инкапсулировать их или внедрить в класс, который мы собираемся запускать, хотя в нем может быть меньше данных, чем в самом исходном классе , вероятно, это не будет таким уж большим улучшением.
Скажем, у меня есть что-то вроде:
Код: Выделить всё
internal async Task
PopulatePathObjectAsync(Vector3Int origin, Vector3Int destination, PathObject path)
{
return await Task.Factory.StartNew(() => PopulatePathObject(origin, destination, path));
}
/// Not sure if we want to make this a task or not because we may just parallelize and await the outer task.
/// We'll have to decide when we get down to finalization of the architecture and how it's used.
internal PathObject PopulatePathObject(Vector3Int origin, Vector3Int destination, PathObject path)
{
Debug.Log($"Pathfinding Search On Thread: ({System.Threading.Thread.CurrentThread.ManagedThreadId})");
if (!TryVerifyPath(origin, destination, ref path, out PathingNode currentNode))
return path;
var openNodes = m_OpenNodeHeap;
m_ClosedNodes.Clear();
openNodes.ClearAndReset();
openNodes.AddNode(currentNode);
for (int i = CollectionBufferSize; openNodes.Count > 0 && i >= 0; i--)
{
currentNode = ProcessNextOpenNode(openNodes);
if (NodePositionMatchesVector(currentNode, destination))
{
return path.PopulatePathBufferFromOriginToDestination(currentNode, origin, PathState.CompletePath);
}
ProcessNeighboringNodes(currentNode, destination);
}
return path.PopulatePathBufferFromOriginToDestination(currentNode, origin, PathState.IncompletePath);
}
Код: Выделить всё
private class PopulatePathObjectTask
{
private readonly Vector2Int m_Origin;
private readonly Vector3Int m_Destination;
private readonly PathObject m_Path;
public PopulatePathObjectTask(Vector2Int origin, Vector3Int destination, PathObject path)
{
m_Origin = origin;
m_Destination = destination;
m_Path = path;
}
public PathObject PopulatePathObject(Vector3Int origin, Vector3Int destination, PathObject path)
{
///Obviously here, without access to the actual AStar class responsible for the search,
///I don't have access to the functions or the class members such as the heap or the hashset
///that represents the closed nodes as well as the calculated buffer size based on the space-state
///dimensions. With that, I'd just be recreating the class and not avoiding much, if any,
///of the overhead created by the closure capturing the class in the first place.
}
}
Подробнее здесь: https://stackoverflow.com/questions/779 ... ned-object