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.hClass 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.cppbool 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

