示例#1
0
std::vector<PathNode*> Pathfinding::FindPath(PathNode* start, PathNode* goal)
{
	std::vector<PathNode*> retPath;

	PathNode* currentNode = new PathNode(*start);
	currentNode->combineNode(currentNode, start);
	while (!ArrivedAtEnd(currentNode, goal))
	{
		PathNode tempChildNode(*currentNode);

		//Get adjacent walkable tiles
		//Move the child node one node to the right to get the node to the right of currentNode
		tempChildNode.xPos++;
		AddChild(tempChildNode, currentNode, goal, Direction::DIRECTION::EAST);

		//Move the child node to the left to get the node to the left of currentNode
		tempChildNode.xPos -= 2;
		AddChild(tempChildNode, currentNode, goal, Direction::DIRECTION::WEST);

		//Move the child node up one row to get the node above currentNode
		tempChildNode.xPos++;
		tempChildNode.zPos++;
		AddChild(tempChildNode, currentNode, goal, Direction::DIRECTION::NORTH);

		//Finally, move the child node to the bottom, to get the node one below currentNode
		tempChildNode.zPos -= 2;
		AddChild(tempChildNode, currentNode, goal, Direction::DIRECTION::SOUTH);

		mClosedSet.insert(currentNode);

		mOpenList.sort(PathNode::FCostSort());

		if (mOpenList.size() > 0)
		{
			currentNode = mOpenList.back();
			mOpenList.remove(currentNode);
		}
		else
		{
			break;
		}
	}
	//Populate and create the path vector
	while (currentNode->parent != NULL && currentNode != start)
	{
		retPath.push_back(currentNode);
		currentNode = currentNode->getParent();
	}
	std::reverse(retPath.begin(), retPath.end());
	mOpenList.clear();
	mClosedSet.clear();
	return retPath;
}