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.helpers.AudioLoadHelper;
import fr.enssat.BoulderDash.controllers.NavigationBetweenViewController;
import fr.enssat.BoulderDash.views.MenuView;
import fr.enssat.BoulderDash.views.GameView;
@ -12,7 +11,7 @@ import java.awt.event.ActionListener;
/**
* GameController
*
* <p>
* This system creates the view.
* The game loop is also handled there.
*
@ -53,11 +52,11 @@ public class GameController implements ActionListener {
* @param event Action event
*/
public void actionPerformed(ActionEvent event) {
switch(event.getActionCommand()) {
switch (event.getActionCommand()) {
case "pause":
if(this.firstClickOnPause) {
if (this.firstClickOnPause) {
this.levelModel.setGamePaused(true);
} else if(!this.firstClickOnPause) {
} else if (!this.firstClickOnPause) {
this.levelModel.setGamePaused(false);
}
@ -66,7 +65,7 @@ public class GameController implements ActionListener {
break;
case "restart":
this.resetGame("restart");
this.reset();
this.getAudioLoadHelper().playSound("new");
this.gameView.getGameFieldView().grabFocus();
break;
@ -74,22 +73,29 @@ public class GameController implements ActionListener {
case "menu":
this.menuView.setVisible(true);
this.getAudioLoadHelper().startMusic("game");
this.resetGame("menu");
this.dispose();
break;
}
}
/**
* Function to reset the game
* Reset the game
*/
private void resetGame(String source) {
this.gameView.dispose();
private void reset() {
String pickedLevel = levelModel.getLevelName();
dispose();
if(source.equals("restart")){
this.levelModel = new LevelModel(this.navigationBetweenViewController.getPickedLevelIdentifier(), audioLoadHelper);
// Recreate game
this.levelModel = new LevelModel(pickedLevel, audioLoadHelper);
this.gameView = new GameView(this, levelModel);
this.gameView.setVisible(true);
}
/**
* Dispose the game
*/
private void dispose() {
this.gameView.dispose();
}
/**
@ -103,17 +109,16 @@ public class GameController implements ActionListener {
/**
* Return the game view
*
* @return gameView
*/
public GameView getGameView() {
return gameView;
}
/**
* Set the gameView
* @param gameView
*/
public void setGameView(GameView gameView) {
this.gameView = gameView;
}
// dead code
//
//public void setGameView(GameView gameView) {
// this.gameView = gameView;
//}
}

View File

@ -104,23 +104,15 @@ public class NavigationBetweenViewController implements ActionListener {
// return menuView;
//}
/**
* Get the pickedLevel
*
* @return pickedLevelIdentifier Picked level identifier
*/
public String getPickedLevelIdentifier() {
return pickedLevelIdentifier;
}
/**
* Set the pickedLevelIdentifier
*
* @param pickedLevelIdentifier Picked level identifier
*/
public void setPickedLevelIdentifier(String pickedLevelIdentifier) {
this.pickedLevelIdentifier = pickedLevelIdentifier;
}
// Replaced with LevelModel.getLevelName()
//
//public String getPickedLevelIdentifier() {
// return 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();
}
public String getLevelName() {
return levelName;
}
/**
* Initializes the animator thread
*/