当前位置: 首页>>代码示例>>C++>>正文


C++ AStarNode::setPosition方法代码示例

本文整理汇总了C++中AStarNode::setPosition方法的典型用法代码示例。如果您正苦于以下问题:C++ AStarNode::setPosition方法的具体用法?C++ AStarNode::setPosition怎么用?C++ AStarNode::setPosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AStarNode的用法示例。


在下文中一共展示了AStarNode::setPosition方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ccp

KDbool Ch7_GridPathfinding::init ( KDvoid )
{	
	// Superclass initialization and message
	if ( !Recipe::init ( ) )
	{
		return KD_FALSE;
	}

	this->showMessage ( "Tap and hold to draw walls.\nPress 'Find Path' to run the simulation." );

	//Initial variables
	m_tGridSize			= ccp ( 25, 15 );
	m_fNodeSpace		= 16.0f;
	m_tTouchedNode		= ccp ( 0, 0 );
	m_tStartCoord		= ccp ( 2, 2 );
	m_tEndCoord			= ccp ( m_tGridSize.x - 3, m_tGridSize.y - 3 );
	m_bTouchedNodeIsNew = KD_FALSE;
	m_pFoundPath		= new CCArray ( );
	m_bAddMode			= KD_TRUE;
	
	// Seperate grid node
	m_pGridNode = CCNode::create ( );
	m_pGridNode->setPosition ( ccp ( 35, 15 ) );
	this->addChild ( m_pGridNode, 3 ); 

	// Create 2D array (grid)
	m_pGrid = new CCArray ( (KDint) m_tGridSize.x ); 
	for ( KDint x = 0; x < (KDint) m_tGridSize.x; x++ )
	{
		m_pGrid->addObject ( CCArray::createWithCapacity ( (KDint) (KDint) m_tGridSize.y ) );
	}

	// Create AStar nodes and place them in the grid
	for ( KDint x = 0; x < (KDint) m_tGridSize.x; x++ )
	{
		for ( KDint y = 0; y < (KDint) m_tGridSize.y; y++ )
		{
			// Add a node
			AStarNode*	pNode = AStarNode::create ( );
			pNode->setPosition ( ccp ( x * m_fNodeSpace + m_fNodeSpace / 2, y * m_fNodeSpace + m_fNodeSpace / 2 ) );
			( (CCArray*) m_pGrid->objectAtIndex ( x ) )->addObject ( pNode );
		}
	}
	
	// Add neighbor nodes
	for ( KDint x = 0; x < (KDint) m_tGridSize.x; x++ )
	{
		for ( KDint y = 0; y < (KDint) m_tGridSize.y; y++ )
		{			
			// Add a node
			AStarNode*	pNode = (AStarNode*) ( (CCArray*) m_pGrid->objectAtIndex ( x ) )->objectAtIndex ( y );
			
			// Add self as neighbor to neighboring nodes
			this->addNeighbor ( pNode, x - 1, y - 1 );		// Top-Left
			this->addNeighbor ( pNode, x - 1, y     );		// Left
			this->addNeighbor ( pNode, x - 1, y + 1 );		// Bottom-Left
			this->addNeighbor ( pNode, x    , y - 1 );		// Top
			
			this->addNeighbor ( pNode, x    , y + 1 );		// Bottom
			this->addNeighbor ( pNode, x + 1, y - 1 );		// Top-Right
			this->addNeighbor ( pNode, x + 1, y     );		// Right
			this->addNeighbor ( pNode, x + 1, y + 1 );		// Bottom-Right		
		}
	}

	// Add visual represenation of nodes
	this->addGridArt ( );
	
	// Menu items 
	CCMenuItemFont::setFontSize ( 30 );

	CCMenuItemFont*		pFindPathItem = CCMenuItemFont::create ( "Find Path", this, menu_selector ( Ch7_GridPathfinding::findPath ) );
	pFindPathItem->setScale ( 0.65f );
	
	CCMenuItemToggle*	pSwitchModeItem = CCMenuItemToggle::createWithTarget 
	(
		this, 
		menu_selector ( Ch7_GridPathfinding::switchMode ), 
		CCMenuItemFont::create ( "Switch Mode: Remove Wall" ),
		CCMenuItemFont::create ( "Switch Mode: Add Wall" ), 
		KD_NULL 
	);
	pSwitchModeItem->setScale ( 0.65f );
	
	CCMenu*				pMenu = CCMenu::create ( pFindPathItem, pSwitchModeItem, KD_NULL );
	pMenu->alignItemsVertically ( 0 );
	pMenu->setPosition ( ccp ( 350, 290 ) );
	this->addChild ( pMenu, Z_HUD );

	// Add draw layer
	this->addDrawLayer ( );
	
	// Schedule step method
	this->schedule ( schedule_selector ( Ch7_GridPathfinding::step ) );

	return KD_TRUE;
}
开发者ID:mcodegeeks,项目名称:OpenKODE-Framework,代码行数:97,代码来源:Ch7_GridPathfinding.cpp

示例2: if

std::vector<AStarNode*>* AStarAlgorithm::startAlgorithm2(AStarNode* startNode, AStarNode* endNode, std::vector<AStarNode*> &path)
{
	path.clear();
	std::vector<AStarNode*> pathList;
	pathList.push_back(startNode);

	std::vector<AStarNode*> wastedList;

	AStarNode* tmp;
	bool wasIn = false;
	bool shorterPathFound = false;

	int distanceTraveled = 0;

	bool secondOut = false;
	AStarNode* returnNode;
	returnNode = startNode;

	while (pathList.size() != 0 && pathList.back()->getName() != endNode->getName() && !pathList.empty())
	{
		tmp = pathList.back();
		wastedList.push_back(pathList.back());
		pathList.pop_back();

		if (secondOut)
		{
			returnNode = tmp;
			secondOut = false;
		}

		if (tmp->getName() == startNode->getName())
		{
			secondOut = true;
		}

		//adding new paths
		for (int i = 0; i < tmp->getPaths()->size(); i++)
		{
			if (tmp->getPaths()->at(i)->getEndNode()->getName() != tmp->getVisitor()->getName() && tmp->getPaths()->at(i)->getEndNode()->getName() != startNode->getName())
			{
				AStarNode* copyNode = new AStarNode();
				for (int j = 0; j < pathList.size(); j++)
				{
					if (tmp->getPaths()->at(i)->getEndNode()->getName() == pathList.at(j)->getName())
					{
						wasIn = true;
						copyNode->setPathList(*pathList.at(j)->getPaths());
						copyNode->setName(pathList.at(j)->getName());
						copyNode->setDistanceToGoal(pathList.at(j)->getDistanceToGoal());
						//copyNode->setPosition(wastedList.at(j)->getPosition());
						break;
					}
				}
				for (int m = 0; m < wastedList.size(); m++)
				{
					if (tmp->getPaths()->at(i)->getEndNode()->getName() == wastedList.at(m)->getName())
					{
						wasIn = true;
						copyNode->setPathList(*wastedList.at(m)->getPaths());
						copyNode->setName(wastedList.at(m)->getName());
						copyNode->setDistanceToGoal(wastedList.at(m)->getDistanceToGoal());
						copyNode->setPosition(wastedList.at(m)->getPosition());
						break;
					}
				}
				if (!wasIn){
					tmp->getPaths()->at(i)->getEndNode()->setDistanceTravelled(tmp->getPaths()->at(i)->getTimeToTravel() + tmp->getDistanceTravelled());
					tmp->getPaths()->at(i)->getEndNode()->setTemporary(tmp->getPaths()->at(i)->getEndNode()->getDistanceToGoal() + tmp->getPaths()->at(i)->getEndNode()->getDistanceTravelled());
					for (int n = 0; n < pathList.size(); n++)
					{
						if (pathList.at(n)->getName() == tmp->getPaths()->at(i)->getEndNode()->getName() && pathList.at(n)->getDistanceTravelled() < tmp->getPaths()->at(i)->getEndNode()->getDistanceTravelled())
						{
							shorterPathFound = true;
						}
						else if (pathList.at(n)->getName() == tmp->getPaths()->at(i)->getEndNode()->getName() && pathList.at(n)->getDistanceTravelled() >= tmp->getPaths()->at(i)->getEndNode()->getDistanceTravelled()){
							pathList.erase(pathList.begin() + n);
						}
					}

					if (!shorterPathFound)
					{
						pathList.push_back(tmp->getPaths()->at(i)->getEndNode());
						tmp->getPaths()->at(i)->getEndNode()->setVisitor(tmp);
					}
					else{
						shorterPathFound = false;
					}
				}
				else{
					copyNode->setDistanceTravelled(-(copyNode->getDistanceTravelled()));
					copyNode->setDistanceTravelled(tmp->getPaths()->at(i)->getTimeToTravel() + tmp->getDistanceTravelled());
					copyNode->setTemporary(tmp->getPaths()->at(i)->getEndNode()->getDistanceToGoal() + copyNode->getDistanceTravelled());
					for (int n = 0; n < pathList.size(); n++)
					{
						if (pathList.at(n)->getName() == copyNode->getName() && pathList.at(n)->getDistanceTravelled() < copyNode->getDistanceTravelled())
						{ 
							shorterPathFound = true;
						}
						else if (pathList.at(n)->getName() == tmp->getPaths()->at(i)->getEndNode()->getName() && pathList.at(n)->getDistanceTravelled() >= tmp->getPaths()->at(i)->getEndNode()->getDistanceTravelled()){
							pathList.at(n)->setDistanceTravelled(copyNode->getDistanceTravelled());
//.........这里部分代码省略.........
开发者ID:GameEngineKoblenz,项目名称:GeKo,代码行数:101,代码来源:AStarAlgorithm.cpp


注:本文中的AStarNode::setPosition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。