Dijkstra of cocos2dx search path
Vc/uxL/Platform + CjxwcmUgY2xhc3M9 "brush: java;"> # ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include "cocos2d. h "# include" vector "using namespace std; USING_NS_CC; class PathSprite: public cocos2d: Sprite // inherit the Sprite class, because you need to add some other variables in It {PathSprite (): sprite () {m_parent = NULL; m_child = NULL; m_distance = 0;}; public: static PathSprite * create (const char * ch) {PathSprite * pRet = new PathSprite (); if (pRet) {pRet-> initWithFile (ch); pRet-> autorelease (); return pRet;} else {delete pRet; pRet = NULL; return NULL ;}} pathSprite * m_parent; // parent node PathSprite * m_child; // child node float m_distance; // The distance from the start point to int m_x; // map coordinate int m_y ;}; class PathSearchInfo // pathing class (mainly responsible for pathing parameters and logic) {public: static int m_startX; // start point static int m_startY; static int m_endX; // end point static int m_endY; static vector M_openList; // open list (which stores adjacent nodes) static vector M_inspectList; // The detection list (the nodes in which the obstacle is stored) static void barrierTest (int x, int y) // simulate the obstacle {PathSprite * _ z = getObjByPointOfMapCoord (m_inspectList, x, y); if (_ z) {_ z-> setColor (ccColor3B: YELLOW); removeObjFromList (PathSearchInfo: m_inspectList, _ z );}} static float calculateTwoObjDistance (PathSprite * obj1, PathSprite * obj2) // calculate the distance between two objects {float _ offsetX = obj1-> m_x-obj2-> m_x; float _ offsetY = obj1-> m_y-obj2-> m_y; return sqrt (_ offsetX * _ offsetX + _ offsetY * _ offsetY);} static void inspectTheAdjacentNodes (PathSprite * node, pathSprite * adjacent) // puts adjacent nodes into an open node {if (adjacent) {adjacent-> m_distance = node-> m_distance + PathSearchInfo: calculateTwoObjDistance (node, adjacent ); // obtain the accumulated path adjacent-> m_parent = node; // set the parent node-> m_child = adjacent; // set the subnode PathSearchInfo: removeObjFromList (PathSearchInfo: m_inspectList, adjacent); // Delete the detected vertex from the detection list PathSearchInfo: m_openList.push_back (adjacent); // Add it to the open list} static PathSprite * getMinPathFormOpenList () // obtain the minimum path value {PathSprite * _ sp = * m_openList.begin (); for (vector : Iterator iter = m_openList.begin (); iter! = M_openList.end (); iter ++) {if (* iter)-> m_distance <_ sp-> m_distance) {_ sp = * iter;} return _ sp ;} static PathSprite * getObjByPointOfMapCoord (vector & SpriteVector, int x, int y) // obtain the object {for (int I = 0; I <spriteVector. size (); I ++) {if (spriteVector [I]-> m_x = x & spriteVector [I]-> m_y = y) {return spriteVector [I];} return NULL;} static bool removeObjFromList (vector & SpriteVector, PathSprite * sprite) // remove the object from the container {for (vector : Iterator iter = spriteVector. begin (); iter! = SpriteVector. end (); iter ++) {if (* iter = sprite) {spriteVector. erase (iter); return true ;}} return false ;}; class HelloWorld: public cocos2d: Layer {public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d: Scene * createScene (); // Here's a difference. method 'init 'in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init (); // a selector callback void menuCloseCallback (cocos2d: Ref * pSender ); // implement the "static create ()" method manually CREATE_FUNC (HelloWorld);}; # endif // _ HELLOWORLD_SCENE_H __
# Include "HelloWorldScene. h" vector
PathSearchInfo: m_openList; vector
PathSearchInfo: m_inspectList; int PathSearchInfo: m_startX; int PathSearchInfo: m_startY; int PathSearchInfo: m_endX; int PathSearchInfo: m_endY; Scene * HelloWorld: createScene () {// 'Scene 'is an autorelease object auto scene = scene: create (); // 'player' is an autorelease object auto layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer); // return the scene return scen E;} // on "init" you need to initialize your instancebool HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); Vec2 origin = Director: getInstance () -> getVisibleOrigin (); Size winSize = Director: getInstance ()-> getWinSize (); /// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelsponob Ject // simulate the upper left corner of a map as (0, 0). It is used to simulate tiledmap. The width of each block is 1 int length = 10; for (int I = 0; I <length; I ++) {for (int j = 0; j <length; j ++) {PathSprite * _ sp = PathSprite: create ("CloseNormal.png"); _ sp-> m_x = j; _ sp-> m_y = I; Size _ size = _ sp-> getContentSize (); _ sp-> setPosition (CCPoint (j * _ size. width + 100,-I * _ size. height + 600); PathSearchInfo: m_inspectList.push_back (_ sp); this-> addChild (_ sp) ;}// sets the obstacle PathS EarchInfo: barrierTest (); PathSearchInfo: barrierTest ); pathSearchInfo: barrierTest ); pathSearchInfo: barrierTest (2, 7); PathSearchInfo: barrierTest (3, 7); // PathSpr Ite: getObjByPointOfMapCoord (m_inspectList, 2, 5)-> removeFromParent (); // set the start and end PathSearchInfo: m_startX = 0; PathSearchInfo: m_startY = 1; PathSearchInfo :: m_endX = 4; PathSearchInfo: m_endY = 9; // get the Start Node PathSprite * _ sp = PathSearchInfo: Unknown (PathSearchInfo: m_inspectList, PathSearchInfo: m_startX, PathSearchInfo:: m_startY); // set the distance from the start point to 0_sp-> m_distance = 0; // Delete the detected vertex from the detection list PathSearchInfo: removeObjFromList (PathSearchInfo: m_inspectList, _ sp); // Add the open list PathSearchInfo: m_openList.push_back (_ sp); PathSprite * _ node = NULL; while (true) {// obtain the vertex _ node = PathSearchInfo: getMinPathFormOpenList (); if (! _ Node) {// The path break cannot be found;} // Delete the computed vertex from the open list PathSearchInfo: removeObjFromList (PathSearchInfo: m_openList, _ node ); int _ x = _ node-> m_x; int _ y = _ node-> m_y; // if (_ x = PathSearchInfo: m_endX & _ y = PathSearchInfo:: m_endY) {break;} // checks whether adjacent nodes in eight directions can be placed in the open list. PathSprite * _ adjacent = PathSearchInfo: getObjByPointOfMapCoord (PathSearchInfo: m_inspectList, _ x + 1, _ y + 1); PathSearchInfo: inspectTheAdjacentNodes (_ node, _ adjacent); _ adjacent = PathSearchInfo: getObjByPointOfMapCoord (PathSearchInfo: m_inspectList, _ x + 1, _ y); PathSearchInfo: inspectTheAdjacentNodes (_ node, _ adjacent); _ adjacent = PathSearchInfo: getObjByPointOfMapCoord (PathSearchInfo: m_inspectList, _ x + 1, _ Y-1); PathSearchInfo: inspectTheAdjacentNodes (_ node, _ adjacent); _ adjacent = PathSearchInfo: Unknown (PathSearchInfo: m_inspectList, _ x, _ y-1); PathSearchInfo:: inspectTheAdjacentNodes (_ node, _ adjacent); _ adjacent = PathSearchInfo: getObjByPointOfMapCoord (PathSearchInfo: m_inspectList, _ x-1, _ y-1); PathSearchInfo :: inspectTheAdjacentNodes (_ node, _ adjacent); _ adjacent = PathSearchInfo: callback (PathSearchInfo: m_inspectList, _ x-1, _ y); PathSearchInfo: inspectTheAdjacentNodes (_ node, _ adjacent); _ adjacent = PathSearchInfo: locate (PathSearchInfo: m_inspectList, _ x-1, _ y + 1); PathSearchInfo: inspectTheAdjacentNodes (_ node, _ adjacent ); _ adjacent = PathSearchInfo: substring (PathSearchInfo: m_inspectList, _ x, _ y + 1); PathSearchInfo: inspectTheAdjacentNodes (_ node, _ adjacent);} int I = 0; while (_ node) {PathSprite * _ sp = _ node; _ node = _ node-> m_parent; _ sp-> setColor (ccColor3B: MAGENTA);} return true ;} void HelloWorld: menuCloseCallback (Ref * pSender) {# if (CC_TARGET_PLATFORM = platform) | (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT) MessageBox ("You pressed the close button. windows Store Apps do not implement a close button. "," Alert "); return; # endif Director: getInstance ()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0); # endif}