void AttackTower::shoot(float dt) { GameManager *instance = GameManager::getInstance(); auto bulletVector = instance->bulletVector; checkNearestEnemy(); if(nearestEnemy!=NULL && nearestEnemy->getCurrHp() > 0 ) { auto currBullet = AttackTowerBullet(); instance->bulletVector.pushBack(currBullet); auto moveDuration = getRate(); Point shootVector = nearestEnemy->sprite->getPosition() - this->getPosition(); Point normalizedShootVector = -shootVector.normalize(); auto farthestDistance = Director::getInstance()->getWinSize().width; Point overshotVector = normalizedShootVector * farthestDistance; Point offscreenPoint = (currBullet->getPosition() - overshotVector); currBullet->runAction(Sequence::create(MoveTo::create(moveDuration, offscreenPoint), CallFuncN::create(CC_CALLBACK_1(AttackTower::removeBullet, this)), NULL)); currBullet = NULL; } }
void ArrowTower::rotateAndShoot(float dt) { checkNearestEnemy(); if (nearestEnemy != NULL) { auto rotateVector = nearestEnemy->getPosition() - this->getPosition(); float rotateradians = rotateVector.getAngle(); float rotateDegrees = CC_RADIANS_TO_DEGREES(-1 * rotateradians); float speed = 0.5 / M_PI; float rotateDuration = fabs(rotateradians * speed); rotateArrow->runAction(Sequence::create(RotateTo::create(rotateDuration, rotateDegrees), CallFunc::create(CC_CALLBACK_0(ArrowTower::shoot, this)),NULL)); } }
void ArrowTower::rotateAndShoot(float dt) { checkNearestEnemy(); if (nearestEnemy != NULL) { shoot(); } }
void ArtilleryTower::rotateAndShoot(float dt) { checkNearestEnemy(0.5); if (nearestEnemy != nullptr && nearestEnemy->getIsVisible()&&!isSilenced) //找到射击范围内最近的敌人 { Point enemyAndTowerVector = nearestEnemy->getPosition() - this->getPosition(); //得到敌人与本炮塔之间的向量 float rotateRadians = enemyAndTowerVector.getAngle(); //得到该向量与x轴的夹角 弧度制 float rotateDegrees = CC_RADIANS_TO_DEGREES(-1 * rotateRadians); float speed = 0.5 / M_PI; //表示pi,是一个宏 float rotateDuration = fabs(speed * rotateRadians);//得到炮塔旋转时间,注意绝对值 //log("enemyPos: %f %f", nearestEnemy->getPosition().x, nearestEnemy->getPosition().y); //log("towerPos: %f %f", this->getPosition().x, this->getPosition().y); rotateArtillery->runAction(Sequence::create(RotateTo::create(rotateDuration, rotateDegrees-90)/*, CallFuncN::create(CC_CALLBACK_0(ArtilleryTower::shoot, this))*/, NULL)); //先旋转再开火,注意角度要减90度才能对应上炮管 //分开使用,开火速率不同于旋转速率 } }