package oop13.space.controller;
import java.util.List;
import oop13.space.model.Achievement;
import oop13.space.model.IModel;
import oop13.space.utilities.GameStrings;
import oop13.space.utilities.ListIOManager;
import oop13.space.views.AchievementsGUI;
import oop13.space.views.Credits;
import oop13.space.views.GamePanel;
import oop13.space.views.HighScoreGUI;
import oop13.space.views.MainFrameInterface;
import oop13.space.views.MainMenu;
import oop13.space.views.MainMenu.MainMenuObserver;
/**
* Class implementing the main space invaders controller.
* It handles the interaction with the {@link oop13.space.views.MainMenu}
* view and permitted to start a new game,
* see the list of the high {@link oop13.space.model.Score},
* see the list of {@link oop13.space.model.Achievement}
* and see the credits of the game
*
* @author Mattia Capucci
*/
public class MainMenuController implements MainMenuObserver {
private MainFrameInterface mainFrame;
private IModel model;
/**
* Creates a new MainMenuController with the
* {@link oop13.space.model.IModel} provided in input.
*
* @param model - The data used by the controller.
*/
public MainMenuController(IModel model) {
this.model = model;
}
/**
* Sets the {@link oop13.space.views.MainFrameInterface} view
* the controller has to observe.
*
* @param mainFrame - The {@link oop13.space.views.MainFrameInterface} to observe.
*/
public void setView(MainFrameInterface mainFrame) {
this.mainFrame = mainFrame;
MainMenu mainMenu = this.mainFrame.getMainMenu();
mainMenu.attachObserver(this);
}
@Override
public void startGameCmd() {
this.model.resetGame();
this.model.getStatistics().resetStatistics();
this.model.initIndividuals();
this.model.initAchievements();
GameController gameController = new GameController(this.mainFrame, this.model);
GamePanel gamePanel = new GamePanel(this.model.getList());
gamePanel.getScoreLabel().setText(GameStrings.SCORE + model.getStatistics().getScore());
gamePanel.getLivesLabel().setText(GameStrings.LIVES + model.getShip().getLives());
gameController.setView(gamePanel);
gamePanel.requestFocusInWindow();
gamePanel.addKeyListener(new ShipController(this.model));
this.mainFrame.replacePanel(gamePanel);
}
@Override
public void seeHighScoresCmd() {
HighScoreGUI highScore = new HighScoreGUI();
HighScoreController highScoreController = new HighScoreController(this.mainFrame, this.model);
highScoreController.setView(highScore);
this.mainFrame.replacePanel(highScore);
}
@Override
public void seeAchievementsCmd() {
ListIOManager<Achievement> achManager = new ListIOManager<Achievement>(this.model.getAchievementsFile());
this.model.initAchievements();
List<Achievement> listAchievements = achManager.readList();
AchievementsGUI achievementsGUI = new AchievementsGUI(listAchievements);
AchievementsController achController = new AchievementsController(this.mainFrame, this.model);
achController.setView(achievementsGUI);
this.mainFrame.replacePanel(achievementsGUI);
}
@Override
public void seeCreditsCmd() {
Credits credits = new Credits();
CreditsController creditsController = new CreditsController(this.mainFrame);
creditsController.setView(credits);
this.mainFrame.replacePanel(credits);
}
}