Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH _ 3 (), cocos2d-x_3
Click to download code http://download.csdn.net/detail/lideguo1979/8291803
Create a RouteNavigation class and define the getPath () method to obtain the role path. We design this class in the single-sample mode. First, let's look at the definition of this class.
RouteNavigation. hclass RouteNavigation {public: static RouteNavigation * routeNav_Instance; // static RouteNavigation * getInstance (); // obtain the static object method void getPath (Sprite * playerSprite, int stepsCount, bool ** canPassGrid, int gridRowsCount, int gridColsCount); // defines the method for obtaining paths. protected: RouteNavigation (void );~ RouteNavigation (void );};
RouteNavigation.cppRouteNavigation::~RouteNavigation(void){routeNav_Instance = NULL; }RouteNavigation* RouteNavigation::getInstance(){if(!routeNav_Instance){ routeNav_Instance = new RouteNavigation(); } return routeNav_Instance;
After defining the class, start to implement the getPath () method. Remember the previous getPath process diagram. I will write this method according to the previous process.
Parameter description: playerSprite: The role whose path is to be obtained, that is, the role that calls the getPath method, transmits itself to stepsCount: the number of steps the role should take canPassGrid: two-dimensional array gridRowsCount: number of rows in the canPassGrid array gridColsCount: Number of columns in the canPassGrid array void overflow: getPath (Sprite * playerSprite, int stepsCount, bool ** canPassGrid, int gridRowsCount, int gridColsCount) {// defines a vector one-dimensional array to store the row and column of the obtained path. First, clear pathCols_vector.clear (); pathRow_vector.clear (); // defines the current row and column of the role, int nextCol, nex TRow; int currentCol, currentRow; // gets the coordinate value float x = playerSprite-> getPositionX (); float y = playerSprite-> getPositionY (); // assign values to the column and column variables starting with the role based on the current coordinate value of the role. It is the coordinate divided by the wide and high values of each row and column currentCol = x/tiledHeight; // we used to display the role in the center, in the GameBaseScene: addPlayer () method, add the vertical position + tiledHeight to the role. In this case, the correct number of rows is obtained: currentRow = (y-tiledWidth)/tiledWidth; // defines canPassGrid_copy, the value bool ** canPassGrid_copy = new bool * [gridRowsCount]; for (int row = 0; row <gridRowsCount; row ++) in the two-dimensional array of canPassGrid received) {for (int col = 0; col <gridColsCount; col ++) {canPassGrid_copy [row] [col] = canPassGrid [row] [col] ;}} // create a one-dimensional array dire_4 _4 []. The value indicates whether the four adjacent positions at the top, bottom, and left of the current row and column can go through std: vector <bool> dire_4 _4; // create a canPassDirVector_temp location where the current location can pass up or down std: vector <int> canPassDirVector_temp; int hasGoneNumber = 0; // start looping to find the value of the row and column that can go in each step while (hasGoneNumber <stepsCount) {// first clear the array and restore it to the default value falsedirection_4.clear (); for (int I = 0; I <4; I ++) {direction_4.push_back (false);} canPassDirVector_temp.clear (); // you can view the top, bottom, and left of the current row and column to see if the row and column are successful, assign true or falsedire_4 _4 [GO_UP] = forward (currentRow, currentCol, GO_UP, canPassGrid_copy) to the corresponding position of dire_4; dire_4 [GO_DOWN] = forward (currentRow, currentCol, GO_DOWN, canPassGrid_copy ); dire_4 _4 [GO_LEFT] = histogram (currentRow, currentCol, GO_LEFT, canPassGrid_copy); direction_4 [GO_RIGHT] = histogram (currentRow, currentCol, GO_RIGHT, canPassGrid_copy); // traverse dire_4 _4, locate the available location and store it in canPassDirVector_temp for (int I = 0; I <4; I ++) {if (direction_4 [I]) {canPassDirVector_temp.push_back (I );}} // random int _ rand = rand () % canPassDirVector_temp () % canPassDirVector_temp.size () from the one-dimensional array that records can be used, obtain the row and column values switch (canPassDirVector_temp [_ rand]) {case GO_UP: {nextRow = currentRow-1; nextCol = currentCol; break;} case GO_DOWN: {nextRow = currentRow + 1; nextCol = currentCol; break;} case GO_LEFT: {nextRow = currentRow; nextCol = currentCol-1; break;} case GO_RIGHT: {nextRow = currentRow; nextCol = currentCol + 1; break;} // switch determines the end direction. After assigning values to the next row and column, save the values to pathCols_vector.push_back (nextCol); pathRow_vector.push_back (nextRow ); // set the current row and column to false, which indicates that the row has passed and cannot be followed. The role cannot be moved forward until canPassGrid_copy [currentRow] [currentCol] = false; // point the current row and column value to the next row and column. You are going to locate the path and column currentCol = nextCol; currentRow = nextRow; // Add step 1, start to find the next row and column that can go through hasGoneNumber ++;} // after finding the path, clean up and release the memory of the relevant variables. CC_SAFE_DELETE (canPassGrid_copy); direction_4.clear (); canPassDirVector_temp.clear:: vector <bool> (direction_4 ). swap (dire_4 _4); std: vector <int> (canPassDirVector_temp ). swap (canPassDirVector_temp );}
Let's take a look at how the isCanGoByColRow () method is used to determine whether the current position is up, down, left, and right. The logic is very simple. It is to judge whether the row and column of the Two-dimensional array canPassGrid are true based on the input direction. If it is true, it means that bool RouteNavigation: isCanGoByColRow (int row, int col, int ction, bool ** canPassGrid) {switch (direction) {case GO_UP: {return canPassGrid [row-1] [col];} case GO_DOWN: {return canPassGrid [row + 1] [col];} case GO_LEFT: {return canPassGrid [row] [col-1];} case GO_RIGHT: {return canPassGrid [row] [col + 1] ;}} return false;
Okay, let's modify the go button and test the obtained path void GameBaseScene: addGoButton () {// modify the Go button to menu Menu * menu = Menu :: create (); menu-> setPosition (CCPointZero); // call the goButtonCallback method MenuItemImage * goMenuItemButton = MenuItemImage: create ("map/go_normal.png", "map/go_press.png ", this, menu_selector (GameBaseScene: goButtonCallback); goMenuItemButton-> setPosition (ccp (tableStartPosition_x + 2 * tableWidth, tableStartPositio N_y-tableHeight * 6); menu-> addChild (goMenuItemButton); addChild (menu);} void GameBaseScene: goButtonCallback (cocos2d: CCObject * pSender) {log ("go button clicked"); // first obtain the RouteNavigation path in step 5: getInstance ()-> getPath (player1, 5, canPassGrid, tiledRowsCount, tiledColsCount ); std: vector <int> colVector = RouteNavigation: getInstance ()-> getPathCols_vector (); std: vector <int> rowVector = RouteNavigation: getInsta Nce ()-> getPathRow_vector (); // print the output path for (int I = 0; I <rowVector. size (); I ++) {log ("rowVector row is % d --- colVector col is % d", rowVector [I], colVector [I]);} .....................}
Test results: the current position can be displayed on the left, right, or top of the obtained path.
To be continued .............