void Enemy::shoot() { Vec2f targetPos = target->get_pos(); aimAt(targetPos.x,targetPos.y); Vec2f pos = body.get_position(); missile.set_angle(turret_angle); missile.set_pos(pos); missile.fire(); time_since_last_shoot=0; }
void Enemy::update(int time_elapsed_ms) { enemy.update(time_elapsed_ms); float time_elapsed = time_elapsed_ms * 0.001f; time_since_last_shoot+=time_elapsed_ms; pos = body.get_position(); if(time_since_last_shoot>2000) { shoot(); } else { Vec2f tpos = target->get_pos(); aimAt( tpos.x,tpos.y); } if(is_dead || pos.x<-30) { remove_enemy(this); game->add_for_deletion(this); update_callback.clear(); } }
void ShootingAI::think(double dt) { // Whether or not the ship should fire. bool shouldFire = false; // If the ShootingAI isn't enabled, or there's no gamestate, skip shooting. if(!enabled || ship->gameState == NULL) { return; } /* Also, do we want to try and rechoose a target every think (ie frame)? * If not, it would make the shooter a little quicker, but may end up * shooting at bad targets (if the gun is pointed in the opposite direction, * it will take time to rotate it around to shoot the target, then * time to kill the asteroid, and throughout all this time a new asteroid * could have become a better target, or even a critical target) * * Just an idea, we will talk it over. */ if (targetID != -1) { target = ship->gameState->custodian[targetID]; } /* If there is no target, or we need to choose a target b/c the old one * went off screen, or if the timer is up and we're allowed to switch * targets. */ if (target == NULL || needToChooseTarget || (targetSwitchTimer.isRunning && targetSwitchTimer.getTimeLeft() <= 0)) { target = chooseTarget(); // If there was nothing on screen, target could have wound up NULL if (target != NULL) { targetID = target->id; targetSwitchTimer.restartCountDown(); // If the chosen target is a Shard, remember that till the next time we choose a target. if (target->type == TYPE_SHARD) targetIsAShard = true; else targetIsAShard = false; } } // If it's been more than weaponSwitchSpeed seconds, we can switch weapons. // Don't bother choosing a weapon if target is NULL if (target != NULL && weaponSwitchTimer.isRunning && weaponSwitchTimer.getTimeLeft() <= 0) { prevWeapon = ship->getCurrentWeapon(); chooseWeapon(target); // If we chose a new weapon, reset the timer so we can't switch again soon. if (prevWeapon != chosenWeapon) { weaponSwitchTimer.restartCountDown(); } } // If we've got an actual target if (target != NULL) { // aimAt aims the cursos and returns whether or not the ship is ready to fire. shouldFire = aimAt(dt, target); // Make sure we don't fire incorrect weapons at shards. if (targetIsAShard) { // If the current weapon is not a tractor beam. if (ship->currentWeapon != TRACTOR_WEAPON_INDEX) { // Don't fire. shouldFire = false; } } ship->fire(shouldFire); } // Otherwise we've got no target else { // Aim the cursor in front of the ship aimCursorAtMiddle(dt); // Make sure we stop firing. ship->fire(false); } }