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.DirtModel;
import fr.enssat.BoulderDash.models.DisplayableElementModel;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/**
* ElementPositionUpdateHelper
@ -16,7 +17,6 @@ import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
* @since 2015-06-19
*/
public class BoulderAndDiamondController extends AbstractLevelController implements Runnable {
private AudioLoadHelper audioLoadHelper;
private Thread elementMovingThread;
/**
@ -24,9 +24,8 @@ public class BoulderAndDiamondController extends AbstractLevelController impleme
*
* @param levelModel Level model
*/
public BoulderAndDiamondController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) {
public BoulderAndDiamondController(LevelModel levelModel) {
super(levelModel);
this.audioLoadHelper = audioLoadHelper;
// Start thread
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()) {
this.levelModel.exploseGround(x, y + 1);
this.audioLoadHelper.playSound("die");
AUDIO_LOAD_HELPER.playSound("die");
try {
Thread.sleep(25);

View File

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

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

View File

@ -12,6 +12,8 @@ import fr.enssat.BoulderDash.views.LevelEditorView;
import javax.swing.*;
import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/**
* LevelEditorController
* <p>
@ -34,7 +36,7 @@ public class LevelEditorController extends AbstractLevelController implements Ac
this.levelModel.setShowCursor(true);
this.nav = nav;
this.nav.getAudioLoadHelper().stopMusic();
AUDIO_LOAD_HELPER.stopMusic();
this.levelEditorView = new LevelEditorView(this, levelModel, nav);
@ -52,7 +54,7 @@ public class LevelEditorController extends AbstractLevelController implements Ac
case "menu":
this.levelEditorView.setVisible(false);
this.nav.setMenuView();
this.nav.getAudioLoadHelper().startMusic("game");
AUDIO_LOAD_HELPER.startMusic("game");
break;
@ -119,23 +121,17 @@ public class LevelEditorController extends AbstractLevelController implements Ac
return levelEditorView;
}
/**
* Gets level model
*
* @return Level model
*/
public LevelModel getLevelModel() {
return this.levelModel;
}
// dead code
//
//public LevelModel getLevelModel() {
// return this.levelModel;
//}
/**
* Sets the level editor view
*
* @param levelEditorView Level editor view
*/
public void setLevelEditorView(LevelEditorView levelEditorView) {
this.levelEditorView = levelEditorView;
}
// dead code
//
//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.ActionListener;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import fr.enssat.BoulderDash.models.LevelModel;
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
@ -17,7 +16,6 @@ import fr.enssat.BoulderDash.controllers.GameController;
public class NavigationBetweenViewController implements ActionListener {
private LevelEditorController levelEditorController;
private MenuView menuView;
private AudioLoadHelper audioLoadHelper;
private GameController gameController;
private String pickedLevelIdentifier;
@ -25,10 +23,8 @@ public class NavigationBetweenViewController implements ActionListener {
* Class constructor
*/
public NavigationBetweenViewController() {
this.audioLoadHelper = new AudioLoadHelper();
// Play game music
this.getAudioLoadHelper().startMusic("game");
AUDIO_LOAD_HELPER.startMusic("game");
// Creation of the first view
this.menuView = new MenuView(this);
@ -48,7 +44,7 @@ public class NavigationBetweenViewController implements ActionListener {
case "editor":
// New blank model for editor
LevelModel levelModelForEditor = new LevelModel(audioLoadHelper);
LevelModel levelModelForEditor = new LevelModel();
this.levelEditorController = new LevelEditorController(levelModelForEditor, this);
this.levelEditorController.getLevelEditorView().setVisible(true);
@ -64,8 +60,8 @@ public class NavigationBetweenViewController implements ActionListener {
// Reinit the levelModelForGame...
pickedLevelIdentifier = this.menuView.getLevelIdentifier();
LevelModel levelModelForGame = new LevelModel(pickedLevelIdentifier, audioLoadHelper);
this.gameController = new GameController(levelModelForGame, audioLoadHelper, this);
LevelModel levelModelForGame = new LevelModel(pickedLevelIdentifier);
this.gameController = new GameController(levelModelForGame, this);
if (levelEditorController != null) {
this.levelEditorController.getLevelEditorView().setVisible(false);
@ -80,15 +76,6 @@ public class NavigationBetweenViewController implements ActionListener {
this.menuView.setVisible(false);
}
/**
* Get the audio load helper
*
* @return Audio load helper
*/
public AudioLoadHelper getAudioLoadHelper() {
return this.audioLoadHelper;
}
/**
* Get the first view
*
@ -100,8 +87,6 @@ public class NavigationBetweenViewController implements ActionListener {
/**
* Set the first view
*
* @param menuView
*/
public MenuView setMenuView() {
this.menuView = new MenuView(this);
@ -117,14 +102,11 @@ public class NavigationBetweenViewController implements ActionListener {
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

@ -8,14 +8,18 @@ import java.util.HashMap;
/**
* AudioLoadHelper
*
* <p>
* Manages audio
*
* @author Valerian Saliou <valerian@valeriansaliou.name>
* @since 2015-06-19
*/
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 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.UnknownModelException;
import fr.enssat.BoulderDash.helpers.LevelLoadHelper;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
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.util.Observable;
import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/**
* LevelModel
*
* <p>
* 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
* the view when there are changes on it.
@ -33,7 +24,6 @@ import java.util.Observable;
public class LevelModel extends Observable implements Runnable {
private DisplayableElementModel[][] groundGrid;
private String levelName;
private AudioLoadHelper audioLoadHelper;
private int sizeWidth = 0;
private int sizeHeight = 0;
private int cursorXPosition = 0;
@ -63,12 +53,10 @@ public class LevelModel extends Observable implements Runnable {
* Class constructor
*
* @param levelName Level name
* @param audioLoadHelper Audio load helper
* @param mode Instance mode
*/
public LevelModel(String levelName, AudioLoadHelper audioLoadHelper, String mode) {
public LevelModel(String levelName, String mode) {
this.levelName = levelName;
this.audioLoadHelper = audioLoadHelper;
this.gamePaused = false;
this.gameRunning = true;
this.mode = mode;
@ -84,7 +72,7 @@ public class LevelModel extends Observable implements Runnable {
this.createLimits();
if(this.mode.equals("game")) {
if (this.mode.equals("game")) {
this.initRockford();
this.initThreadAnimator();
}
@ -94,19 +82,15 @@ public class LevelModel extends Observable implements Runnable {
* Class constructor
*
* @param levelName Level name
* @param audioLoadHelper Audio load helper
*/
public LevelModel(String levelName, AudioLoadHelper audioLoadHelper) {
this(levelName, audioLoadHelper, "game");
public LevelModel(String levelName) {
this(levelName, "game");
}
/**
* Class constructor (editor mode)
*
* @param audioLoadHelper Audio load helper
*/
public LevelModel(AudioLoadHelper audioLoadHelper) {
this.audioLoadHelper = audioLoadHelper;
public LevelModel() {
this.gameRunning = false;
this.mode = "editor";
@ -209,7 +193,7 @@ public class LevelModel extends Observable implements Runnable {
}
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) {
// No block value?
if(blockValue == null || blockValue.isEmpty()) {
if (blockValue == null || blockValue.isEmpty()) {
return;
}
// 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;
}
@ -343,7 +327,7 @@ public class LevelModel extends Observable implements Runnable {
public BufferedImage getImage(int x, int y) {
DisplayableElementModel elementModel = this.getDisplayableElement(x, y);
if(elementModel == null) {
if (elementModel == null) {
return new DirtModel().getSprite();
}
@ -376,7 +360,7 @@ public class LevelModel extends Observable implements Runnable {
// Iterate and catch it!
for (int x = 0; x < this.getSizeWidth() && !isInModel; x++) {
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;
}
}
@ -396,7 +380,7 @@ public class LevelModel extends Observable implements Runnable {
// Iterate and catch it!
for (int x = 0; x < this.getSizeWidth(); x++) {
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;
}
}
@ -410,12 +394,12 @@ public class LevelModel extends Observable implements Runnable {
*/
public void checkConstraints() throws LevelConstraintNotRespectedException {
// Diamonds number?
if(this.countDiamonds() < 3) {
if (this.countDiamonds() < 3) {
throw new LevelConstraintNotRespectedException("Add at least 3 diamonds!");
}
// Rockford in model?
if(!this.isRockfordInModel()) {
if (!this.isRockfordInModel()) {
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
*/
public void updateSprites(int x, int y) {
if(groundGrid[x][y] == null) {
if (groundGrid[x][y] == null) {
groundGrid[x][y] = new DirtModel();
}

View File

@ -31,7 +31,7 @@ public class GameGroundView extends GroundView {
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.setFocusable(true);

View File

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