AI::AI(QObject *parent, BattleField *field) : QObject(parent), _field(field) { reset(); connect(_field, &BattleField::numberOfShipsChanged, [&](){ if(shipActive()) { setShipActive(false); _shipSunken = true; _currentHitsOnShip = 0; _shipFirstHit = -1; setLastDirection(randomDirection()); } }); }
void AI::setLastHit(bool lastHit) { _lastHit = lastHit; if(_lastHit) { Settings::Difficulty diff = Settings::instance()->difficulty(); if(!_shipSunken && (diff == Settings::Better)) { if((_currentHitsOnShip == 0) && (_shipFirstHit < 0)) _shipFirstHit = _alreadyTried.last(); _currentHitsOnShip++; setShipActive(true); } qDebug() << "Got a hit..."; } }
//Timer interrupt for the game. void timer_interrupt_handler() { if(!paused) { secondTimer++; alienMarchTimer++; bulletMoveTimer++; controllerTimer++; if(getShipAlive()) shipMoveTimer++; } tankMoveTimer++; //Poll the buttons if(tankMoveTimer >= TANK_SPEED) { handleButton(currentButtonState); handleSwitches(currentSwitchesState); } if(controllerTimer >= POLL_CONTROLLER_TIMER) { controllerTimer = 0; //readController(); } //Advance the aliens and fire bullets if(alienMarchTimer >= ALIEN_MARCH_SPEED) { alienMarch(); if(rand() % BULLET_CHANCE == 0) { fireAlienBullet(); } alienMarchTimer = 0; } //Advance the bullets if(bulletMoveTimer >= BULLET_SPEED) { bulletMove(); bulletMoveTimer = 0; } //Move the saucer if it is active if(getShipActive()&&!getPaused()) { setSound(ufolowpitch_getSound(),ufolowpitch_getNumFrames(), SAUCER_PRIORITY); if(shipMoveTimer >= SHIP_MOVE_MAX_TIMER) { shipMoveTimer = 0; marchShip(); } } //If the saucer is killed flash the score animation if(getMothershipKilled() && saucerFlash == 0) { saucerFlash = 1; } //If the saucer is killed flash the score animation if(saucerFlash > 0) { //Paint score if(secondTimer == HALF_SECOND) { setSound(ufohighpitch_getSound(),ufohighpitch_getNumFrames(), SAUCER_DEATH_PRIORITY); paintShipScore(1); saucerFlash++; } //paint black if(secondTimer == SECOND_TIMER_MAX) { paintShipScore(0); saucerFlash++; } if(saucerFlash >= 7) { paintShipScore(0); saucerFlash = 0; setMothershipKilled(0); if(getShipDirection()) { setShipPos(-SHIP_WIDTH); setShipDirection(1); } else { setShipPos(SCREEN_WIDTH + SHIP_WIDTH); setShipDirection(0); } } } //Spawn the saucer every X seconds if(saucerTime == 10 && !paused) { saucerTime = 0; if(!getShipActive() && saucerFlash == 0) { setShipActive(1); setShipAlive(1); setMothershipKilled(0); } } //Tic the clock one second if(secondTimer >= SECOND_TIMER_MAX) { secondTimer = 0; gameRunTime++; saucerTime++; } }