兵营塔可以产生三个士兵,可以设置士兵集结点,当周围有敌人通过时,士兵会拦下敌人,并与敌人进行搏斗。每隔N秒,会回复士兵血量,可以升级,有更多的兵种。
去其他防御塔相比,兵营塔在建造完毕,即上一章节中的buildingSmokeAnimation执行完毕后创建3个士兵,给士兵设置集结点,由士兵自行走到集结点。
setRallyPoint是重新设置集结点,士兵会停止一切动作,走到新的集结点(runToLocation()方法)。
void BaseBarracksTower::setRallyPoint(Point point) { auto rally = point - this->getParent()->getPosition(); Soldiers.at(0)->setLocation(Point(rally.x + 20,rally.y - 20)); Soldiers.at(1)->setLocation(Point(rally.x - 20,rally.y - 20)); Soldiers.at(2)->setLocation(Point(rally.x,rally.y + 20)); for(int i = 0; i< Soldiers.size()&& Soldiers.at(i)!=NULL && Soldiers.at(i)->getState()!=SoldierStateDeath;i++) { if(Soldiers.at(i)->nearestMonster!=NULL && Soldiers.at(i)->nearestMonster->getState()!=stateDeath) Soldiers.at(i)->nearestMonster->restartWalking(); Soldiers.at(i)->runToLocation(Soldiers.at(i)->getLocation()); } }
cureSoldiers(float dt)是设置在每隔dt时间,对士兵进行一次治疗,若有死掉的士兵,自动制造一名新的士兵。
void BaseBarracksTower::cureSoldiers(float dt) { for(int i = 0;i<Soldiers.size();i++)//遍历士兵 { auto Soldier = Soldiers.at(i); if(Soldier->getState() == SoldierStateWait){//若士兵没有处于攻击状态 Soldier->setCurrHp(Soldier->getMaxHp());//回复士兵所有血量 Soldier->setHpPercentage(100); Soldier->getHpBar()->setPercentage(100); } else if(Soldier->getState() == SoldierStateDeath){//若有士兵死亡 if(level <= 3){ SoundManager::playBarrackOpendoor();//播放兵营开门声音以及动画 door->runAction(Animate::create(AnimationCache::getInstance()->getAnimation(String::createWithFormat("level%d_barracks_ope ndoor",getLevel())->getCString()))); auto point = Soldier->getLocation(); Soldier ->removeFromParent(); Soldier = NULL;//将死亡的士兵清除 auto newSoldier = BarrackSoldier::createSoldier(point,level);//创建新的士兵 Soldiers.replace(i,newSoldier);//更新士兵队列 addChild(newSoldier); }//省略。。。。。 } }
士兵塔主要工作都放在士兵类当中,士兵塔只负责根据士兵的状态更新士兵
士兵塔的升级界面略有不同,多了一个旗帜用于设置集结点,代码部分在UpdatePanleLayerWithFlag中
auto flaglistener = EventListenerTouchOneByOne::create(); flaglistener->onTouchBegan = [&](Touch* touch,Event* event){ auto target = static_cast<Sprite*>(event->getCurrentTarget()); Point locationInNode = target->convertTouchToNodeSpace(touch); Size size = target->getContentSize(); Rect rect = Rect(0,size.width,size.height); if (rect.containsPoint(locationInNode)) { return true; } return false; }; flaglistener->onTouchEnded = [&](Touch* touch,Event* event){ static_cast<TouchLayer*>(this->getParent())->tower = this->getTower(); static_cast<TouchLayer*>(this->getParent())->setRallyFlagTouchShield(); tower->isUpdateMenuShown = false; this->removeFromParent(); };
当点击旗帜后,清除升级菜单,通知TouchLayer显示集结点范围,并且创建监听事件
void TouchLayer::setRallyFlagTouchShield() { rallyCircle = RallyCircle::create(); rallyCircle->setPosition(tower->getParent()->getPosition()); rallyCircle->setTag(1001); addChild(rallyCircle); listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(TouchLayer::onRallyFlagTouchBegan,this); listener->onTouchEnded = CC_CALLBACK_2(TouchLayer::onRallyFlagTouchEnded,this); listener->setSwallowTouches(true); _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this); }当点击在范围内,显示集结点旗帜,重设士兵塔的集结点即setRallyPoint方法
bool TouchLayer::onRallyFlagTouchBegan(Touch* touch,Event* event) { auto target = static_cast<TouchLayer*>(event->getCurrentTarget()); Point locationInNode = target->convertTouchToNodeSpace(touch); if(locationInNode.distance(tower->getParent()->getPosition())<160 && locationInNode.distance(tower->getParent()->getPosition())>80){ addRallyFlag(locationInNode); tower->setRallyPoint(locationInNode); SoundManager::playRallyPointPlaced(); isFlag = true; }else{ addWrongPlace(static_cast<TouchLayer*>(event->getCurrentTarget())->convertTouchToNodeSpace(touch)); } return true; }以上即士兵塔基本实现方法。 原文链接:https://www.f2er.com/cocos2dx/340912.html