Use cocos2d3.0 to write a srpg game-mobile implementation

Source: Internet
Author: User

Determine the moving range of a person in SRPG,

I am using a silly example,

In fact, it is to determine whether the characters can be moved at every point of the movable length,

Then, based on the level of obstacle that the terrain affects the characters,

Each step requires less obstacle-level walking ability.

Specific Code

Void SearchPathForSLG: init (TMXTiledMap * map, Point heroIndex, int runLength, bool movePath [] [255]) {

Point mapSize = Point (map-> getMapSize (). width, map-> getMapSize (). height );

// Reset the movable range

For (int I = 0; I <255; I ++ ){

For (int j = 0; j <255; j ++ ){

MovePath [I] [j] = false;

}

}

// Set the obstacle level of movement

For (int I = 0; I <255; I ++ ){

For (int j = 0; j <255; j ++ ){

ObstacleLevel [I] [j] = TMXMapHelper: getMapTiledObstacleLevel (TMXMapHelper: getMapTiledType (Point (I, j), map ));

}

}

// Start searching

Search (heroIndex, runLength, movePath, mapSize );

}


// Search for the movable range. The sequence is top, bottom, left, and right.

Bool SearchPathForSLG: search (Point heroIndex, int count, bool movePath [] [255], Point mapSize ){

SearchPath (Point (heroIndex. x, heroIndex. Y-1), count, movePath, mapSize );

SearchPath (Point (heroIndex. x, heroIndex. y + 1), count, movePath, mapSize );

SearchPath (Point (heroIndex. X-1, heroIndex. y), count, movePath, mapSize );

SearchPath (Point (heroIndex. x + 1, heroIndex. y), count, movePath, mapSize );

Return false;

}


// Find whether the corresponding location is movable

Bool SearchPathForSLG: searchPath (Point heroIndex, int count, bool movePath [] [255], Point mapSize ){

Point thisPoint = heroIndex;

// If this location does not cross the border, continue the disk

If (thisPoint. x> = 0 & thisPoint. y> = 0 & thisPoint. x

Int pointX = (int) thisPoint. x;

Int pointY = (int) thisPoint. y;

// Obtain the obstacle level for this position

Int thisObstacleLevel = obstacleLevel [pointX] [pointY];

// If the obstacle level is 99, the location cannot be changed.

If (thisObstacleLevel = 99)

Return false;

// If the current mobility capability can be moved after the obstacle level is reduced, continue searching

If (count-thisObstacleLevel)> = 0 ){

MovePath [pointX] [pointY] = true;

Search (thisPoint, count-thisObstacleLevel, movePath, mapSize );

}

}

Return false;

}


You can see the figure for the effect.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.