Merge branch 'ueb03-daniel2' into ueb03

# Conflicts:
#	.idea/misc.xml
#	src/fr/enssat/BoulderDash/controllers/GameController.java
#	src/fr/enssat/BoulderDash/controllers/GameKeyController.java
#	src/fr/enssat/BoulderDash/controllers/LevelEditorController.java
#	src/fr/enssat/BoulderDash/controllers/LevelEditorKeyController.java
This commit is contained in:
0nlineSam 2024-11-07 20:55:12 +01:00
commit a8dc0b20c7
15 changed files with 1399 additions and 1357 deletions

2
.idea/misc.xml generated
View File

@ -3,7 +3,7 @@
<component name="PDMPlugin">
<option name="skipTestSources" value="false" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/classes" />
</component>
</project>

View File

@ -0,0 +1,87 @@
package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.LevelModel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public abstract class AbstractKeyController extends AbstractLevelController implements KeyListener {
public AbstractKeyController(LevelModel levelModel) {
super(levelModel);
}
/**
* Handles the 'key pressed' event
*
* @param e Key event
*/
public void keyPressed(KeyEvent e) {
handleKey(e.getKeyCode());
additionalSteps();
}
protected void handleKey(final int keyCode) {
switch (keyCode) {
case KeyEvent.VK_UP:
up();
break;
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_LEFT:
left();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_SPACE:
space();
break;
case KeyEvent.VK_SHIFT:
shift();
break;
}
}
protected void up() {
}
protected void down() {
}
protected void left() {
}
protected void right() {
}
protected void space() {
}
protected void shift() {
}
protected void additionalSteps() {
}
/**
* Handles the 'key released' event
*
* @param e Key event
*/
@Override
public void keyReleased(KeyEvent e) {
// Do nothing.
}
/**
* Handles the 'key typed' event
*
* @param e Key event
*/
@Override
public void keyTyped(KeyEvent e) {
// Do nothing.
}
}

View File

@ -0,0 +1,11 @@
package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.LevelModel;
public abstract class AbstractLevelController {
protected LevelModel levelModel;
public AbstractLevelController(LevelModel levelModel) {
this.levelModel = levelModel;
}
}

View File

@ -0,0 +1,14 @@
package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.LevelModel;
import java.awt.event.ActionListener;
public abstract class AbstractNavController extends AbstractLevelController implements ActionListener {
protected NavigationBetweenViewController nav;
public AbstractNavController(LevelModel levelModel, NavigationBetweenViewController nav) {
super(levelModel);
this.nav = nav;
}
}

View File

@ -3,11 +3,12 @@ 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
*
* <p>
* Updates position of all elements displayed on the map, according to their
* next potential position. Each object has a weight, which is used to compare
* their power to destroy in the food chain. Sorry for that Darwinism.
@ -15,150 +16,147 @@ import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
* @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19
*/
public class BoulderAndDiamondController implements Runnable {
private LevelModel levelModel;
private AudioLoadHelper audioLoadHelper;
private Thread elementMovingThread;
public class BoulderAndDiamondController extends AbstractLevelController implements Runnable {
private Thread elementMovingThread;
/**
* Class constructor
*
* @param levelModel Level model
*/
public BoulderAndDiamondController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) {
this.levelModel = levelModel;
this.audioLoadHelper = audioLoadHelper;
/**
* Class constructor
*
* @param levelModel Level model
*/
public BoulderAndDiamondController(LevelModel levelModel) {
super(levelModel);
// Start thread
this.elementMovingThread = new Thread(this);
this.elementMovingThread.start();
}
// Start thread
this.elementMovingThread = new Thread(this);
this.elementMovingThread.start();
}
/**
* Watches for elements to be moved
*/
public void run() {
while (this.levelModel.isGameRunning()) {
if(!this.levelModel.getGamePaused()){
this.manageFallingObject();
}
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Watches for elements to be moved
*/
public void run() {
while (this.levelModel.isGameRunning()) {
if (!this.levelModel.getGamePaused()) {
this.manageFallingObject();
}
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Scan the ground to detect the boulders & the diamonds, then make them
* fall if necessary
* Note: scan of the ground upside down: we want things to fall slowly !
*/
private void manageFallingObject() {
for (int x = this.levelModel.getSizeWidth() - 1; x >= 0; x--) {
for (int y = this.levelModel.getSizeHeight() - 1; y >= 0; y--) {
// Gets the spriteName of actual DisplayableElementModel object scanned
DisplayableElementModel elementModel = this.levelModel.getGroundLevelModel()[x][y];
/**
* Scan the ground to detect the boulders & the diamonds, then make them
* fall if necessary
* Note: scan of the ground upside down: we want things to fall slowly !
*/
private void manageFallingObject() {
for (int x = this.levelModel.getSizeWidth() - 1; x >= 0; x--) {
for (int y = this.levelModel.getSizeHeight() - 1; y >= 0; y--) {
// Gets the spriteName of actual DisplayableElementModel object scanned
DisplayableElementModel elementModel = this.levelModel.getGroundLevelModel()[x][y];
if(elementModel == null) {
elementModel = new DirtModel();
}
if (elementModel == null) {
elementModel = new DirtModel();
}
String spriteName = elementModel.getSpriteName();
// If it is a boulder or a diamond...
if (spriteName == "boulder" || spriteName == "diamond") {
this.manageFall(x, y);
} else if(spriteName == "expandingwall"){
if(this.expandWall(x,y).equals("left")){
x -= 1;
}
}
}
}
}
String spriteName = elementModel.getSpriteName();
/**
* Expand the wall at left & right
*
* @param x Horizontal position
* @param y Vertical position
*/
private String expandWall(int x, int y) {
DisplayableElementModel elementLeft = this.levelModel.getGroundLevelModel()[x - 1][y];
DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y];
String spriteNameLeft = elementLeft.getSpriteName();
String spriteNameRight = elementRight.getSpriteName();
String way = "";
if(spriteNameLeft == "black"){
this.levelModel.expandThisWallToLeft(x,y);
way = "left";
}
if(spriteNameRight == "black"){
this.levelModel.expandThisWallToRight(x,y);
way = "right";
}
return way;
}
// If it is a boulder or a diamond...
if (spriteName == "boulder" || spriteName == "diamond") {
this.manageFall(x, y);
} else if (spriteName == "expandingwall") {
if (this.expandWall(x, y).equals("left")) {
x -= 1;
}
}
}
}
}
/**
* Manages the fall of elements
*
* @param x Horizontal position
* @param y Vertical position
*/
private void manageFall(int x, int y) {
// Get informed about Rockford surroundings
DisplayableElementModel elementBelow = this.levelModel.getGroundLevelModel()[x][y + 1];
DisplayableElementModel elementLeft = this.levelModel.getGroundLevelModel()[x - 1][y];
DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y];
/**
* Expand the wall at left & right
*
* @param x Horizontal position
* @param y Vertical position
*/
private String expandWall(int x, int y) {
DisplayableElementModel elementLeft = this.levelModel.getGroundLevelModel()[x - 1][y];
DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y];
String spriteNameLeft = elementLeft.getSpriteName();
String spriteNameRight = elementRight.getSpriteName();
String spriteNameBelow = elementBelow.getSpriteName();
String spriteNameLeft = elementLeft.getSpriteName();
String spriteNameRight = elementRight.getSpriteName();
String way = "";
if (spriteNameLeft == "black") {
this.levelModel.expandThisWallToLeft(x, y);
way = "left";
}
if (spriteNameRight == "black") {
this.levelModel.expandThisWallToRight(x, y);
way = "right";
}
return way;
}
// Then, process in case of the surrounding
if (spriteNameBelow == "black") {
this.levelModel.makeThisDisplayableElementFall(x, y);
} else if (spriteNameBelow == "boulder") {
// Boulders have to roll if they hit another boulder
if (this.levelModel.getGroundLevelModel()[x - 1][y + 1].getSpriteName() == "black") {
this.levelModel.makeThisBoulderSlideLeft(x, y);
} else if (this.levelModel.getGroundLevelModel()[x + 1][y + 1].getSpriteName() == "black") {
this.levelModel.makeThisBoulderSlideRight(x, y);
}
} else if (spriteNameBelow == "rockford" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) {
this.levelModel.exploseGround(x, y + 1);
/**
* Manages the fall of elements
*
* @param x Horizontal position
* @param y Vertical position
*/
private void manageFall(int x, int y) {
// Get informed about Rockford surroundings
DisplayableElementModel elementBelow = this.levelModel.getGroundLevelModel()[x][y + 1];
DisplayableElementModel elementLeft = this.levelModel.getGroundLevelModel()[x - 1][y];
DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y];
this.audioLoadHelper.playSound("die");
String spriteNameBelow = elementBelow.getSpriteName();
String spriteNameLeft = elementLeft.getSpriteName();
String spriteNameRight = elementRight.getSpriteName();
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Then, process in case of the surrounding
if (spriteNameBelow == "black") {
this.levelModel.makeThisDisplayableElementFall(x, y);
} else if (spriteNameBelow == "boulder") {
// Boulders have to roll if they hit another boulder
if (this.levelModel.getGroundLevelModel()[x - 1][y + 1].getSpriteName() == "black") {
this.levelModel.makeThisBoulderSlideLeft(x, y);
} else if (this.levelModel.getGroundLevelModel()[x + 1][y + 1].getSpriteName() == "black") {
this.levelModel.makeThisBoulderSlideRight(x, y);
}
} else if (spriteNameBelow == "rockford" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) {
this.levelModel.exploseGround(x, y + 1);
this.levelModel.setGameRunning(false);
} else if (spriteNameBelow == "magicwall") {
if (this.levelModel.getGroundLevelModel()[x][y].getSpriteName() == "boulder"
&& (this.levelModel.getGroundLevelModel()[x][y+2].getSpriteName() == "dirt" ||
this.levelModel.getGroundLevelModel()[x][y+2].getSpriteName() == "black")) {
if(this.levelModel.getGroundLevelModel()[x][y].isConvertible()) {
this.levelModel.transformThisBoulderIntoADiamond(x, y);
} else {
this.levelModel.deleteThisBoulder(x, y);
}
}
} else if (elementBelow.isDestructible() && spriteNameBelow != "dirt" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) {
this.levelModel.exploseThisBrickWall(x, y);
} else if (spriteNameLeft == "rockford" && this.levelModel.getRockford().isRunningRight() && this.levelModel.getGroundLevelModel()[x + 1][y].getSpriteName() == "black") {
this.levelModel.moveThisBoulderToRight(x, y);
} else if (spriteNameRight == "rockford" && this.levelModel.getRockford().isRunningLeft() && this.levelModel.getGroundLevelModel()[x - 1][y].getSpriteName() == "black") {
this.levelModel.moveThisBoulderToLeft(x, y);
} else {
this.levelModel.getGroundLevelModel()[x][y].setFalling(false);
}
}
AUDIO_LOAD_HELPER.playSound("die");
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.levelModel.setGameRunning(false);
} else if (spriteNameBelow == "magicwall") {
if (this.levelModel.getGroundLevelModel()[x][y].getSpriteName() == "boulder"
&& (this.levelModel.getGroundLevelModel()[x][y + 2].getSpriteName() == "dirt" ||
this.levelModel.getGroundLevelModel()[x][y + 2].getSpriteName() == "black")) {
if (this.levelModel.getGroundLevelModel()[x][y].isConvertible()) {
this.levelModel.transformThisBoulderIntoADiamond(x, y);
} else {
this.levelModel.deleteThisBoulder(x, y);
}
}
} else if (elementBelow.isDestructible() && spriteNameBelow != "dirt" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) {
this.levelModel.exploseThisBrickWall(x, y);
} else if (spriteNameLeft == "rockford" && this.levelModel.getRockford().isRunningRight() && this.levelModel.getGroundLevelModel()[x + 1][y].getSpriteName() == "black") {
this.levelModel.moveThisBoulderToRight(x, y);
} else if (spriteNameRight == "rockford" && this.levelModel.getRockford().isRunningLeft() && this.levelModel.getGroundLevelModel()[x - 1][y].getSpriteName() == "black") {
this.levelModel.moveThisBoulderToLeft(x, y);
} else {
this.levelModel.getGroundLevelModel()[x][y].setFalling(false);
}
}
}

View File

@ -1,112 +1,103 @@
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 static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/**
* GameController
*
* <p>
* This system creates the view.
* The game loop is also handled there.
*
* @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19
* @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19
*/
public class GameController extends GameModuleController {
private AudioLoadHelper audioLoadHelper;
private boolean firstClickOnPause;
private MenuView menuView;
private GameView gameView;
public class GameController extends AbstractNavController {
private boolean firstClickOnPause;
private MenuView menuView;
private GameView gameView;
/**
* Class constructor
*
* @param levelModel Level model
* @param navigationBetweenViewController
*/
public GameController(LevelModel levelModel, AudioLoadHelper audioLoadHelper, NavigationBetweenViewController navigationBetweenViewController) {
super(levelModel, navigationBetweenViewController);
this.firstClickOnPause = true;
/**
* Class constructor
*
* @param levelModel Level model
* @param nav
*/
public GameController(LevelModel levelModel, NavigationBetweenViewController nav) {
super(levelModel, nav);
this.audioLoadHelper = audioLoadHelper;
this.gameView = new GameView(this, levelModel);
this.menuView = navigationBetweenViewController.getMenuView();
this.firstClickOnPause = true;
this.getAudioLoadHelper().stopMusic();
this.getAudioLoadHelper().playSound("new");
}
this.gameView = new GameView(this, levelModel);
this.menuView = nav.getMenuView();
/**
* Handles the 'action performed' event
*
* @param event Action event
*/
@Override
public void actionPerformed(ActionEvent event) {
switch(event.getActionCommand()) {
case "pause":
LevelModel levelModel = getLevelModel();
levelModel.setGamePaused(this.firstClickOnPause);
AUDIO_LOAD_HELPER.stopMusic();
AUDIO_LOAD_HELPER.playSound("new");
}
this.firstClickOnPause = !this.firstClickOnPause;
this.gameView.getGameFieldView().grabFocus();
break;
case "restart":
this.resetGame("restart");
this.getAudioLoadHelper().playSound("new");
this.gameView.getGameFieldView().grabFocus();
break;
case "menu":
this.menuView.setVisible(true);
this.getAudioLoadHelper().startMusic("game");
this.resetGame("menu");
break;
/**
* 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);
}
}
/**
* Function to reset the game
*/
private void resetGame(String source) {
this.gameView.dispose();
if(source.equals("restart")){
resetLevelModel(this.audioLoadHelper);
LevelModel levelModel = getLevelModel();
this.gameView = new GameView(this, levelModel);
this.gameView.setVisible(true);
}
}
this.firstClickOnPause = !this.firstClickOnPause;
this.gameView.getGameFieldView().grabFocus();
break;
/**
* Gets the audio load helper instance
*
* @return Audio load helper instance
*/
public AudioLoadHelper getAudioLoadHelper() {
return this.audioLoadHelper;
case "restart":
this.resetGame("restart");
AUDIO_LOAD_HELPER.playSound("new");
this.gameView.getGameFieldView().grabFocus();
break;
case "menu":
this.menuView.setVisible(true);
AUDIO_LOAD_HELPER.startMusic("game");
this.resetGame("menu");
break;
}
}
/**
* Return the game view
* @return gameView
*/
public GameView getGameView() {
return gameView;
}
/**
* Function to reset the game
*/
private void resetGame(String source) {
this.gameView.dispose();
/**
* Set the gameView
* @param gameView
*/
public void setGameView(GameView gameView) {
this.gameView = gameView;
}
if (source.equals("restart")) {
this.levelModel = new LevelModel(this.nav.getPickedLevelIdentifier());
this.gameView = new GameView(this, levelModel);
this.gameView.setVisible(true);
}
}
/**
* Return the game view
*
* @return gameView
*/
public GameView getGameView() {
return gameView;
}
// dead code
//
//public void setGameView(GameView gameView) {
// this.gameView = gameView;
//}
}

View File

@ -2,92 +2,79 @@ package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.DisplayableElementModel;
import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.controllers.RockfordUpdateController;
import fr.enssat.BoulderDash.controllers.BoulderAndDiamondController;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import java.awt.event.KeyEvent;
/**
* GameKeyController
*
* <p>
* Manages the key events controller.
*
* @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19
* @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19
*/
public class GameKeyController extends KeyController {
private RockfordUpdateController updatePosRockford;
/**
* Class constructor
*
* @param levelModel Level model
*/
public GameKeyController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) {
super(levelModel);
new BoulderAndDiamondController(levelModel, audioLoadHelper);
this.updatePosRockford = new RockfordUpdateController(levelModel);
}
public class GameKeyController extends AbstractKeyController {
private RockfordUpdateController updatePosRockford;
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
LevelModel levelModel = getLevelModel();
/**
* Class constructor
*
* @param levelModel Level model
*/
public GameKeyController(LevelModel levelModel) {
super(levelModel);
new BoulderAndDiamondController(levelModel);
this.updatePosRockford = new RockfordUpdateController(levelModel);
}
switch (keyCode) {
// Direction: UP
case KeyEvent.VK_UP:
DisplayableElementModel upElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() - 1];
@Override
protected void up() {
DisplayableElementModel upElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() - 1];
if (upElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() - 1);
levelModel.getRockford().startRunningUp();
}
if (upElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() - 1);
this.levelModel.getRockford().startRunningUp();
}
}
break;
@Override
protected void down() {
DisplayableElementModel downElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() + 1];
// Direction: DOWN
case KeyEvent.VK_DOWN:
DisplayableElementModel downElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() + 1];
if (downElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() + 1);
this.levelModel.getRockford().startRunningDown();
}
}
if (downElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() + 1);
levelModel.getRockford().startRunningDown();
}
@Override
protected void left() {
DisplayableElementModel leftElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX() - 1][levelModel.getRockfordPositionY()];
break;
if (leftElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX() - 1, levelModel.getRockfordPositionY());
this.levelModel.getRockford().startRunningLeft();
}
}
// Direction: LEFT
case KeyEvent.VK_LEFT:
DisplayableElementModel leftElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX() - 1][levelModel.getRockfordPositionY()];
@Override
protected void right() {
DisplayableElementModel rightElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX() + 1][levelModel.getRockfordPositionY()];
if (leftElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX() - 1, levelModel.getRockfordPositionY());
levelModel.getRockford().startRunningLeft();
}
if (rightElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX() + 1, levelModel.getRockfordPositionY());
this.levelModel.getRockford().startRunningRight();
}
}
break;
// Direction: RIGHT
case KeyEvent.VK_RIGHT:
DisplayableElementModel rightElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX() + 1][levelModel.getRockfordPositionY()];
if (rightElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX() + 1, levelModel.getRockfordPositionY());
levelModel.getRockford().startRunningRight();
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
getLevelModel().getRockford().startStaying();
}
@Override
public void keyTyped(KeyEvent e) {
// Do nothing.
}
/**
* Handles the 'key released' event
*
* @param e Key event
*/
@Override
public void keyReleased(KeyEvent e) {
this.levelModel.getRockford().startStaying();
}
}

View File

@ -8,125 +8,127 @@ import fr.enssat.BoulderDash.helpers.LevelSaveHelper;
import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.views.HelpView;
import fr.enssat.BoulderDash.views.LevelEditorView;
import fr.enssat.BoulderDash.controllers.NavigationBetweenViewController;
import javax.swing.*;
import static fr.enssat.BoulderDash.helpers.AudioLoadHelper.AUDIO_LOAD_HELPER;
/**
* LevelEditorController
*
* <p>
* Manages the level editor controller.
*
* @author Valerian Saliou <valerian@valeriansaliou.name>
* @since 2015-06-19
* @author Valerian Saliou <valerian@valeriansaliou.name>
* @since 2015-06-19
*/
public class LevelEditorController extends GameModuleController {
private LevelEditorView levelEditorView;
public class LevelEditorController extends AbstractNavController {
private LevelEditorView levelEditorView;
/**
* Class constructor'
*
* @param levelModel Level model
*/
public LevelEditorController(LevelModel levelModel, NavigationBetweenViewController nav) {
super(levelModel, nav);
levelModel.setShowCursor(true);
/**
* Class constructor
*
* @param levelModel Level model
*/
public LevelEditorController(LevelModel levelModel, NavigationBetweenViewController nav) {
super(levelModel, nav);
this.levelModel.setShowCursor(true);
nav.getAudioLoadHelper().stopMusic();
AUDIO_LOAD_HELPER.stopMusic();
this.levelEditorView = new LevelEditorView(this, levelModel, nav);
this.levelEditorView = new LevelEditorView(this, levelModel, nav);
// Pre-bind event watcher (hack to fix a Java issue)
levelModel.decrementCursorXPosition();
}
// Pre-bind event watcher (hack to fix a Java issue)
this.levelModel.decrementCursorXPosition();
}
/**
* Handles the 'action performed' event
*
* @param event Action event
*/
@Override
public void actionPerformed(ActionEvent event) {
switch(event.getActionCommand()) {
case "menu":
this.levelEditorView.setVisible(false);
super.menuActionPerformed();
/**
* Handles the 'action performed' event
*
* @param event Action event
*/
public void actionPerformed(ActionEvent event) {
switch (event.getActionCommand()) {
case "menu":
this.levelEditorView.setVisible(false);
this.nav.setMenuView();
AUDIO_LOAD_HELPER.startMusic("game");
break;
break;
case "save":
LevelModel levelModel = getLevelModel();
case "save":
// Check constraints
try {
this.levelModel.checkConstraints();
// Check constraints
try {
levelModel.checkConstraints();
// Save action (direct save)
String levelId = this.levelEditorView.getSelectedLevel();
LevelSaveHelper levelSave;
// Save action (direct save)
String levelId = this.levelEditorView.getSelectedLevel();
LevelSaveHelper levelSave;
if (levelId == null || levelId.isEmpty()) {
// Create a new level
levelSave = new LevelSaveHelper(levelModel.getGroundLevelModel());
} else {
// Overwrite existing level
levelSave = new LevelSaveHelper(levelId, levelModel.getGroundLevelModel());
}
if(levelId == null || levelId.isEmpty()) {
// Create a new level
levelSave = new LevelSaveHelper(levelModel.getGroundLevelModel());
} else {
// Overwrite existing level
levelSave = new LevelSaveHelper(levelId, levelModel.getGroundLevelModel());
}
JFrame frameDialog = new JFrame("Info");
JOptionPane.showMessageDialog(frameDialog, "Level saved");
JFrame frameDialog = new JFrame("Info");
JOptionPane.showMessageDialog(frameDialog, "Level saved");
this.levelEditorView.openedLevelChange(levelSave.getLevelId());
} catch(LevelConstraintNotRespectedException e) {
JFrame frameDialog = new JFrame("Error");
JOptionPane.showMessageDialog(frameDialog, e.getMessage());
}
break;
case "delete":
String levelId = this.levelEditorView.getSelectedLevel();
JFrame frameDialog = new JFrame("Info");
if(levelId == null || levelId.isEmpty()) {
JOptionPane.showMessageDialog(frameDialog, "Level not yet saved, no need to delete it!");
} else {
new LevelRemoveHelper(levelId);
JOptionPane.showMessageDialog(frameDialog, "Level deleted!");
this.levelEditorView.openedLevelChange(null);
}
break;
case "help":
new HelpView();
break;
case "new":
this.levelEditorView.openedLevelChange(null);
break;
this.levelEditorView.openedLevelChange(levelSave.getLevelId());
} catch (LevelConstraintNotRespectedException e) {
JFrame frameDialog = new JFrame("Error");
JOptionPane.showMessageDialog(frameDialog, e.getMessage());
}
this.getLevelEditorView().getLevelEditorGroundView().grabFocus();
break;
case "delete":
String levelId = this.levelEditorView.getSelectedLevel();
JFrame frameDialog = new JFrame("Info");
if (levelId == null || levelId.isEmpty()) {
JOptionPane.showMessageDialog(frameDialog, "Level not yet saved, no need to delete it!");
} else {
new LevelRemoveHelper(levelId);
JOptionPane.showMessageDialog(frameDialog, "Level deleted!");
this.levelEditorView.openedLevelChange(null);
}
break;
case "help":
new HelpView();
break;
case "new":
this.levelEditorView.openedLevelChange(null);
break;
}
/**
* Gets the level editor view
*
* @return Level editor view
*/
public LevelEditorView getLevelEditorView() {
return levelEditorView;
}
this.getLevelEditorView().getLevelEditorGroundView().grabFocus();
}
/**
* Gets the level editor view
*
* @return Level editor view
*/
public LevelEditorView getLevelEditorView() {
return levelEditorView;
}
// dead code
//
//public LevelModel getLevelModel() {
// return this.levelModel;
//}
// dead code
//
//public void setLevelEditorView(LevelEditorView levelEditorView) {
// this.levelEditorView = levelEditorView;
//}
/**
* Sets the level editor view
*
* @param levelEditorView Level editor view
*/
public void setLevelEditorView(LevelEditorView levelEditorView) {
this.levelEditorView = levelEditorView;
}
}

View File

@ -3,82 +3,67 @@ package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.views.LevelEditorView;
import java.awt.event.KeyEvent;
/**
* LevelEditorKeyController
*
* <p>
* Manages the key events controller.
*
* @author Valerian Saliou <valerian@valeriansaliou.name>
* @since 2015-06-21
* @author Valerian Saliou <valerian@valeriansaliou.name>
* @since 2015-06-21
*/
public class LevelEditorKeyController extends KeyController {
private LevelEditorView levelEditorView;
private boolean capLocks;
public class LevelEditorKeyController extends AbstractKeyController {
private LevelEditorView levelEditorView;
private boolean capLocks;
/**
* Class constructor
*
* @param levelModel Level model
* @param levelEditorView Level editor view
*/
public LevelEditorKeyController(LevelModel levelModel, LevelEditorView levelEditorView) {
super(levelModel);
this.capLocks = false;
this.levelEditorView = levelEditorView;
/**
* Class constructor
*
* @param levelModel Level model
* @param levelEditorView Level editor view
*/
public LevelEditorKeyController(LevelModel levelModel, LevelEditorView levelEditorView) {
super(levelModel);
this.capLocks = false;
this.levelEditorView = levelEditorView;
}
@Override
protected void additionalSteps() {
// Hold block change (quick edit)
if (capLocks) {
this.levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
LevelModel levelModel = getLevelModel();
@Override
protected void up() {
this.levelModel.decrementCursorYPosition();
return;
}
switch (keyCode) {
// Direction: UP
case KeyEvent.VK_UP:
levelModel.decrementCursorYPosition();
break;
@Override
protected void down() {
this.levelModel.incrementCursorYPosition();
}
// Direction: DOWN
case KeyEvent.VK_DOWN:
levelModel.incrementCursorYPosition();
break;
@Override
protected void left() {
this.levelModel.decrementCursorXPosition();
}
// Direction: LEFT
case KeyEvent.VK_LEFT:
levelModel.decrementCursorXPosition();
break;
@Override
protected void right() {
this.levelModel.incrementCursorXPosition();
}
// Direction: RIGHT
case KeyEvent.VK_RIGHT:
levelModel.incrementCursorXPosition();
break;
@Override
protected void space() {
this.levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
}
// Key: SPACE
case KeyEvent.VK_SPACE:
levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
break;
case 16:
this.capLocks = !capLocks;
break;
}
// Hold block change (quick edit)
if(capLocks) {
levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
}
}
@Override
public void keyReleased(KeyEvent e) {
// Do nothing.
}
@Override
public void keyTyped(KeyEvent e) {
// Do nothing.
}
@Override
protected void shift() {
this.capLocks = !capLocks;
}
}

View File

@ -3,130 +3,110 @@ 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
*
* @author Colin Leverger <me@colinleverger.fr>
*
* @author Colin Leverger <me@colinleverger.fr>
*/
public class NavigationBetweenViewController implements ActionListener {
private LevelEditorController levelEditorController;
private MenuView menuView;
private AudioLoadHelper audioLoadHelper;
private LevelModel levelModelForGame, levelModelForEditor;
private GameController gameController;
private String pickedLevelIdentifier;
private LevelEditorController levelEditorController;
private MenuView menuView;
private GameController gameController;
private String pickedLevelIdentifier;
/**
* Class constructor
*/
public NavigationBetweenViewController() {
this.audioLoadHelper = new AudioLoadHelper();
/**
* Class constructor
*/
public NavigationBetweenViewController() {
// Play game music
AUDIO_LOAD_HELPER.startMusic("game");
// Play game music
this.getAudioLoadHelper().startMusic("game");
// Creation of the first view
this.menuView = new MenuView(this);
}
// Creation of the first view
this.menuView = new MenuView(this);
}
/**
* Action performed event handler
*
* @param event Action event
*/
@Override
public void actionPerformed(ActionEvent event) {
switch (event.getActionCommand()) {
case "quit":
System.exit(0);
break;
/**
* Action performed event handler
*
* @param event Action event
*/
@Override
public void actionPerformed(ActionEvent event) {
switch (event.getActionCommand()) {
case "quit":
System.exit(0);
break;
case "editor":
// New blank model for editor
LevelModel levelModelForEditor = new LevelModel();
this.levelEditorController = new LevelEditorController(levelModelForEditor, this);
case "editor":
// New blank model for editor
this.levelModelForEditor = new LevelModel(audioLoadHelper);
this.levelEditorController = new LevelEditorController(this.levelModelForEditor, this);
this.levelEditorController.getLevelEditorView().setVisible(true);
this.levelEditorController.getLevelEditorView().getLevelEditorGroundView().grabFocus();
this.levelEditorController.getLevelEditorView().setVisible(true);
this.levelEditorController.getLevelEditorView().getLevelEditorGroundView().grabFocus();
if (gameController != null) {
this.gameController.getGameView().setVisible(false);
}
if (gameController != null) {
this.gameController.getGameView().setVisible(false);
}
break;
break;
case "game":
// Reinit the levelModelForGame...
pickedLevelIdentifier = this.menuView.getLevelIdentifier();
case "game":
// Reinit the levelModelForGame...
pickedLevelIdentifier = this.menuView.getLevelIdentifier();
LevelModel levelModelForGame = new LevelModel(pickedLevelIdentifier);
this.gameController = new GameController(levelModelForGame, this);
this.levelModelForGame = new LevelModel(pickedLevelIdentifier, audioLoadHelper);
this.gameController = new GameController(levelModelForGame, audioLoadHelper, this);
if (levelEditorController != null) {
this.levelEditorController.getLevelEditorView().setVisible(false);
}
if (levelEditorController != null) {
this.levelEditorController.getLevelEditorView().setVisible(false);
}
this.gameController.getGameView().setVisible(true);
this.gameController.getGameView().getGameFieldView().grabFocus();
this.gameController.getGameView().setVisible(true);
this.gameController.getGameView().getGameFieldView().grabFocus();
break;
}
this.menuView.setVisible(false);
}
/**
* Get the audio load helper
*
* @return Audio load helper
*/
public AudioLoadHelper getAudioLoadHelper() {
return this.audioLoadHelper;
break;
}
/**
* Get the first view
*
* @return First view
*/
public MenuView getMenuView() {
return this.menuView;
}
this.menuView.setVisible(false);
}
/**
* Set the first view
*
* @param menuView
*/
public MenuView setMenuView() {
this.menuView = new MenuView(this);
return menuView;
}
/**
* Get the first view
*
* @return First view
*/
public MenuView getMenuView() {
return this.menuView;
}
/**
* Set the first view
*/
public MenuView setMenuView() {
this.menuView = new MenuView(this);
return menuView;
}
/**
* Get the pickedLevel
*
* @return pickedLevelIdentifier Picked level identifier
*/
public String getPickedLevelIdentifier() {
return pickedLevelIdentifier;
}
// dead code
//
//public void setPickedLevelIdentifier(String pickedLevelIdentifier) {
// this.pickedLevelIdentifier = pickedLevelIdentifier;
//}
/**
* 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;
}
}

View File

@ -4,7 +4,7 @@ import fr.enssat.BoulderDash.models.LevelModel;
/**
* ElementPositionUpdateHelper
*
* <p>
* Updates position of all elements displayed on the map, according to their
* next potential position. Each object has a weight, which is used to compare
* their power to destroy in the food chain. Sorry for that Darwinism.
@ -12,53 +12,52 @@ import fr.enssat.BoulderDash.models.LevelModel;
* @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19
*/
public class RockfordUpdateController implements Runnable {
private LevelModel levelModel;
private Thread elementMovingThread;
private int rockfordPositionX;
private int rockfordPositionY;
private boolean rockfordHasMoved;
public class RockfordUpdateController extends AbstractLevelController implements Runnable {
private Thread elementMovingThread;
private int rockfordPositionX;
private int rockfordPositionY;
private boolean rockfordHasMoved;
/**
* Class constructor
*
* @param levelModel Level model
*/
public RockfordUpdateController(LevelModel levelModel) {
this.levelModel = levelModel;
this.elementMovingThread = new Thread(this);
this.elementMovingThread.start();
this.rockfordHasMoved = false;
}
/**
* Class constructor
*
* @param levelModel Level model
*/
public RockfordUpdateController(LevelModel levelModel) {
super(levelModel);
this.elementMovingThread = new Thread(this);
this.elementMovingThread.start();
this.rockfordHasMoved = false;
}
/**
* Watches for elements to be moved
*/
public void run() {
while (this.levelModel.isGameRunning()) {
if(!this.levelModel.getGamePaused()){
if (this.rockfordHasMoved) {
this.levelModel.setPositionOfRockford(rockfordPositionX, rockfordPositionY);
this.rockfordHasMoved = false;
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Moves Rockford
*
* @param rockfordPositionX Next horizontal position on the grid
* @param rockfordPositionY Next vertical position on the grid
*/
public void moveRockford(int rockfordPositionX, int rockfordPositionY) {
this.rockfordPositionX = rockfordPositionX;
this.rockfordPositionY = rockfordPositionY;
this.rockfordHasMoved = true;
}
/**
* Watches for elements to be moved
*/
public void run() {
while (this.levelModel.isGameRunning()) {
if (!this.levelModel.getGamePaused()) {
if (this.rockfordHasMoved) {
this.levelModel.setPositionOfRockford(rockfordPositionX, rockfordPositionY);
this.rockfordHasMoved = false;
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Moves Rockford
*
* @param rockfordPositionX Next horizontal position on the grid
* @param rockfordPositionY Next vertical position on the grid
*/
public void moveRockford(int rockfordPositionX, int rockfordPositionY) {
this.rockfordPositionX = rockfordPositionX;
this.rockfordPositionY = rockfordPositionY;
this.rockfordHasMoved = true;
}
}

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;

File diff suppressed because it is too large Load Diff

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);