package oop13.space.controller;
import oop13.space.model.IModel;
import oop13.space.utilities.GameStrings;
import oop13.space.views.GameOver;
import oop13.space.views.GameWon.GameWonObserver;
import oop13.space.views.GamePanel;
import oop13.space.views.GameWonInterface;
import oop13.space.views.MainFrameInterface;
/**
* Controller handling the interaction with the
* {@link oop13.space.views.GameWonInterface} view.
*
* @author Mattia Capucci
*/
public class GameWonController implements GameWonObserver {
private MainFrameInterface mainFrame;
private GameWonInterface gameWon;
private IModel model;
/**
* Creates a new GameWonController 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 GameWonController(MainFrameInterface mainFrame, IModel model) {
this.mainFrame = mainFrame;
this.model = model;
}
/**
* Sets the {@link oop13.space.views.GameWonInterface} view the controller
* has to observe.
*
* @param gameWon - The {@link oop13.space.views.GameWonInterface} to observe.
*/
public void setView(GameWonInterface gameWon) {
this.gameWon = gameWon;
this.gameWon.attachObserver(this);
this.gameWon.getScoreLabel().setText(GameStrings.SCORE + model.getStatistics().getScore());
}
@Override
public void goToNextLevel() {
int oldLives = this.model.getShip().getLives();
this.model.resetGame();
this.model.getShip().setLives(oldLives);
this.model.getShip().increaseLives();
this.model.initIndividuals();
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 endGame() {
GameOver gameOver = new GameOver();
GameOverController gameOverController = new GameOverController(this.mainFrame, this.model);
gameOverController.setView(gameOver);
this.mainFrame.replacePanel(gameOver);
}
}