Cocos2d-x 3.0 game instance learning notes "card tower defense" sixth step --- Fort & amp; click the fort to add a hero & amp; hero upgrade, cocos2d tower defense

Source: Internet
Author: User
Tags parse csv file

Cocos2d-x 3.0 game instance learning notes the sixth step of the card tower guard --- Fort & click the gun to add a hero & hero upgrade, cocos2d tower defense

/* Description:

** 1. This game instance is the last game on the cocos2d-x game development journey, which is rewritten and noted down here with 3.0

** 2. I have also asked me about wood. He said: Write it as you like. First, do not copy the code completely. Second, You can note that it is a learning note-Good Guy.

** 3. Here With cocos2d-x 3.0 version rewriting, many places are different, but from the rewriting process also good learning cocos2d-x

*/

* ** All the code corresponding to each step and the resources used are packaged at the end.

* ** To avoid code overhead, the code in each step is marked. At first glance, the code is implemented in the first step to avoid errors and cannot be changed back (if not, Git should be used again ?)

* ** You can follow the design idea (well, the name is too tall. The reality is this step to do) first self-implementation-cocos2d-x is so, the same function has many different implementation methods; first self-toss is quite good.

* ** For the convenience of porting to the mobile phone, android testing is compiled for each step. Because the code can be compiled in win32 many times, compilation will fail, the code is tested.

Note content:

1. Design Ideas

2. Code & Test Results

3. Next Content preview

4. source code and resources

I. Design Ideas

In the previous step, after entering different levels of game levels, we loaded the points of the previously edited fort.

In this step, we will use real gunshots to demonstrate these points.

Then click the bastion host to add a hero.

1. First, for subsequent expansion, the fort should also have attributes, such as the level or something. So Heroes and monsters also have various attributes such as speed and defense. You can extract a base class Entity.

2. In this step, the relationship between the gun and the hero is involved. First, design the gun. The gun inherits the entity, which is displayed through the genie and performs an action. It looks tall.

3. In the hero manager, the previously loaded point is saved in the container. We traverse the container and place a turret for each turret coordinate.

4. Then there is a hero. In this step, we only click the gun to generate a hero. The hero's attributes are related to the configuration file Csv.

5. After adding a hero, click "hero" again to perform some operations and upgrade! Delete!

Ii. Code & Effects

First, let's look at the base class:. h

<span style="font-size:14px;">class Entity : public Node{public:Entity();~Entity();void bindSprite(Sprite* sprite);Sprite* getSprite();void hurtMe(int hurtValue);bool isDead();Rect boundingBox();protected:virtual void onDead();virtual void onBindSprite();virtual void onHurt(int hurtValue);private:Sprite* _sprite;bool _isDead;CC_SYNTHESIZE(int,_id,ID);CC_SYNTHESIZE(int,_modeID,ModeID);CC_SYNTHESIZE(__String*,_Name,Name);CC_SYNTHESIZE(int,_hp,HP);CC_SYNTHESIZE(int,_defense,Defense);CC_SYNTHESIZE(int,_speed,Speed);CC_SYNTHESIZE(int,_level,Level);};</span>
. Cpp

<Span style = "font-size: 14px;"> Entity: Entity () {_ sprite = NULL; _ Name = _ String: create (""); _ hp = 1; _ defense = 1; _ isDead = false; _ speed = 1; _ level = 1;} Entity ::~ Entity () {} void Entity: bindSprite (Sprite * sprite) {if (this-> _ sprite! = NULL) {_ sprite-> removeFromParentAndCleanup (true);} this-> _ sprite = sprite; this-> addChild (_ sprite ); // ** 6 ** if the subclass binding requires other functions, rewrite the following function onBindSprite ();} Sprite * Entity: getSprite () {return this-> _ sprite;} void Entity: hurtMe (int hurtValue) {if (_ isDead) {return;} if (hurtValue <= getDefense ()) {hurtValue = 1 ;}int curHP = getHP (); int afterHP = curHP-hurtValue; onHurt (hurtValue); if (afterHP> 0) {setHP (afterHP );} else {_ isDead = true; onDead () ;}} Rect Entity: boundingBox () {Node: boundingBox (); if (getSprite () = NULL) {return CCRectMake (0, 0, 0);} Point pos = getPosition (); Size size = getSprite ()-> getContentSize (); return CCRectMake (pos. x, pos. y, size. width, size. height);} bool Entity: isDead () {return this-> _ isDead;} // empty function, left to the subclass void Entity: onDead () {} void Entity :: onBindSprite () {}void Entity: onHurt (int hurtValue) {}</span>
We can see that it has some attributes, as well as functions such as injury, death, and genie. Different functions are implemented for different entities.

TowerBorder. h

<Span style = "font-size: 14px;"> class TowerBorder: public Entity {public: TowerBorder ();~ TowerBorder (); CREATE_FUNC (TowerBorder); virtual bool init (); bool isClickMe (Point pos); // check whether the gun station is clicked private :}; </span>
. Cpp

<span style="font-size:14px;">TowerBorder::TowerBorder(){}TowerBorder::~TowerBorder(){}bool TowerBorder::init(){__String* sFilePath = __String::createWithFormat("sprite/hero/border_%d.png",_level);Sprite* sprite = Sprite::create(sFilePath->getCString());bindSprite(sprite);ActionInterval* rotateBy = RotateBy::create(25.0f,360,360);auto repeat = RepeatForever::create(rotateBy);sFilePath = __String::createWithFormat("sprite/hero/magic_border_%d.png",_level);sprite = Sprite::create(sFilePath->getCString());sprite->setOpacity(80);sprite->runAction(repeat);this->addChild(sprite);return true;}bool TowerBorder::isClickMe(Point pos){Size size = getSprite()->getContentSize();Point borderPos = getPosition();Point srcPos = Point(borderPos.x-size.width,borderPos.y+size.height);Point detPos = Point(borderPos.x+size.width,borderPos.y-size.height);if(pos.x>=srcPos.x && pos.x<=detPos.x && pos.y<=srcPos.y && pos.y>=detPos.y){return true;}return false;}</span>
Then test: Add a member in HeroManager:

<span style="font-size:14px;">Vector<TowerBorder*> m_towerBorderList;void createTowerBorder();</span>
Then, after createTowerPos, we call createTowerBorder, that is, adding the fort by clicking

<Span style = "font-size: 14px;"> void HeroManager: createTowerPos (int curLevel) {/******************* omitting the code ***************/// ** 6 ** after loading all vertices, then you can add the turret createTowerBorder ();} void HeroManager: createTowerBorder () {PosBase * tPos = NULL; for (auto ref: m_towerPosList) {tPos = dynamic_cast <PosBase *> (ref); if (tPos! = NULL) {TowerBorder * border = TowerBorder: create (); border-> setPosition (tPos-> getPos (); this-> addChild (border ); m_towerBorderList.pushBack (border) ;}}</span>
Change the adjust mode of the previous coordinates to false, and the transparency of the background image is not designed. Run the test as follows:


Next, click "add Hero" to add a hero. Let's take a look at the hero.

<span style="font-size:14px;">class Hero : public Entity{public:Hero(); ~Hero();static Hero* createFromCsvFileByID(int heroID);bool initFromCsvFileByID(int heroID);};</span>
Here we add them based on the ID, that is, there are different types of heroes. But there is only one kind of this game-you can also expand it by yourself.

<span style="font-size:14px;">Hero::Hero(){}Hero::~Hero(){}Hero* Hero::createFromCsvFileByID(int heroID){Hero* hero = new Hero();if(hero && hero->initFromCsvFileByID(heroID)){hero->autorelease();}else{CC_SAFE_DELETE(hero);}return hero;}bool Hero::initFromCsvFileByID(int heroID){Sprite* sprite = Sprite::create(__String::createWithFormat("sprite/hero/hero_%d.png",heroID)->getCString());bindSprite(sprite);return true;}</span>
Then we understand the process of adding a hero: Click any fort and then add it. However, we need to solve the problem by clicking the turret?

Add functions in HeroManager:

<span style="font-size:14px;">TowerBorder* findClickTowerBorder(Point pos){TowerBorder* tBorder = NULL;for(auto ref : m_towerBorderList){tBorder = dynamic_cast<TowerBorder*>(ref);if(tBorder){if(tBorder->isClickMe(pos)){return tBorder;}}}return NULL;}</span>
At the same time, a gun has only one hero, so Add the following in TowerBorder:

<span style="font-size:14px;">Hero* _hero;TowerBorder::TowerBorder(){_hero = NULL;}TowerBorder::~TowerBorder(){CC_SAFE_RELEASE(_hero);}Hero* getHero(){ return _hero;};void bindHero(Hero* hero){ _hero = hero; };</span>
Then, in the end function of the touch event of the Manager:

<Span style = "font-size: 14px;"> bool HeroManager: initWithLevel (int curLevel) {/*****************************/listener-> onTouchEnded = [=] (Touch * touch, event * event) {/***************************/TowerBorder * clickBorder = findClickTowerBorder (pos ); if (clickBorder = NULL) {return;} if (clickBorder-> getHero () = NULL) {Hero * hero = Hero: createFromCsvFileByID (1 ); hero-> setPosition (clickBorder-> getPosition (); this-> addChild (hero); clickBorder-> bindHero (hero );}}; ************************* ************/} </span>
In the test, click the bastion host to add a hero. The effect is as follows:



Iii. Preview in the next section

Let's take a look at how to add the hero attribute in Csv to the hero --- read the Csv & Parse Csv file, add more operations to the hero, upgrade it, and delete it from the gun.


--------------------------------

IV:Source code & Resources

-----------------------------------

Personal ignorance. Thank you for your correction and discussion.


Cocos2d-x 30 game engine, what language to edit the game

Supports c ++, lua, and javascript. The underlying layer is implemented in c ++, and the upper layer can be bound to two other scripting languages.
 

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.