package oop13.space.controller;
import java.awt.Color;
import java.io.File;
import java.util.List;
import javax.swing.JOptionPane;
import oop13.space.game.Game;
import oop13.space.model.Achievement;
import oop13.space.model.IModel;
import oop13.space.utilities.ListIOManager;
import oop13.space.utilities.GameStrings;
import oop13.space.views.GameOver;
import oop13.space.views.GamePanel.GamePanelObserver;
import oop13.space.views.GamePanelInterface;
import oop13.space.views.GameWon;
import oop13.space.views.MainFrameInterface;
/**
* Controller handling the interaction with the {@link oop13.space.game.Game}
* and the {@link oop13.space.views.GamePanelInterface} view.
*
* @author Mattia Capucci
* @author Manuel Bottazzi
*/
public class GameController implements GamePanelObserver {
private static final int ALIENS_KILLED_ACH = 100;
private static final int MOTHERSHIP_ALIENS_KILLED_ACH = 5;
private static final int LEVELS_COMPLETED_ACH = 3;
private static final int LEVELS_SURVIVOR_ACH = 1;
private static final int LIVES_SURVIVOR_ACH = 3;
private static final int LAST_LIFE = 1;
private MainFrameInterface mainFrame;
private GamePanelInterface gamePanel;
private IModel model;
/**
* Creates a new GameController with the parameters provided in input.
*
* @param mainFrame - The {@link oop13.space.views.MainFrameInterface} to use
* for changing the current panel displayed.
*
* @param model - The data used by the controller.
*/
public GameController(MainFrameInterface mainFrame, IModel model) {
this.mainFrame = mainFrame;
this.model = model;
}
/**
* Sets the {@link oop13.space.views.GamePanelInterface} view
* the controller has to observe and start the
* {@link oop13.space.game.Game} thread.
*
* @param gamePanel - The {@link oop13.space.views.GamePanelInterface} to observe
*/
public void setView(GamePanelInterface gamePanel) {
this.gamePanel = gamePanel;
this.gamePanel.attachObserver(this);
Game newGame = new Game(model, this);
newGame.start();
}
@Override
public void setShipLivesInPanel() {
int lives = this.model.getShip().getLives();
this.gamePanel.getLivesLabel().setText(GameStrings.LIVES + lives);
if (this.model.getShip().getLives() == LAST_LIFE) {
this.gamePanel.getLivesLabel().setForeground(Color.RED);
}
}
@Override
public void setScoreInPanel(int score) {
this.gamePanel.getScoreLabel().setText(GameStrings.SCORE + score);
}
@Override
public void updatePanel() {
this.gamePanel.gameChanged();
}
@Override
public void gameOver() {
GameOver gameOverGUI = new GameOver();
GameOverController gameOverController = new GameOverController(this.mainFrame, this.model);
gameOverController.setView(gameOverGUI);
this.mainFrame.replacePanel(gameOverGUI);
}
@Override
public void gameWon() {
GameWon gameWonGUI = new GameWon();
GameWonController gameWonController = new GameWonController(this.mainFrame, this.model);
gameWonController.setView(gameWonGUI);
this.mainFrame.replacePanel(gameWonGUI);
}
@Override
public void checkAchievements() {
int aliensKilled = model.getStatistics().getAliensKilled();
int motherShipsKilled = model.getStatistics().getMotherShipsKilled();
int levelsCompleted = model.getStatistics().getLevelsCompleted();
File achievementsFile = this.model.getAchievementsFile();
ListIOManager<Achievement> achManager = new ListIOManager<Achievement>(achievementsFile);
List<Achievement> listAchievements = achManager.readList();
for (Achievement ach : listAchievements) {
if (aliensKilled >= ALIENS_KILLED_ACH) {
if (!ach.isCompleted() && ach.getName().equals(GameStrings.ACH_100_ALIENS)) {
JOptionPane.showMessageDialog(null, GameStrings.ACH_ALIENS_COMPLETED, GameStrings.CONGRATULATIONS,
JOptionPane.INFORMATION_MESSAGE);
ach.setCompleted();
}
}
if (motherShipsKilled >= MOTHERSHIP_ALIENS_KILLED_ACH) {
if (!ach.isCompleted() && ach.getName().equals(GameStrings.ACH_5_MOTHERSHIPS)) {
JOptionPane.showMessageDialog(null, GameStrings.ACH_MOTHERSHIPS_COMPLETED, GameStrings.CONGRATULATIONS,
JOptionPane.INFORMATION_MESSAGE);
ach.setCompleted();
}
}
if (levelsCompleted >= LEVELS_COMPLETED_ACH) {
if (!ach.isCompleted() && ach.getName().equals(GameStrings.ACH_3_LEVELS)) {
JOptionPane.showMessageDialog(null, GameStrings.ACH_LEVELS_COMPLETED, GameStrings.CONGRATULATIONS,
JOptionPane.INFORMATION_MESSAGE);
ach.setCompleted();
}
}
if (model.getShip().getLives() >= LIVES_SURVIVOR_ACH && levelsCompleted == LEVELS_SURVIVOR_ACH) {
if (!ach.isCompleted() && ach.getName().equals(GameStrings.ACH_SURVIVOR)) {
JOptionPane.showMessageDialog(null, GameStrings.ACH_SURVIVOR_COMPLETED, GameStrings.CONGRATULATIONS,
JOptionPane.INFORMATION_MESSAGE);
ach.setCompleted();
}
}
}
achManager.writeList(listAchievements);
}
}