コード例 #1
0
ファイル: PathFinding.cpp プロジェクト: Angus3397/SP4
void CPathFinding::ContinuePath()
{
	if (m_openList.empty())
	{
		return;
	}

	CSearchCell* currentCell = GetNextCell();

	if (currentCell->m_id == m_goalCell->m_id)
	{
		m_goalCell->parent = currentCell->parent;

		CSearchCell* getPath;

		for (getPath = m_goalCell; getPath != NULL; getPath = getPath->parent)	// Checks through m_goalCell to find shortest path & push to m_pathToGoal list
		{
			m_pathToGoal.push_back(new Vector3(getPath->m_xCoord, 0, getPath->m_zCoord));
		}

		m_foundGoal = true;
		return;
	}
	else 
	{
		// Right Side
		PathOpened(currentCell->m_xCoord + 1, currentCell->m_zCoord, currentCell->G + 1, currentCell);

		// Left Side
		PathOpened(currentCell->m_xCoord - 1, currentCell->m_zCoord, currentCell->G + 1, currentCell);

		// Top
		PathOpened(currentCell->m_xCoord, currentCell->m_zCoord + 1, currentCell->G + 1, currentCell);

		// Bottom
		PathOpened(currentCell->m_xCoord, currentCell->m_zCoord - 1, currentCell->G + 1, currentCell);

		// Diagonals (if needed, remove otherwise)
		// Left-Top Diagonal
		PathOpened(currentCell->m_xCoord - 1, currentCell->m_zCoord + 1, currentCell->G + 1.414f, currentCell);

		// Right-Top Diagonal
		PathOpened(currentCell->m_xCoord + 1, currentCell->m_zCoord + 1, currentCell->G + 1.414f, currentCell);

		// Left-Bottom Diagonal
		PathOpened(currentCell->m_xCoord - 1, currentCell->m_zCoord - 1, currentCell->G + 1.414f, currentCell);

		// Right-Bottom Diagonal
		PathOpened(currentCell->m_xCoord + 1, currentCell->m_zCoord - 1, currentCell->G + 1.414f, currentCell);

		for (int i = 0; i < m_openList.size(); i++)
		{
			if (currentCell->m_id == m_openList[i]->m_id)
			{
				m_openList.erase(m_openList.begin() + 1);
			}
		}
	}
}
コード例 #2
0
void PathFinding::ContinuePath() {
	if(m_openList.empty()) {
		return;
	}

	SearchCell* currentCell = GetNextCell();

	if(currentCell->m_id == m_goalCell->m_id) {
		m_goalCell->mp_parent = currentCell->mp_parent;

		SearchCell* getPath;

		for(getPath = m_goalCell; getPath != NULL; getPath = getPath->mp_parent) {
			m_pathToGoal.push_back(new sf::Vector2f(getPath->m_xcoord, getPath->m_ycoord));
		}

		m_foundGoal = true;
		return;
	}
	else {
		//right
		PathOpened(currentCell->m_xcoord + 1, currentCell->m_ycoord, currentCell->m_g + 10, currentCell);
		//left
		PathOpened(currentCell->m_xcoord - 1, currentCell->m_ycoord, currentCell->m_g + 10, currentCell);
		//down
		PathOpened(currentCell->m_xcoord, currentCell->m_ycoord + 1, currentCell->m_g + 10, currentCell);
		//up
		PathOpened(currentCell->m_xcoord, currentCell->m_ycoord - 1, currentCell->m_g + 10, currentCell);
		/*//left_down diagonal
		PathOpened(currentCell->m_xcoord - 1, currentCell->m_ycoord + 1, currentCell->m_g + 14, currentCell);
		//right_down diagonal
		PathOpened(currentCell->m_xcoord + 1, currentCell->m_ycoord + 1, currentCell->m_g + 14, currentCell);
		//left_up diagonal
		PathOpened(currentCell->m_xcoord - 1, currentCell->m_ycoord - 1, currentCell->m_g + 14, currentCell);
		//right_up diagonal
		PathOpened(currentCell->m_xcoord + 1, currentCell->m_ycoord - 1, currentCell->m_g + 14, currentCell);
		*/
		for(int i = 0; i < m_openList.size(); i++) {
			if(currentCell->m_id == m_openList.at(i)->m_id) {
				m_openList.erase(m_openList.begin() + i);
			}
		}
	}
}
コード例 #3
0
void PathFinding::ContinuePath() {
	for (int i = 0; i < 50; i++) {
		if (m_openList.empty()) return;
		SearchCell *currentCell = GetNextCell();
		if (currentCell->m_id == m_goalCell->m_id) {
			m_goalCell->parent = currentCell->parent;
			SearchCell *getPath;
			for (getPath = m_goalCell; getPath != NULL; getPath = getPath->parent) m_pathToGoal.push_back(new Vector3((float)(getPath->m_x_coord * CELL_SIZE), (float)(getPath->m_y_coord * CELL_SIZE), 0));
			m_foundGoal = true;
			return;
		}
		else {
			// right side
			PathOpened(currentCell->m_x_coord + 1, currentCell->m_y_coord, currentCell->g + 1, currentCell);
			// left side
			PathOpened(currentCell->m_x_coord - 1, currentCell->m_y_coord, currentCell->g + 1, currentCell);
			// up
			PathOpened(currentCell->m_x_coord, currentCell->m_y_coord + 1, currentCell->g + 1, currentCell);
			// down
			PathOpened(currentCell->m_x_coord, currentCell->m_y_coord - 1, currentCell->g + 1, currentCell);

			// left-up diagonal
			PathOpened(currentCell->m_x_coord - 1, currentCell->m_y_coord + 1, currentCell->g + 1.414f, currentCell);
			// left down diagonal
			PathOpened(currentCell->m_x_coord - 1, currentCell->m_y_coord - 1, currentCell->g + 1.414f, currentCell);

			// right up diagonal
			PathOpened(currentCell->m_x_coord + 1, currentCell->m_y_coord + 1, currentCell->g + 1.414f, currentCell);
			// right down diagonal
			PathOpened(currentCell->m_x_coord + 1, currentCell->m_y_coord - 1, currentCell->g + 1.414f, currentCell);

			for (unsigned int i = 0; i < m_openList.size(); i++) {
				if (currentCell->m_id == m_openList[i]->m_id) {
					m_openList.erase(m_openList.begin() + 1);
				}
			}
		}
	}
}