Beispiel #1
0
void CMap::Input(Vec2I position)
{
	Vec2I end_pos(position.x / GRID_WIDTH, position.y / GRID_HEIGHT);
	DEBUG_TRACE("start:%d,%d\tend:%d,%d\n", m_pos.x, m_pos.y, end_pos.x, end_pos.y);

	if (! IsFree(end_pos))
		return;

	QWORD start_time = GetTimer()->GetTime();
	if (m_Astar.FindPath(m_pos, end_pos))
	{
		QWORD end_time = GetTimer()->GetTime();
		DEBUG_TRACE("find path time:%f\n", GetTimer()->GetTimeMillisec(end_time - start_time));

		m_findPath.clear();
		std::vector<Vec2I>& vec = m_Astar.GetPath();
		m_findPath.reserve(vec.size());
		for (int i = 0; i < vec.size(); ++i)
		{
			Vec2I pt = vec[i];
			m_findPath.push_back(Grid2CenterPt(pt));
		}
		m_pos = end_pos;
		m_rect.SetRect(m_pos.x * GRID_WIDTH, m_pos.y * GRID_HEIGHT, (m_pos.x + 1) * GRID_WIDTH, (m_pos.y + 1) * GRID_HEIGHT);
	}
}
Beispiel #2
0
int main()
{
	int array[12][12] = 
	{
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
		{ 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1},
		{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1},
		{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1},
		{ 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1},
		{ 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
		{ 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
	};
	
	CAStar* pAStar = new CAStar(array);
	CPoint* start = new CPoint(1, 1);
	CPoint* end = new CPoint(6, 10);
	CPoint* point = pAStar->FindPath(start, end, false);

	while(point != NULL)
	{
		std::cout << "(" << point->X << "," << point->Y << ");" << std::endl;
		point = point->m_parentPoint;
	}
	return 0;
}