public void run() {
int counterTimer = 0;
int lives = model.getShip().getLives();
double motherSpawnMoment = Math.random() * MOTHERSHIP_SPAWN_FREQ;
AlienBlock alienBlock = model.getAliens();
Ship spaceShip = model.getShip();
List<Individual> individuals = model.getList();
alienBlock.startMove();
AudioPlayer.getAudioPlayer().playLoop(AudioPlayer.BACKGROUND_SOUND);
while (!this.isOver) {
counterTimer++;
if (counterTimer % SHOT_RATIO == 0) {
alienShot();
}
//Adds a mother ship to the game
if (counterTimer >= motherSpawnMoment) {
AlienMotherShip motherShip = model.addMotherShip();
motherShip.startMove();
motherSpawnMoment = MOTHERSHIP_BASE_FREQ + Math.random() * MOTHERSHIP_SPAWN_FREQ;
counterTimer = 0;
}
/* Moves all the individuals
* (except the aliens 'cause they already move).
* Checks all the possible collisions of all individuals
* Checks if some aliens are dead
* Sets lives and score in the game panel */
for (Individual ind : individuals) {
if (!(ind instanceof Alien)) {
ind.moveIndividual(ind.getPositionX(), ind.getPositionY());
}
ind.collideWith(individuals);
if (spaceShip.getLives() != lives) {
this.obs.setShipLivesInPanel();
lives--;
}
if (ind.isDead()) {
this.model.removeIndividual(ind);
if (ind instanceof Alien && !(ind instanceof AlienMotherShip)) {
AudioPlayer.getAudioPlayer().playOnce(AudioPlayer.ALIEN_KILLED_SOUND);
this.aliensKilled++;
}
if (ind instanceof AlienMotherShip) {
if (!((AlienMotherShip) ind).isOutOfLimit()) {
this.motherShipsKilled++;
}
}
this.obs.setScoreInPanel(this.model.getStatistics().getScore());
}
}
//Speed up the last alien survivor
if (this.aliensKilled >= KILLED_ALIENS_BEFORE_SPEEDUP) {
alienBlock.setSleepTime(ALIENS_SLEEP_TIME);
}
//Check game won
if (alienBlock.checkGameWon()) {
this.levelsCompleted++;
this.obs.gameWon();
this.isOver = true;
}
//Check game over
if (alienBlock.checkGameOver() || spaceShip.isDead()) {
//Stops the alien block's thread
if (!alienBlock.checkGameOver()) {
alienBlock.setGameOver();
}
spaceShip.setLives(0);
this.obs.gameOver();
this.isOver = true;
}