A Method for C ++ decoupling

Source: Internet
Author: User
Tags map class unpack

Data-centric, global variables are used between multiple modules for message notifications or status changes, so there is no direct connection between modules. All modules use global variables directly. The problem of global variables is not discussed here.

 

In my example, the hero in the game runs automatically on the map to the place where the mouse is clicked, or the coordinates transmitted by network signaling. SetAutoMovePos, the automatic moving method of the main character, is not described here.

 

The original practice is as follows:

// Map. cpp file // click the event bool Map: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {CCPoint point = pTouch-> getLocation (); // obtain the map coordinate float x = point. x-getPositionX (); float y = point. y-getPositionY (); // obtain the wizard control Player * pc = (Player *) this-> getChildByTag (PC. mapUserInfo. ID); pc-> setAutoMovePos (int) x, (int) y); // the engine will help the main character automatically move // indicates that the event has been intercepted, no more go to the next layer. return true;} // set the main character's automatic path void Map: auto_routing (void) {// obtain the main character's genie control Player * pc = (Player *) this-> getChildByTag (PC. mapUserInfo. ID); pc-> setAutoMovePos (int) x, (int) y); // the engine will help the protagonist move automatically}

The following code snippets of Network Signaling parsing players:

// Xiyou_unpack.cpp file // 230 automatically find void xiyou_unpack: unpack_MAP_AUTO_ROUTING (char * data, int32 len) {PC. mapUserInfo. map_destination_x = unpack. unpack_GET_H (); PC. mapUserInfo. map_destination_y = unpack. unpack_GET_H (); if (xiyou_game: getGameState () = XIYOU_STATE_MAP) {map. auto_routing ();}}

The above code can illustrate the problem. In the xiyou_unpack.cpp file, Map. h needs to be introduced, so that two different modules will be coupled together.

 

The following is a sample code that separates xiyou_unpack.cpp from Map. cpp from the data center of the main character and connects xiyou_unpack.cpp with PC Data.

The PC Data is added with the Boolean variable PC. mapUserInfo. isAutoMove to identify whether to start automatic movement.

// Map. cpp file // click the event bool Map: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {CCPoint point = pTouch-> getLocation (); float x = point. x-getPositionX (); float y = point. y-getPositionY (); // sets the automatic mobile PC. mapUserInfo. isAutoMove = true; PC. mapUserInfo. map_destination_x = (int) x; PC. mapUserInfo. map_destination_y = (int) y; return true; // indicates that the event has been intercepted and no longer goes further} // Map refresh function. Each frame calls void Map :: mapUpdate (float dt) {if (PC. mapUserInfo. isAutoMove) {PC. mapUserInfo. isAutoMove = false; Player * PCP = (Player *) this-> getChildByTag (PC. mapUserInfo. ID); PCP-> setAutoMovePos (PC. mapUserInfo. map_destination_x, PC. mapUserInfo. map_destination_y );}}

In this way, the PC must be automatically moved in each frame of the map refresh. If necessary, the PC will be automatically moved.

Modify the position of the signaling parsing module

// Xiyou_unpack.cpp file // 230 automatically find void xiyou_unpack: unpack_MAP_AUTO_ROUTING (char * data, int32 len) {PC. mapUserInfo. map_destination_x = unpack. unpack_GET_H (); PC. mapUserInfo. map_destination_y = unpack. unpack_GET_H (); PC. mapUserInfo. isAutoMove = true; // start automatic path searching}

In this way, when the signaling arrives at the client, you only need to modify the data of the PC. The map module is refreshing the data of the PC. when the data is flushed to the start of Automatic route searching, the coordinates of the destination of the PC are also obtained, you can start to walk automatically!

 

The above xiyou_unpack.cpp file does not contain the Map object, so you do not need to introduce the Map class. The two modules do not care about each other and achieve decoupling!

 

This article is from the "key code window" blog and will not be reprinted!

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.