AudioLoadHelper singleton
This commit is contained in:
parent
f896028199
commit
f40b8b4492
@ -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);
|
||||
|
@ -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;
|
||||
//}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
//}
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
//}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user