AudioLoadHelper singleton

This commit is contained in:
Daniel Langbein 2024-11-07 16:17:42 +01:00
parent f896028199
commit f40b8b4492
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
9 changed files with 789 additions and 838 deletions

View File

@ -3,7 +3,8 @@ package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.models.DirtModel; import fr.enssat.BoulderDash.models.DirtModel;
import fr.enssat.BoulderDash.models.DisplayableElementModel; import fr.enssat.BoulderDash.models.DisplayableElementModel;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/** /**
* ElementPositionUpdateHelper * ElementPositionUpdateHelper
@ -16,7 +17,6 @@ import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class BoulderAndDiamondController extends AbstractLevelController implements Runnable { public class BoulderAndDiamondController extends AbstractLevelController implements Runnable {
private AudioLoadHelper audioLoadHelper;
private Thread elementMovingThread; private Thread elementMovingThread;
/** /**
@ -24,9 +24,8 @@ public class BoulderAndDiamondController extends AbstractLevelController impleme
* *
* @param levelModel Level model * @param levelModel Level model
*/ */
public BoulderAndDiamondController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) { public BoulderAndDiamondController(LevelModel levelModel) {
super(levelModel); super(levelModel);
this.audioLoadHelper = audioLoadHelper;
// Start thread // Start thread
this.elementMovingThread = new Thread(this); this.elementMovingThread = new Thread(this);
@ -131,7 +130,7 @@ public class BoulderAndDiamondController extends AbstractLevelController impleme
} else if (spriteNameBelow == "rockford" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) { } else if (spriteNameBelow == "rockford" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) {
this.levelModel.exploseGround(x, y + 1); this.levelModel.exploseGround(x, y + 1);
this.audioLoadHelper.playSound("die"); AUDIO_LOAD_HELPER.playSound("die");
try { try {
Thread.sleep(25); Thread.sleep(25);

View File

@ -1,13 +1,14 @@
package fr.enssat.BoulderDash.controllers; 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.views.MenuView; import fr.enssat.BoulderDash.views.MenuView;
import fr.enssat.BoulderDash.views.GameView; import fr.enssat.BoulderDash.views.GameView;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/** /**
* GameController * GameController
@ -19,7 +20,6 @@ import java.awt.event.ActionListener;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class GameController extends AbstractLevelController implements ActionListener { public class GameController extends AbstractLevelController implements ActionListener {
private AudioLoadHelper audioLoadHelper;
private boolean firstClickOnPause; private boolean firstClickOnPause;
private MenuView menuView; private MenuView menuView;
private GameView gameView; private GameView gameView;
@ -31,19 +31,18 @@ public class GameController extends AbstractLevelController implements ActionLis
* @param levelModel Level model * @param levelModel Level model
* @param navigationBetweenViewController * @param navigationBetweenViewController
*/ */
public GameController(LevelModel levelModel, AudioLoadHelper audioLoadHelper, NavigationBetweenViewController navigationBetweenViewController) { public GameController(LevelModel levelModel, NavigationBetweenViewController navigationBetweenViewController) {
super(levelModel); super(levelModel);
this.firstClickOnPause = true; this.firstClickOnPause = true;
this.navigationBetweenViewController = navigationBetweenViewController; this.navigationBetweenViewController = navigationBetweenViewController;
this.audioLoadHelper = audioLoadHelper;
this.gameView = new GameView(this, levelModel); this.gameView = new GameView(this, levelModel);
this.menuView = navigationBetweenViewController.getMenuView(); this.menuView = navigationBetweenViewController.getMenuView();
this.getAudioLoadHelper().stopMusic(); AUDIO_LOAD_HELPER.stopMusic();
this.getAudioLoadHelper().playSound("new"); AUDIO_LOAD_HELPER.playSound("new");
} }
/** /**
@ -66,13 +65,13 @@ public class GameController extends AbstractLevelController implements ActionLis
case "restart": case "restart":
this.resetGame("restart"); this.resetGame("restart");
this.getAudioLoadHelper().playSound("new"); AUDIO_LOAD_HELPER.playSound("new");
this.gameView.getGameFieldView().grabFocus(); this.gameView.getGameFieldView().grabFocus();
break; break;
case "menu": case "menu":
this.menuView.setVisible(true); this.menuView.setVisible(true);
this.getAudioLoadHelper().startMusic("game"); AUDIO_LOAD_HELPER.startMusic("game");
this.resetGame("menu"); this.resetGame("menu");
break; break;
} }
@ -85,21 +84,12 @@ public class GameController extends AbstractLevelController implements ActionLis
this.gameView.dispose(); this.gameView.dispose();
if (source.equals("restart")) { if (source.equals("restart")) {
this.levelModel = new LevelModel(this.navigationBetweenViewController.getPickedLevelIdentifier(), audioLoadHelper); this.levelModel = new LevelModel(this.navigationBetweenViewController.getPickedLevelIdentifier());
this.gameView = new GameView(this, levelModel); this.gameView = new GameView(this, levelModel);
this.gameView.setVisible(true); this.gameView.setVisible(true);
} }
} }
/**
* Gets the audio load helper instance
*
* @return Audio load helper instance
*/
public AudioLoadHelper getAudioLoadHelper() {
return this.audioLoadHelper;
}
/** /**
* Return the game view * Return the game view
* *
@ -109,12 +99,9 @@ public class GameController extends AbstractLevelController implements ActionLis
return gameView; return gameView;
} }
/** // dead code
* Set the gameView //
* //public void setGameView(GameView gameView) {
* @param gameView // this.gameView = gameView;
*/ //}
public void setGameView(GameView gameView) {
this.gameView = gameView;
}
} }

View File

@ -2,7 +2,6 @@ package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.DisplayableElementModel; import fr.enssat.BoulderDash.models.DisplayableElementModel;
import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
@ -24,9 +23,9 @@ public class GameKeyController extends AbstractLevelController implements KeyLis
* *
* @param levelModel Level model * @param levelModel Level model
*/ */
public GameKeyController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) { public GameKeyController(LevelModel levelModel) {
super(levelModel); super(levelModel);
new BoulderAndDiamondController(levelModel, audioLoadHelper); new BoulderAndDiamondController(levelModel);
this.updatePosRockford = new RockfordUpdateController(levelModel); this.updatePosRockford = new RockfordUpdateController(levelModel);
} }

View File

@ -12,6 +12,8 @@ import fr.enssat.BoulderDash.views.LevelEditorView;
import javax.swing.*; import javax.swing.*;
import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/** /**
* LevelEditorController * LevelEditorController
* <p> * <p>
@ -34,7 +36,7 @@ public class LevelEditorController extends AbstractLevelController implements Ac
this.levelModel.setShowCursor(true); this.levelModel.setShowCursor(true);
this.nav = nav; this.nav = nav;
this.nav.getAudioLoadHelper().stopMusic(); AUDIO_LOAD_HELPER.stopMusic();
this.levelEditorView = new LevelEditorView(this, levelModel, nav); this.levelEditorView = new LevelEditorView(this, levelModel, nav);
@ -52,7 +54,7 @@ public class LevelEditorController extends AbstractLevelController implements Ac
case "menu": case "menu":
this.levelEditorView.setVisible(false); this.levelEditorView.setVisible(false);
this.nav.setMenuView(); this.nav.setMenuView();
this.nav.getAudioLoadHelper().startMusic("game"); AUDIO_LOAD_HELPER.startMusic("game");
break; break;
@ -119,23 +121,17 @@ public class LevelEditorController extends AbstractLevelController implements Ac
return levelEditorView; return levelEditorView;
} }
/** // dead code
* Gets level model //
* //public LevelModel getLevelModel() {
* @return Level model // return this.levelModel;
*/ //}
public LevelModel getLevelModel() {
return this.levelModel;
}
/** // dead code
* Sets the level editor view //
* //public void setLevelEditorView(LevelEditorView levelEditorView) {
* @param levelEditorView Level editor view // this.levelEditorView = levelEditorView;
*/ //}
public void setLevelEditorView(LevelEditorView levelEditorView) {
this.levelEditorView = levelEditorView;
}
} }

View File

@ -3,11 +3,10 @@ package fr.enssat.BoulderDash.controllers;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.views.MenuView; import fr.enssat.BoulderDash.views.MenuView;
import fr.enssat.BoulderDash.controllers.LevelEditorController;
import fr.enssat.BoulderDash.controllers.GameController; import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/** /**
* Controller to navigate between the different views * Controller to navigate between the different views
@ -17,7 +16,6 @@ import fr.enssat.BoulderDash.controllers.GameController;
public class NavigationBetweenViewController implements ActionListener { public class NavigationBetweenViewController implements ActionListener {
private LevelEditorController levelEditorController; private LevelEditorController levelEditorController;
private MenuView menuView; private MenuView menuView;
private AudioLoadHelper audioLoadHelper;
private GameController gameController; private GameController gameController;
private String pickedLevelIdentifier; private String pickedLevelIdentifier;
@ -25,10 +23,8 @@ public class NavigationBetweenViewController implements ActionListener {
* Class constructor * Class constructor
*/ */
public NavigationBetweenViewController() { public NavigationBetweenViewController() {
this.audioLoadHelper = new AudioLoadHelper();
// Play game music // Play game music
this.getAudioLoadHelper().startMusic("game"); AUDIO_LOAD_HELPER.startMusic("game");
// Creation of the first view // Creation of the first view
this.menuView = new MenuView(this); this.menuView = new MenuView(this);
@ -48,7 +44,7 @@ public class NavigationBetweenViewController implements ActionListener {
case "editor": case "editor":
// New blank model for editor // New blank model for editor
LevelModel levelModelForEditor = new LevelModel(audioLoadHelper); LevelModel levelModelForEditor = new LevelModel();
this.levelEditorController = new LevelEditorController(levelModelForEditor, this); this.levelEditorController = new LevelEditorController(levelModelForEditor, this);
this.levelEditorController.getLevelEditorView().setVisible(true); this.levelEditorController.getLevelEditorView().setVisible(true);
@ -64,8 +60,8 @@ public class NavigationBetweenViewController implements ActionListener {
// Reinit the levelModelForGame... // Reinit the levelModelForGame...
pickedLevelIdentifier = this.menuView.getLevelIdentifier(); pickedLevelIdentifier = this.menuView.getLevelIdentifier();
LevelModel levelModelForGame = new LevelModel(pickedLevelIdentifier, audioLoadHelper); LevelModel levelModelForGame = new LevelModel(pickedLevelIdentifier);
this.gameController = new GameController(levelModelForGame, audioLoadHelper, this); this.gameController = new GameController(levelModelForGame, this);
if (levelEditorController != null) { if (levelEditorController != null) {
this.levelEditorController.getLevelEditorView().setVisible(false); this.levelEditorController.getLevelEditorView().setVisible(false);
@ -80,15 +76,6 @@ public class NavigationBetweenViewController implements ActionListener {
this.menuView.setVisible(false); this.menuView.setVisible(false);
} }
/**
* Get the audio load helper
*
* @return Audio load helper
*/
public AudioLoadHelper getAudioLoadHelper() {
return this.audioLoadHelper;
}
/** /**
* Get the first view * Get the first view
* *
@ -100,8 +87,6 @@ public class NavigationBetweenViewController implements ActionListener {
/** /**
* Set the first view * Set the first view
*
* @param menuView
*/ */
public MenuView setMenuView() { public MenuView setMenuView() {
this.menuView = new MenuView(this); this.menuView = new MenuView(this);
@ -117,14 +102,11 @@ public class NavigationBetweenViewController implements ActionListener {
return pickedLevelIdentifier; return pickedLevelIdentifier;
} }
/** // dead code
* Set the pickedLevelIdentifier //
* //public void setPickedLevelIdentifier(String pickedLevelIdentifier) {
* @param pickedLevelIdentifier Picked level identifier // this.pickedLevelIdentifier = pickedLevelIdentifier;
*/ //}
public void setPickedLevelIdentifier(String pickedLevelIdentifier) {
this.pickedLevelIdentifier = pickedLevelIdentifier;
}
} }

View File

@ -8,14 +8,18 @@ import java.util.HashMap;
/** /**
* AudioLoadHelper * AudioLoadHelper
* * <p>
* Manages audio * Manages audio
* *
* @author Valerian Saliou <valerian@valeriansaliou.name> * @author Valerian Saliou <valerian@valeriansaliou.name>
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class AudioLoadHelper { public class AudioLoadHelper {
private static String pathToAudioStore = "./res/audio"; private static final String pathToAudioStore = "./res/audio";
/**
* Singleton
*/
public static final AudioLoadHelper AUDIO_LOAD_HELPER = new AudioLoadHelper();
private SoundJLayerBridge musicToPlay; private SoundJLayerBridge musicToPlay;
private HashMap<String, SoundJLayerBridge> preloadedSounds; private HashMap<String, SoundJLayerBridge> preloadedSounds;

View File

@ -3,26 +3,17 @@ package fr.enssat.BoulderDash.models;
import fr.enssat.BoulderDash.exceptions.LevelConstraintNotRespectedException; import fr.enssat.BoulderDash.exceptions.LevelConstraintNotRespectedException;
import fr.enssat.BoulderDash.exceptions.UnknownModelException; import fr.enssat.BoulderDash.exceptions.UnknownModelException;
import fr.enssat.BoulderDash.helpers.LevelLoadHelper; import fr.enssat.BoulderDash.helpers.LevelLoadHelper;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import fr.enssat.BoulderDash.helpers.ModelConvertHelper; import fr.enssat.BoulderDash.helpers.ModelConvertHelper;
import fr.enssat.BoulderDash.models.DisplayableElementModel;
import fr.enssat.BoulderDash.models.RockfordModel;
import fr.enssat.BoulderDash.models.GameInformationModel;
import fr.enssat.BoulderDash.models.SteelWallModel;
import fr.enssat.BoulderDash.models.EmptyModel;
import fr.enssat.BoulderDash.models.DiamondModel;
import fr.enssat.BoulderDash.models.DoorModel;
import fr.enssat.BoulderDash.models.DirtModel;
import fr.enssat.BoulderDash.models.ExpandingWallModel;
import fr.enssat.BoulderDash.models.CursorModel;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Observable; import java.util.Observable;
import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/** /**
* LevelModel * LevelModel
* * <p>
* Levels are loaded from XML file. The view knows the model, the controller is * Levels are loaded from XML file. The view knows the model, the controller is
* going to modify the model in function of the game panel. The model notifies * going to modify the model in function of the game panel. The model notifies
* the view when there are changes on it. * the view when there are changes on it.
@ -33,7 +24,6 @@ import java.util.Observable;
public class LevelModel extends Observable implements Runnable { public class LevelModel extends Observable implements Runnable {
private DisplayableElementModel[][] groundGrid; private DisplayableElementModel[][] groundGrid;
private String levelName; private String levelName;
private AudioLoadHelper audioLoadHelper;
private int sizeWidth = 0; private int sizeWidth = 0;
private int sizeHeight = 0; private int sizeHeight = 0;
private int cursorXPosition = 0; private int cursorXPosition = 0;
@ -63,12 +53,10 @@ public class LevelModel extends Observable implements Runnable {
* Class constructor * Class constructor
* *
* @param levelName Level name * @param levelName Level name
* @param audioLoadHelper Audio load helper
* @param mode Instance mode * @param mode Instance mode
*/ */
public LevelModel(String levelName, AudioLoadHelper audioLoadHelper, String mode) { public LevelModel(String levelName, String mode) {
this.levelName = levelName; this.levelName = levelName;
this.audioLoadHelper = audioLoadHelper;
this.gamePaused = false; this.gamePaused = false;
this.gameRunning = true; this.gameRunning = true;
this.mode = mode; this.mode = mode;
@ -84,7 +72,7 @@ public class LevelModel extends Observable implements Runnable {
this.createLimits(); this.createLimits();
if(this.mode.equals("game")) { if (this.mode.equals("game")) {
this.initRockford(); this.initRockford();
this.initThreadAnimator(); this.initThreadAnimator();
} }
@ -94,19 +82,15 @@ public class LevelModel extends Observable implements Runnable {
* Class constructor * Class constructor
* *
* @param levelName Level name * @param levelName Level name
* @param audioLoadHelper Audio load helper
*/ */
public LevelModel(String levelName, AudioLoadHelper audioLoadHelper) { public LevelModel(String levelName) {
this(levelName, audioLoadHelper, "game"); this(levelName, "game");
} }
/** /**
* Class constructor (editor mode) * Class constructor (editor mode)
*
* @param audioLoadHelper Audio load helper
*/ */
public LevelModel(AudioLoadHelper audioLoadHelper) { public LevelModel() {
this.audioLoadHelper = audioLoadHelper;
this.gameRunning = false; this.gameRunning = false;
this.mode = "editor"; this.mode = "editor";
@ -209,7 +193,7 @@ public class LevelModel extends Observable implements Runnable {
} }
if (collisionSound != null) { if (collisionSound != null) {
this.audioLoadHelper.playSound(collisionSound); AUDIO_LOAD_HELPER.playSound(collisionSound);
} }
} }
@ -274,12 +258,12 @@ public class LevelModel extends Observable implements Runnable {
*/ */
public void triggerBlockChange(String blockValue) { public void triggerBlockChange(String blockValue) {
// No block value? // No block value?
if(blockValue == null || blockValue.isEmpty()) { if (blockValue == null || blockValue.isEmpty()) {
return; return;
} }
// Cancel if Rockford is already in model // Cancel if Rockford is already in model
if((blockValue.equals("Rockford") || blockValue.equals("rockford")) && this.isRockfordInModel()) { if ((blockValue.equals("Rockford") || blockValue.equals("rockford")) && this.isRockfordInModel()) {
return; return;
} }
@ -343,7 +327,7 @@ public class LevelModel extends Observable implements Runnable {
public BufferedImage getImage(int x, int y) { public BufferedImage getImage(int x, int y) {
DisplayableElementModel elementModel = this.getDisplayableElement(x, y); DisplayableElementModel elementModel = this.getDisplayableElement(x, y);
if(elementModel == null) { if (elementModel == null) {
return new DirtModel().getSprite(); return new DirtModel().getSprite();
} }
@ -376,7 +360,7 @@ public class LevelModel extends Observable implements Runnable {
// Iterate and catch it! // Iterate and catch it!
for (int x = 0; x < this.getSizeWidth() && !isInModel; x++) { for (int x = 0; x < this.getSizeWidth() && !isInModel; x++) {
for (int y = 0; y < this.getSizeHeight() && !isInModel; y++) { for (int y = 0; y < this.getSizeHeight() && !isInModel; y++) {
if(this.groundGrid[x][y] != null && this.groundGrid[x][y].getSpriteName() == "rockford") { if (this.groundGrid[x][y] != null && this.groundGrid[x][y].getSpriteName() == "rockford") {
isInModel = true; isInModel = true;
} }
} }
@ -396,7 +380,7 @@ public class LevelModel extends Observable implements Runnable {
// Iterate and catch it! // Iterate and catch it!
for (int x = 0; x < this.getSizeWidth(); x++) { for (int x = 0; x < this.getSizeWidth(); x++) {
for (int y = 0; y < this.getSizeHeight(); y++) { for (int y = 0; y < this.getSizeHeight(); y++) {
if(this.groundGrid[x][y] != null && this.groundGrid[x][y].getSpriteName() == "diamond") { if (this.groundGrid[x][y] != null && this.groundGrid[x][y].getSpriteName() == "diamond") {
numberOfDiamonds += 1; numberOfDiamonds += 1;
} }
} }
@ -410,12 +394,12 @@ public class LevelModel extends Observable implements Runnable {
*/ */
public void checkConstraints() throws LevelConstraintNotRespectedException { public void checkConstraints() throws LevelConstraintNotRespectedException {
// Diamonds number? // Diamonds number?
if(this.countDiamonds() < 3) { if (this.countDiamonds() < 3) {
throw new LevelConstraintNotRespectedException("Add at least 3 diamonds!"); throw new LevelConstraintNotRespectedException("Add at least 3 diamonds!");
} }
// Rockford in model? // Rockford in model?
if(!this.isRockfordInModel()) { if (!this.isRockfordInModel()) {
throw new LevelConstraintNotRespectedException("Add Rockford on the map!"); throw new LevelConstraintNotRespectedException("Add Rockford on the map!");
} }
} }
@ -480,7 +464,7 @@ public class LevelModel extends Observable implements Runnable {
* @param y Sprite block vertical position * @param y Sprite block vertical position
*/ */
public void updateSprites(int x, int y) { public void updateSprites(int x, int y) {
if(groundGrid[x][y] == null) { if (groundGrid[x][y] == null) {
groundGrid[x][y] = new DirtModel(); groundGrid[x][y] = new DirtModel();
} }

View File

@ -31,7 +31,7 @@ public class GameGroundView extends GroundView {
this.gameController = gameController; this.gameController = gameController;
this.addKeyListener(new GameKeyController(this.levelModel, this.gameController.getAudioLoadHelper())); this.addKeyListener(new GameKeyController(this.levelModel));
this.setBorder(BorderFactory.createLineBorder(Color.black)); this.setBorder(BorderFactory.createLineBorder(Color.black));
this.setFocusable(true); this.setFocusable(true);

View File

@ -169,10 +169,10 @@ public class LevelEditorView extends JFrame implements Observer {
if(selectedLevelValue != null && !selectedLevelValue.isEmpty()) { if(selectedLevelValue != null && !selectedLevelValue.isEmpty()) {
// Load existing model // Load existing model
pickedLevelModel = new LevelModel(selectedLevelValue, this.nav.getAudioLoadHelper(), "editor"); pickedLevelModel = new LevelModel(selectedLevelValue, "editor");
} else { } else {
// New blank model for editor // New blank model for editor
pickedLevelModel = new LevelModel(this.nav.getAudioLoadHelper()); pickedLevelModel = new LevelModel();
} }
pickedLevelModel.setShowCursor(true); pickedLevelModel.setShowCursor(true);