mv levelModel field up to superclass

This commit is contained in:
Daniel Langbein 2024-11-07 15:30:55 +01:00
parent 99db8d4956
commit f896028199
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
8 changed files with 643 additions and 640 deletions

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

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

View File

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

View File

@ -2,8 +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.controllers.RockfordUpdateController;
import fr.enssat.BoulderDash.controllers.BoulderAndDiamondController;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper; import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@ -12,98 +10,98 @@ import java.awt.event.KeyListener;
/** /**
* GameKeyController * GameKeyController
* * <p>
* Manages the key events controller. * Manages the key events controller.
* *
* @author Colin Leverger <me@colinleverger.fr> * @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class GameKeyController implements KeyListener { public class GameKeyController extends AbstractLevelController implements KeyListener {
private LevelModel levelModel; private RockfordUpdateController updatePosRockford;
private RockfordUpdateController updatePosRockford;
/**
* Class constructor
*
* @param levelModel Level model
*/
public GameKeyController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) {
this.levelModel = levelModel;
new BoulderAndDiamondController(levelModel, audioLoadHelper);
this.updatePosRockford = new RockfordUpdateController(levelModel);
}
/** /**
* Handles the 'key pressed' event * Class constructor
* *
* @param e Key event * @param levelModel Level model
*/ */
public void keyPressed(KeyEvent e) { public GameKeyController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) {
int keyCode = e.getKeyCode(); super(levelModel);
new BoulderAndDiamondController(levelModel, audioLoadHelper);
this.updatePosRockford = new RockfordUpdateController(levelModel);
}
switch (keyCode) { /**
// Direction: UP * Handles the 'key pressed' event
case KeyEvent.VK_UP: *
DisplayableElementModel upElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() - 1]; * @param e Key event
*/
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (upElement.getPriority() < levelModel.getRockford().getPriority()) { switch (keyCode) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() - 1); // Direction: UP
this.levelModel.getRockford().startRunningUp(); case KeyEvent.VK_UP:
} DisplayableElementModel upElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() - 1];
break; if (upElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() - 1);
// Direction: DOWN this.levelModel.getRockford().startRunningUp();
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();
}
break;
// Direction: LEFT
case KeyEvent.VK_LEFT:
DisplayableElementModel leftElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX() - 1][levelModel.getRockfordPositionY()];
if (leftElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX() - 1, levelModel.getRockfordPositionY());
this.levelModel.getRockford().startRunningLeft();
}
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());
this.levelModel.getRockford().startRunningRight();
}
break;
} }
}
/** break;
* Handles the 'key released' event
*
* @param e Key event
*/
@Override
public void keyReleased(KeyEvent e) {
this.levelModel.getRockford().startStaying();
}
/** // Direction: DOWN
* Handles the 'key typed' event case KeyEvent.VK_DOWN:
* DisplayableElementModel downElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() + 1];
* @param e Key event
*/ if (downElement.getPriority() < levelModel.getRockford().getPriority()) {
@Override this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() + 1);
public void keyTyped(KeyEvent e) { this.levelModel.getRockford().startRunningDown();
// Do nothing. }
}
break;
// Direction: LEFT
case KeyEvent.VK_LEFT:
DisplayableElementModel leftElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX() - 1][levelModel.getRockfordPositionY()];
if (leftElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX() - 1, levelModel.getRockfordPositionY());
this.levelModel.getRockford().startRunningLeft();
}
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());
this.levelModel.getRockford().startRunningRight();
}
break;
}
}
/**
* Handles the 'key released' event
*
* @param e Key event
*/
@Override
public void keyReleased(KeyEvent e) {
this.levelModel.getRockford().startStaying();
}
/**
* Handles the 'key typed' event
*
* @param e Key event
*/
@Override
public void keyTyped(KeyEvent e) {
// Do nothing.
}
} }

View File

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

View File

@ -9,91 +9,90 @@ import java.awt.event.KeyListener;
/** /**
* LevelEditorKeyController * LevelEditorKeyController
* * <p>
* Manages the key events controller. * Manages the key events controller.
* *
* @author Valerian Saliou <valerian@valeriansaliou.name> * @author Valerian Saliou <valerian@valeriansaliou.name>
* @since 2015-06-21 * @since 2015-06-21
*/ */
public class LevelEditorKeyController implements KeyListener { public class LevelEditorKeyController extends AbstractLevelController implements KeyListener {
private LevelModel levelModel; private LevelEditorView levelEditorView;
private LevelEditorView levelEditorView; private boolean capLocks;
private boolean capLocks;
/** /**
* Class constructor * Class constructor
* *
* @param levelModel Level model * @param levelModel Level model
* @param levelEditorView Level editor view * @param levelEditorView Level editor view
*/ */
public LevelEditorKeyController(LevelModel levelModel, LevelEditorView levelEditorView) { public LevelEditorKeyController(LevelModel levelModel, LevelEditorView levelEditorView) {
this.levelModel = levelModel; super(levelModel);
this.capLocks = false; this.capLocks = false;
this.levelEditorView = levelEditorView; this.levelEditorView = levelEditorView;
}
/**
* Handles the 'key pressed' event
*
* @param e Key event
*/
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
// Direction: UP
case KeyEvent.VK_UP:
this.levelModel.decrementCursorYPosition();
break;
// Direction: DOWN
case KeyEvent.VK_DOWN:
this.levelModel.incrementCursorYPosition();
break;
// Direction: LEFT
case KeyEvent.VK_LEFT:
this.levelModel.decrementCursorXPosition();
break;
// Direction: RIGHT
case KeyEvent.VK_RIGHT:
this.levelModel.incrementCursorXPosition();
break;
// Key: SPACE
case KeyEvent.VK_SPACE:
this.levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
break;
case 16:
this.capLocks = !capLocks;
break;
} }
/** // Hold block change (quick edit)
* Handles the 'key pressed' event if (capLocks) {
* this.levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
* @param e Key event
*/
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
// Direction: UP
case KeyEvent.VK_UP:
this.levelModel.decrementCursorYPosition();
break;
// Direction: DOWN
case KeyEvent.VK_DOWN:
this.levelModel.incrementCursorYPosition();
break;
// Direction: LEFT
case KeyEvent.VK_LEFT:
this.levelModel.decrementCursorXPosition();
break;
// Direction: RIGHT
case KeyEvent.VK_RIGHT:
this.levelModel.incrementCursorXPosition();
break;
// Key: SPACE
case KeyEvent.VK_SPACE:
this.levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
break;
case 16:
this.capLocks = !capLocks;
break;
}
// Hold block change (quick edit)
if(capLocks) {
this.levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
}
} }
}
/** /**
* Handles the 'key released' event * Handles the 'key released' event
* *
* @param e Key event * @param e Key event
*/ */
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
// Do nothing. // Do nothing.
} }
/** /**
* Handles the 'key typed' event * Handles the 'key typed' event
* *
* @param e Key event * @param e Key event
*/ */
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
// Do nothing. // Do nothing.
} }
} }

View File

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

View File

@ -4,7 +4,7 @@ import fr.enssat.BoulderDash.models.LevelModel;
/** /**
* ElementPositionUpdateHelper * ElementPositionUpdateHelper
* * <p>
* Updates position of all elements displayed on the map, according to their * 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 * 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. * 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> * @author Colin Leverger <me@colinleverger.fr>
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class RockfordUpdateController implements Runnable { public class RockfordUpdateController extends AbstractLevelController implements Runnable {
private LevelModel levelModel; private Thread elementMovingThread;
private Thread elementMovingThread; private int rockfordPositionX;
private int rockfordPositionX; private int rockfordPositionY;
private int rockfordPositionY; private boolean rockfordHasMoved;
private boolean rockfordHasMoved;
/** /**
* Class constructor * Class constructor
* *
* @param levelModel Level model * @param levelModel Level model
*/ */
public RockfordUpdateController(LevelModel levelModel) { public RockfordUpdateController(LevelModel levelModel) {
this.levelModel = levelModel; super(levelModel);
this.elementMovingThread = new Thread(this); this.elementMovingThread = new Thread(this);
this.elementMovingThread.start(); this.elementMovingThread.start();
this.rockfordHasMoved = false; this.rockfordHasMoved = false;
} }
/** /**
* Watches for elements to be moved * Watches for elements to be moved
*/ */
public void run() { public void run() {
while (this.levelModel.isGameRunning()) { while (this.levelModel.isGameRunning()) {
if(!this.levelModel.getGamePaused()){ if (!this.levelModel.getGamePaused()) {
if (this.rockfordHasMoved) { if (this.rockfordHasMoved) {
this.levelModel.setPositionOfRockford(rockfordPositionX, rockfordPositionY); this.levelModel.setPositionOfRockford(rockfordPositionX, rockfordPositionY);
this.rockfordHasMoved = false; this.rockfordHasMoved = false;
} }
} }
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
/** /**
* Moves Rockford * Moves Rockford
* *
* @param rockfordPositionX Next horizontal position on the grid * @param rockfordPositionX Next horizontal position on the grid
* @param rockfordPositionY Next vertical position on the grid * @param rockfordPositionY Next vertical position on the grid
*/ */
public void moveRockford(int rockfordPositionX, int rockfordPositionY) { public void moveRockford(int rockfordPositionX, int rockfordPositionY) {
this.rockfordPositionX = rockfordPositionX; this.rockfordPositionX = rockfordPositionX;
this.rockfordPositionY = rockfordPositionY; this.rockfordPositionY = rockfordPositionY;
this.rockfordHasMoved = true; this.rockfordHasMoved = true;
} }
} }