使用Cocos2d-x-3.0游戏引擎。编写一个塔防游戏 part05

Previous:http://www.cocoachina.com/bbs/read.php?tid=203063

5.5 TutorialScene.h

把这些头文件添加进TutorialScene.h里面:

#include "cocos2d.h"
#include "Creep.h"
#include "WayPoint.h"
#include "Wave.h"
//#include "GameHUD .h" // 稍后处理.
```


and .cpp.

//#include "Tower.h" // 稍后处理
#include "DataModel.h"
#include 
#include 

```


In the TutorialScene.h
Class TutorialScene:public cocos2d::Layer
{
public:
// 其他代码...
    int currentLevel;
    void addWayPoint();
    void addWaves();
    void FollowPath(Node *sender);
    void gameLogic(float dt);
    void addTarget();
    
    virtual void update(float dt);
    Wave* getCurrentWave();
    Wave* getNextWave();
// 其他代码...
}
```


In the TutorialScene.cpp
bool TutorialScene::init()
{
   ……
   // 之前写的那些代码.
   ……
    this->addWayPoint();
    this->addWaves();

    this->scheduleUpdate();
    this->schedule(schedule_selector(TutorialScene::gameLogic), 1.0f);
    this->currentLevel = 0;

……
    return true; 
}

void TutorialScene::FollowPath(Node *sender)
{
    Creep *creep = (Creep *)sender;

    WayPoint *waypoint = creep->getNextWaypoint();

    int moveDuration = creep->moveDuration;
    auto actionMove = MoveTo::create(moveDuration, waypoint->getPosition());
    auto actionMoveDone = CallFuncN::create(this, callfuncN_selector(TutorialScene::FollowPath));
    creep->stopAllActions();
    creep->runAction(Sequence::create(actionMove, actionMoveDone, NULL));
}
```


// Here we read the value from the class ‘creep’. And we make the enemy move. 
    // 这里我们从类‘Creep’中读取了相应的值。并且让敌人开始移动。

void TutorialScene::addWaves()
{
    DataModel *m = DataModel::getModel();

    Wave *wave = NULL;
    
    wave = Wave::create()->initWithCreep(FastRedCreep::creep(), 0.3, 75);
    m->waves.pushBack(wave);
    wave = NULL;

    wave = Wave::create()->initWithCreep(StrongGreenCreep::creep(), 1.0, 10);
    m->waves.pushBack(wave);
    wave = NULL;
}
```


//Here, we set the parameters for the class ‘Wave’ about the creep type, spawn rate and the number of the creep.
//这里,我们为类‘Wave’设定关于不同类型敌人、重生速度和敌人总量的参数。

void TutorialScene::addWayPoint()
{
    DataModel *m = DataModel::getModel();
    auto *objects = this->_tileMap->objectGroupNamed("Objects");
    WayPoint *wp = NULL;

    std::string stringWithFormat = "Waypoint";
    int wayPointCounter = 0;
    ValueMap wayPoint;
    wayPoint = objects->objectNamed(stringWithFormat + std::to_string(wayPointCounter));

    while (wayPoint.begin() != wayPoint.end())
    {
        int x = wayPoint.at("x").asInt();
        int y = wayPoint.at("y").asInt();
        wp = WayPoint::create();
        wp->setPosition(ccp(x, y));
        m->waypoints.pushBack(wp);
        wayPointCounter++;
        wayPoint = objects->objectNamed(stringWithFormat + std::to_string(wayPointCounter));
    }
    wp =NULL;
}

void TutorialScene::addTarget()
{
    DataModel *m = DataModel::getModel();
    Wave *wave = this->getCurrentWave();

    if (wave->totalCreeps < 0) 
    {
        return;
    }
    wave->totalCreeps--;

    Creep *target = NULL;
    int random = CCRANDOM_0_1() * 2;
    if (random == 0)
    {
        target = FastRedCreep::creep();
    }  
    else
    {
        target = StrongGreenCreep::creep();
    }

    WayPoint *waypoint = target->getCurrentWaypoint();
    target->setPosition(waypoint->getPosition());
    waypoint = target->getNextWaypoint();
    this->addChild(target, 1);

    int moveDuration = target->moveDuration;
    auto actionMove = CCMoveTo::create(moveDuration, waypoint->getPosition());
    auto actionMoveDone = CallFuncN::create(this, callfuncN_selector(TutorialScene::FollowPath));

    target->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
    target->tag = 1;
    m->targets.pushBack(target);
    
}

void TutorialScene::gameLogic(float dt)
{
    DataModel *m = DataModel::getModel();
    Wave *wave = this->getCurrentWave();
    static double lastTimeTargetAdded = 0;

    double now = 0;
    if (lastTimeTargetAdded == 0 || now - lastTimeTargetAdded >= wave->spawnRate)
    {
        this->addTarget();
        lastTimeTargetAdded = now;
    }
}

void TutorialScene::update(float dt)
{

}

Wave *TutorialScene::getCurrentWave()
{
    DataModel *m = DataModel::getModel();
    Wave *wave = (Wave *)m->waves.at(this->currentLevel);

    return wave;
}

Wave *TutorialScene::getNextWave()
{
    DataModel *m = DataModel::getModel();

    this->currentLevel++;
    if (this->currentLevel > 1)
    {
        this->currentLevel = 0;
    }

    Wave *wave = (Wave *)m->waves.at(this->currentLevel);

    return wave;
}
```


Let us see the result:
效果展示:
  
 
They are coming !


未完待续~(一日一更)

Next:  http://www.cocoachina.com/bbs/read.php?tid=203510

THX … :2:

首先感谢楼主分享,有个疑问:gameLogic函数是不是有点问题?变量now和lastTimeTargetAdded始终都是0哦

请教楼主:在Wave *wave = (Wave *)m->waves.at(this->currentLevel);这里崩了,是怎么回事呢?
这是输出的信息:cocos2d: Assert failed: index out of range in getObjectAtIndex()
Assertion failed: (index >= 0 && index < size()), function at, file /Users/apple/Desktop/android/cocos2d-x-3.0/projects/TaFang/proj.ios_mac/…/cocos2d/cocos/base/CCVector.h, line 212.

超出了数组范围。。你用CCLOG(“%f”, XX)调试下,看哪个数组的值超了。

CCLOG("%zd",m->waves.size());输出为0

std::to_string 在移植android的过程中报错。
:to_string is not a menber of std咋回事捏?

bug 一大堆
前面遍地图的时候,对象层里路径点是Waypoing00,到了这里变 Waypoint了
还有.cpp内应该是void Tutorial::*** 实现方法,而不是void TutorialScene::****