GameController, LevelModel

a) resetGame -> reset, dispose
b) getPickedLevelIdentifier -> LevelModel.getLevelName
This commit is contained in:
Daniel Langbein 2024-11-07 14:02:15 +01:00
parent aa32caaf42
commit 9625483e49
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
3 changed files with 109 additions and 108 deletions

View File

@ -2,7 +2,6 @@ package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper; import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import fr.enssat.BoulderDash.controllers.NavigationBetweenViewController;
import fr.enssat.BoulderDash.views.MenuView; import fr.enssat.BoulderDash.views.MenuView;
import fr.enssat.BoulderDash.views.GameView; import fr.enssat.BoulderDash.views.GameView;
@ -12,108 +11,114 @@ import java.awt.event.ActionListener;
/** /**
* GameController * GameController
* * <p>
* This system creates the view. * This system creates the view.
* The game loop is also handled there. * The game loop is also handled there.
* *
* @author Colin Leverger <me@colinleverger.fr> * @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class GameController implements ActionListener { public class GameController implements ActionListener {
private LevelModel levelModel; private LevelModel levelModel;
private AudioLoadHelper audioLoadHelper; private AudioLoadHelper audioLoadHelper;
private boolean firstClickOnPause; private boolean firstClickOnPause;
private MenuView menuView; private MenuView menuView;
private GameView gameView; private GameView gameView;
private NavigationBetweenViewController navigationBetweenViewController; private NavigationBetweenViewController navigationBetweenViewController;
/**
* Class constructor
*
* @param levelModel Level model
* @param navigationBetweenViewController
*/
public GameController(LevelModel levelModel, AudioLoadHelper audioLoadHelper, NavigationBetweenViewController navigationBetweenViewController) {
this.firstClickOnPause = true;
this.navigationBetweenViewController = navigationBetweenViewController;
this.levelModel = levelModel;
this.audioLoadHelper = audioLoadHelper;
this.gameView = new GameView(this, levelModel);
this.menuView = navigationBetweenViewController.getMenuView();
this.getAudioLoadHelper().stopMusic(); /**
this.getAudioLoadHelper().playSound("new"); * Class constructor
} *
* @param levelModel Level model
* @param navigationBetweenViewController
*/
public GameController(LevelModel levelModel, AudioLoadHelper audioLoadHelper, NavigationBetweenViewController navigationBetweenViewController) {
this.firstClickOnPause = true;
/** this.navigationBetweenViewController = navigationBetweenViewController;
* 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.levelModel = levelModel;
this.gameView.getGameFieldView().grabFocus(); this.audioLoadHelper = audioLoadHelper;
break; this.gameView = new GameView(this, levelModel);
this.menuView = navigationBetweenViewController.getMenuView();
case "restart": this.getAudioLoadHelper().stopMusic();
this.resetGame("restart"); this.getAudioLoadHelper().playSound("new");
this.getAudioLoadHelper().playSound("new"); }
this.gameView.getGameFieldView().grabFocus();
break; /**
* Handles the 'action performed' event
case "menu": *
this.menuView.setVisible(true); * @param event Action event
this.getAudioLoadHelper().startMusic("game"); */
this.resetGame("menu"); public void actionPerformed(ActionEvent event) {
break; switch (event.getActionCommand()) {
case "pause":
if (this.firstClickOnPause) {
this.levelModel.setGamePaused(true);
} else if (!this.firstClickOnPause) {
this.levelModel.setGamePaused(false);
} }
}
/** this.firstClickOnPause = !this.firstClickOnPause;
* Function to reset the game this.gameView.getGameFieldView().grabFocus();
*/ break;
private void resetGame(String source) {
this.gameView.dispose();
if(source.equals("restart")){
this.levelModel = new LevelModel(this.navigationBetweenViewController.getPickedLevelIdentifier(), audioLoadHelper);
this.gameView = new GameView(this, levelModel);
this.gameView.setVisible(true);
}
}
/** case "restart":
* Gets the audio load helper instance this.reset();
* this.getAudioLoadHelper().playSound("new");
* @return Audio load helper instance this.gameView.getGameFieldView().grabFocus();
*/ break;
public AudioLoadHelper getAudioLoadHelper() {
return this.audioLoadHelper; case "menu":
this.menuView.setVisible(true);
this.getAudioLoadHelper().startMusic("game");
this.dispose();
break;
} }
}
/** /**
* Return the game view * Reset the game
* @return gameView */
*/ private void reset() {
public GameView getGameView() { String pickedLevel = levelModel.getLevelName();
return gameView; dispose();
}
/** // Recreate game
* Set the gameView this.levelModel = new LevelModel(pickedLevel, audioLoadHelper);
* @param gameView this.gameView = new GameView(this, levelModel);
*/ this.gameView.setVisible(true);
public void setGameView(GameView gameView) { }
this.gameView = gameView;
} /**
* Dispose the game
*/
private void dispose() {
this.gameView.dispose();
}
/**
* Gets the audio load helper instance
*
* @return Audio load helper instance
*/
public AudioLoadHelper getAudioLoadHelper() {
return this.audioLoadHelper;
}
/**
* Return the game view
*
* @return gameView
*/
public GameView getGameView() {
return gameView;
}
// dead code
//
//public void setGameView(GameView gameView) {
// this.gameView = gameView;
//}
} }

View File

@ -104,23 +104,15 @@ public class NavigationBetweenViewController implements ActionListener {
// return menuView; // return menuView;
//} //}
/** // Replaced with LevelModel.getLevelName()
* Get the pickedLevel //
* //public String getPickedLevelIdentifier() {
* @return pickedLevelIdentifier Picked level identifier // return pickedLevelIdentifier;
*/ //}
public String getPickedLevelIdentifier() {
return pickedLevelIdentifier;
}
/**
* Set the pickedLevelIdentifier
*
* @param pickedLevelIdentifier Picked level identifier
*/
public void setPickedLevelIdentifier(String pickedLevelIdentifier) {
this.pickedLevelIdentifier = pickedLevelIdentifier;
}
// dead code
//
//public void setPickedLevelIdentifier(String pickedLevelIdentifier) {
// this.pickedLevelIdentifier = pickedLevelIdentifier;
//}
} }

View File

@ -125,6 +125,10 @@ public class LevelModel extends Observable implements Runnable {
this.createLimits(); this.createLimits();
} }
public String getLevelName() {
return levelName;
}
/** /**
* Initializes the animator thread * Initializes the animator thread
*/ */