package fr.enssat.BoulderDash.controllers; import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.views.MenuView; import fr.enssat.BoulderDash.views.GameView; import java.awt.event.ActionEvent; import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER; /** * GameController *

* This system creates the view. * The game loop is also handled there. * * @author Colin Leverger * @since 2015-06-19 */ public class GameController extends AbstractNavController { private boolean firstClickOnPause; private MenuView menuView; private GameView gameView; /** * Class constructor * * @param levelModel Level model * @param nav */ public GameController(LevelModel levelModel, NavigationBetweenViewController nav) { super(levelModel, nav); this.firstClickOnPause = true; this.gameView = new GameView(this, levelModel); this.menuView = nav.getMenuView(); AUDIO_LOAD_HELPER.stopMusic(); AUDIO_LOAD_HELPER.playSound("new"); } /** * Handles the 'action performed' event * * @param event Action event */ public void actionPerformed(ActionEvent event) { switch (event.getActionCommand()) { case "pause": if (this.firstClickOnPause) { this.levelModel.setGamePaused(true); } else if (!this.firstClickOnPause) { this.levelModel.setGamePaused(false); } this.firstClickOnPause = !this.firstClickOnPause; this.gameView.getGameFieldView().grabFocus(); break; case "restart": this.resetGame("restart"); AUDIO_LOAD_HELPER.playSound("new"); this.gameView.getGameFieldView().grabFocus(); break; case "menu": this.menuView.setVisible(true); AUDIO_LOAD_HELPER.startMusic("game"); this.resetGame("menu"); break; } } /** * Function to reset the game */ private void resetGame(String source) { this.gameView.dispose(); if (source.equals("restart")) { this.levelModel = new LevelModel(this.nav.getPickedLevelIdentifier()); this.gameView = new GameView(this, levelModel); this.gameView.setVisible(true); } } /** * Return the game view * * @return gameView */ public GameView getGameView() { return gameView; } // dead code // //public void setGameView(GameView gameView) { // this.gameView = gameView; //} }