void startGame(Missile missile, Enemy enemy){ cout<< "do you want to launch a missile?(y/n)"<<endl; char check; cin>>check; while(check!='n'){ if(check=='y'){ askWhereX(missile.getPosition()); askWhereY(0); checkIfEnemyThere(missile.getPosition(), enemy); cout<<"Would you like to launch another?"<<endl; cin>>check; }else{
void MissileMovementLoop::update(float delta){ Missile* parent = (Missile*)getOwner(); if(parent==NULL) { return; }else{ auto moveDown = MoveTo::create(missileFlyDuration, Point(0,parent->getPosition().x)); parent->runAction(moveDown); } }
//////// Missile tracks player and follows them //////// //////// Has a limited turning radius. //////// //////// Has a limited lifetime until it //////// //////// explodes //////// void AI::enemyMissUpdate(Missile &proj, GSP420::ABC *player, const float dt) { D3DXVECTOR3 currDir, newDir; // Get the missiles current direction from its velocity currDir = proj.getVelocity(); D3DXVec3Normalize(&currDir, &currDir); // Get the missiles new direction according to player position newDir = player->getPosition() - proj.getPosition(); D3DXVec3Normalize(&newDir, &newDir); // Find the angle between the two vectors float angle = D3DXVec3Dot(&currDir, &newDir); angle = acos(angle); // Re-use newDir vector for updating projectile velocity newDir = proj.getVelocity(); D3DXMATRIX rotMat; // Check if angle is larger than our missile's turning radius // Then update the missiles velocity by rotating it in the // direction of the angle if (abs(angle) > ENEMY_MISSILE_TURN_RADIUS) { if (angle > 0.0f) { D3DXMatrixRotationY(&rotMat, ENEMY_MISSILE_TURN_RADIUS); newDir.x = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); newDir.y = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); newDir.z = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); proj.setVelocity(newDir); } else { D3DXMatrixRotationY(&rotMat, -ENEMY_MISSILE_TURN_RADIUS); newDir.x = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); newDir.y = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); newDir.z = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); proj.setVelocity(newDir); } } else { D3DXMatrixRotationY(&rotMat, angle); newDir.x = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); newDir.y = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); newDir.z = (rotMat._11 * newDir.x) + (rotMat._12 * newDir.y) + (rotMat._13 * newDir.z); proj.setVelocity(newDir); }
//////// Missile locks on to closest enemy in front of player //////// //////// Missile tracks enemy aggressively //////// void AI::playerMissUpdate(Missile &proj, std::list<Enemy> *enemies, GSP420::ABC *player) { // If missile has no target, get one if (proj.getEnemyTarget() == NULL) { float closestY = -100.0f; // Search through the enemy list for the one closest // to the player within a set field of view std::list<Enemy>::iterator enemyIt = enemies->begin(); while (enemyIt != enemies->end()) { if (enemyIt->getPosition().x > player->getPosition().x - 5.0f && enemyIt->getPosition().x < player->getPosition().x + 5.0f && enemyIt->getPosition().y > closestY) { closestY = enemyIt->getPosition().y; proj.setEnemyTarget(&*enemyIt); } enemyIt++; } } else // Otherwise track the enemy target { D3DXVECTOR3 newVel; Enemy *enemy = proj.getEnemyTarget(); // Find the updated direction for the missile based on enemy position newVel = enemy->getPosition() - proj.getPosition(); D3DXVec3Normalize(&newVel, &newVel); // Multiply direction vector by missile speed to obtain missile velocity newVel *= ENEMY_MISSILE_SPEED; proj.setVelocity(newVel); } }