Cocos2d-x3.6 connection and view connection algorithm, cocos2d-x3.6 connection
My blog: http://blog.csdn.net/dawn_moon
The previous chapter describes the main logic of the connected video game, connects algorithms, and describes how to implement the game.
This continuous view does not use the breadth-first search algorithm. It uses a more skillful algorithm. See the previous section.
It can be roughly divided into several parts:
Check the connection function.
Bool GameScene: link (cocos2d: Vec2 v1, cocos2d: Vec2 v2) {if (v1.equals (v2) {return false;} // path point set, is a vector mPath. clear (); // determine whether the two clicked points have the same pattern. if (mMap [(int) v1.x] [(int) v1.y] = mMap [(int) v2.x] [(int) v2.y]) {// directly connect if (linkD (v1, v2) {mPath. push_back (v1); mPath. push_back (v2); return true;} // a corner, which can be directly connected to auto p = Vec2 (v1.x, v2.y); if (mMap [(int) p. x] [(int) p. y] = 0) {if (linkD (v1, p) & linkD (p, v2) {mPath. push_back (v1); mPath. push_back (p); mPath. push_back (v2); return true ;}// a corner, which can be directly connected to p = Vec2 (v2.x, v1.y); if (mMap [(int) p. x] [(int) p. y] = 0) {if (linkD (v1, p) & linkD (p, v2) {mPath. push_back (v1); mPath. push_back (p); mPath. push_back (v2); return true ;}// X extension to determine whether a directly connected vertex expandX (v1, p1E); expandX (v2, p2E ); for (auto pt1: p1E) {for (auto pt2: p2E) {if (pt1.x = pt2.x) {if (linkD (pt1, pt2) {mPath. push_back (v1); mPath. push_back (pt1); mPath. push_back (pt2); mPath. push_back (v2); return true ;}}// Y extension, determine whether there are directly connected points expandY (v1, p1E); expandY (v2, p2E ); for (auto pt1: p1E) {for (auto pt2: p2E) {if (pt1.y = pt2.y) {if (linkD (pt1, pt2) {mPath. push_back (v1); mPath. push_back (pt1); mPath. push_back (pt2); mPath. push_back (v2); return true ;}}} return false ;}
The basis of the connected function is direct connection. The two points must be converted to whether the function can be directly connected. Let's look at this function:
Bool GameScene: linkD (Vec2 v1, Vec2 v2) {// X coordinates are consistent, scanning Y coordinates one by one. if all paths are in the middle, you can directly connect to if (v1.x = v2.x) {int y1 = MIN (v1.y, v2.y); int y2 = MAX (v2.y, v2.y); bool flag = true; for (int y = y1 + 1; y <y2; y ++) {if (mMap [(int) v1.x] [y]! = 0) {flag = false; break;} if (flag) {return true ;}// Y coordinates are consistent, X coordinates are scanned one by one. if path is used in the middle, then you can directly connect to if (v1.y = v2.y) {int x1 = MIN (v1.x, v2.x); int x2 = MAX (v1.x, v2.x); bool flag = true; for (int x = x1 + 1; x <x2; x ++) {if (mMap [x] [(int) v1.y]! = 0) {flag = false; break;} if (flag) {return true ;}} return false ;}
Note that the coordinates here are all map coordinates, which are integers such as the number of cells.
Let's take a look at the extended functions:
Void GameScene: expandX (Vec2 v, std: vector <Vec2> & vector) {// note that the second parameter vector is a reference vector. clear (); // the x axis is extended to the boundary. If it is empty, the extended point is placed in the container for (int x = (int) v. x + 1; x <xCount; x ++) {if (mMap [x] [(int) v. y]! = 0) {break;} vector. push_back (Vec2 (x, (int) v. y);} // extend week Y to the boundary. If it is empty, add the extended vertex to the container for (int x = (int) v. x-1; x> = 0; x --) {if (mMap [x] [(int) v. y]! = 0) {break;} vector. push_back (Vec2 (x, (int) v. y) ;}} void GameScene: expandY (Vec2 v, std: vector <Vec2> & vector) {vector. clear (); for (int y = (int) v. y + 1; y <yCount; y ++) {if (mMap [(int) v. x] [y]! = 0) {break;} vector. push_back (Vec2 (int) v. x, y);} for (int y = (int) v. y-1; y> = 0; y --) {if (mMap [(int) v. x] [y]! = 0) {break;} vector. push_back (Vec2 (int) v. x, y ));}}
Note that the second parameter of the two functions is reference transfer. Why.
Because I don't want to use a function to return a copy of a container, both of these functions return void, and directly reference the variable itself without returning the value.
The connectivity function checks whether two points are connected. If the two points are connected, the connected points are placed in the container, and then the points in the container are used to draw the connection route.
The next section describes how to draw a line.