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
*
* <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 +15,149 @@ 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 AudioLoadHelper audioLoadHelper;
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, AudioLoadHelper audioLoadHelper) {
super(levelModel);
this.audioLoadHelper = audioLoadHelper;
// 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);
}
}
this.audioLoadHelper.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

@ -2,7 +2,6 @@ package fr.enssat.BoulderDash.controllers;
import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import fr.enssat.BoulderDash.controllers.NavigationBetweenViewController;
import fr.enssat.BoulderDash.views.MenuView;
import fr.enssat.BoulderDash.views.GameView;
@ -12,108 +11,110 @@ import java.awt.event.ActionListener;
/**
* 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 implements ActionListener {
private LevelModel levelModel;
private AudioLoadHelper audioLoadHelper;
private boolean firstClickOnPause;
private MenuView menuView;
private GameView gameView;
private NavigationBetweenViewController navigationBetweenViewController;
/**
* Class constructor
*
* @param levelModel Level model
* @param navigationBetweenViewController
*/
public GameController(LevelModel levelModel, AudioLoadHelper audioLoadHelper, NavigationBetweenViewController navigationBetweenViewController) {
this.firstClickOnPause = true;
this.navigationBetweenViewController = navigationBetweenViewController;
this.levelModel = levelModel;
this.audioLoadHelper = audioLoadHelper;
this.gameView = new GameView(this, levelModel);
this.menuView = navigationBetweenViewController.getMenuView();
public class GameController extends AbstractLevelController implements ActionListener {
private AudioLoadHelper audioLoadHelper;
private boolean firstClickOnPause;
private MenuView menuView;
private GameView gameView;
private NavigationBetweenViewController navigationBetweenViewController;
this.getAudioLoadHelper().stopMusic();
this.getAudioLoadHelper().playSound("new");
}
/**
* Class constructor
*
* @param levelModel Level model
* @param navigationBetweenViewController
*/
public GameController(LevelModel levelModel, AudioLoadHelper audioLoadHelper, NavigationBetweenViewController navigationBetweenViewController) {
super(levelModel);
/**
* 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);
}
this.firstClickOnPause = true;
this.firstClickOnPause = !this.firstClickOnPause;
this.gameView.getGameFieldView().grabFocus();
break;
this.navigationBetweenViewController = navigationBetweenViewController;
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;
this.audioLoadHelper = audioLoadHelper;
this.gameView = new GameView(this, levelModel);
this.menuView = navigationBetweenViewController.getMenuView();
this.getAudioLoadHelper().stopMusic();
this.getAudioLoadHelper().playSound("new");
}
/**
* 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")){
this.levelModel = new LevelModel(this.navigationBetweenViewController.getPickedLevelIdentifier(), audioLoadHelper);
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");
this.getAudioLoadHelper().playSound("new");
this.gameView.getGameFieldView().grabFocus();
break;
case "menu":
this.menuView.setVisible(true);
this.getAudioLoadHelper().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.navigationBetweenViewController.getPickedLevelIdentifier(), audioLoadHelper);
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
*
* @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.LevelModel;
import fr.enssat.BoulderDash.controllers.RockfordUpdateController;
import fr.enssat.BoulderDash.controllers.BoulderAndDiamondController;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import java.awt.event.KeyEvent;
@ -12,98 +10,98 @@ import java.awt.event.KeyListener;
/**
* 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 implements KeyListener {
private LevelModel levelModel;
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);
}
public class GameKeyController extends AbstractLevelController implements KeyListener {
private RockfordUpdateController updatePosRockford;
/**
* Handles the 'key pressed' event
*
* @param e Key event
*/
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
/**
* Class constructor
*
* @param levelModel Level model
*/
public GameKeyController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) {
super(levelModel);
new BoulderAndDiamondController(levelModel, audioLoadHelper);
this.updatePosRockford = new RockfordUpdateController(levelModel);
}
switch (keyCode) {
// Direction: UP
case KeyEvent.VK_UP:
DisplayableElementModel upElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() - 1];
/**
* Handles the 'key pressed' event
*
* @param e Key event
*/
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (upElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() - 1);
this.levelModel.getRockford().startRunningUp();
}
switch (keyCode) {
// Direction: UP
case KeyEvent.VK_UP:
DisplayableElementModel upElement = levelModel.getGroundLevelModel()[levelModel.getRockfordPositionX()][levelModel.getRockfordPositionY() - 1];
break;
// 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();
}
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;
if (upElement.getPriority() < levelModel.getRockford().getPriority()) {
this.updatePosRockford.moveRockford(levelModel.getRockfordPositionX(), levelModel.getRockfordPositionY() - 1);
this.levelModel.getRockford().startRunningUp();
}
}
/**
* Handles the 'key released' event
*
* @param e Key event
*/
@Override
public void keyReleased(KeyEvent e) {
this.levelModel.getRockford().startStaying();
}
break;
/**
* Handles the 'key typed' event
*
* @param e Key event
*/
@Override
public void keyTyped(KeyEvent e) {
// Do nothing.
}
// 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();
}
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.views.HelpView;
import fr.enssat.BoulderDash.views.LevelEditorView;
import fr.enssat.BoulderDash.controllers.NavigationBetweenViewController;
import javax.swing.*;
/**
* 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 implements ActionListener {
private LevelModel levelModel;
private LevelEditorView levelEditorView;
private NavigationBetweenViewController nav;
public class LevelEditorController extends AbstractLevelController implements ActionListener {
private LevelEditorView levelEditorView;
private NavigationBetweenViewController nav;
/**
* Class constructor'
*
* @param levelModel Level model
*/
public LevelEditorController(LevelModel levelModel, NavigationBetweenViewController nav) {
this.levelModel = levelModel;
this.levelModel.setShowCursor(true);
/**
* Class constructor'
*
* @param levelModel Level model
*/
public LevelEditorController(LevelModel levelModel, NavigationBetweenViewController nav) {
super(levelModel);
this.levelModel.setShowCursor(true);
this.nav = nav;
this.nav.getAudioLoadHelper().stopMusic();
this.levelEditorView = new LevelEditorView(this, levelModel, nav);
this.nav = nav;
this.nav.getAudioLoadHelper().stopMusic();
// Pre-bind event watcher (hack to fix a Java issue)
this.levelModel.decrementCursorXPosition();
}
this.levelEditorView = new LevelEditorView(this, levelModel, nav);
/**
* 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();
this.nav.getAudioLoadHelper().startMusic("game");
// Pre-bind event watcher (hack to fix a Java issue)
this.levelModel.decrementCursorXPosition();
}
break;
/**
* 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();
this.nav.getAudioLoadHelper().startMusic("game");
case "save":
// Check constraints
try {
this.levelModel.checkConstraints();
break;
// Save action (direct save)
String levelId = this.levelEditorView.getSelectedLevel();
LevelSaveHelper levelSave;
case "save":
// Check constraints
try {
this.levelModel.checkConstraints();
if(levelId == null || levelId.isEmpty()) {
// Create a new level
levelSave = new LevelSaveHelper(levelModel.getGroundLevelModel());
} else {
// Overwrite existing level
levelSave = new LevelSaveHelper(levelId, levelModel.getGroundLevelModel());
}
// Save action (direct save)
String levelId = this.levelEditorView.getSelectedLevel();
LevelSaveHelper levelSave;
JFrame frameDialog = new JFrame("Info");
JOptionPane.showMessageDialog(frameDialog, "Level saved");
if (levelId == null || levelId.isEmpty()) {
// Create a new level
levelSave = new LevelSaveHelper(levelModel.getGroundLevelModel());
} else {
// Overwrite existing level
levelSave = new LevelSaveHelper(levelId, levelModel.getGroundLevelModel());
}
this.levelEditorView.openedLevelChange(levelSave.getLevelId());
} catch(LevelConstraintNotRespectedException e) {
JFrame frameDialog = new JFrame("Error");
JOptionPane.showMessageDialog(frameDialog, e.getMessage());
}
JFrame frameDialog = new JFrame("Info");
JOptionPane.showMessageDialog(frameDialog, "Level saved");
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;
}
/**
* Gets level model
*
* @return Level model
*/
public LevelModel getLevelModel() {
return this.levelModel;
}
/**
* Sets the level editor view
*
* @param levelEditorView Level editor view
*/
public void setLevelEditorView(LevelEditorView levelEditorView) {
this.levelEditorView = levelEditorView;
}
/**
* Gets level model
*
* @return Level model
*/
public LevelModel getLevelModel() {
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
*
* <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 implements KeyListener {
private LevelModel levelModel;
private LevelEditorView levelEditorView;
private boolean capLocks;
public class LevelEditorKeyController extends AbstractLevelController implements KeyListener {
private LevelEditorView levelEditorView;
private boolean capLocks;
/**
* Class constructor
*
* @param levelModel Level model
* @param levelEditorView Level editor view
*/
public LevelEditorKeyController(LevelModel levelModel, LevelEditorView levelEditorView) {
this.levelModel = 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;
}
/**
* 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;
}
/**
* 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)
if(capLocks) {
this.levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
}
// Hold block change (quick edit)
if (capLocks) {
this.levelModel.triggerBlockChange(this.levelEditorView.getPickedBlockValue());
}
}
/**
* Handles the 'key released' event
*
* @param e Key event
*/
@Override
public void keyReleased(KeyEvent e) {
// Do nothing.
}
/**
* 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.
}
/**
* Handles the 'key typed' event
*
* @param e Key event
*/
@Override
public void keyTyped(KeyEvent e) {
// Do nothing.
}
}

View File

@ -11,122 +11,120 @@ import fr.enssat.BoulderDash.controllers.GameController;
/**
* 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 AudioLoadHelper audioLoadHelper;
private GameController gameController;
private String pickedLevelIdentifier;
/**
* Class constructor
*/
public NavigationBetweenViewController() {
this.audioLoadHelper = new AudioLoadHelper();
/**
* Class constructor
*/
public NavigationBetweenViewController() {
this.audioLoadHelper = new AudioLoadHelper();
// Play game music
this.getAudioLoadHelper().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
this.levelModelForEditor = new LevelModel(audioLoadHelper);
this.levelEditorController = new LevelEditorController(this.levelModelForEditor, this);
case "editor":
// New blank model for editor
LevelModel levelModelForEditor = new LevelModel(audioLoadHelper);
this.levelEditorController = new LevelEditorController(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();
this.levelModelForGame = new LevelModel(pickedLevelIdentifier, audioLoadHelper);
this.gameController = new GameController(levelModelForGame, audioLoadHelper, this);
LevelModel 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 audio load helper
*
* @return Audio load helper
*/
public AudioLoadHelper getAudioLoadHelper() {
return this.audioLoadHelper;
}
/**
* Get the first view
*
* @return First view
*/
public MenuView getMenuView() {
return this.menuView;
}
/**
* Set the first view
*
* @param menuView
*/
public MenuView setMenuView() {
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;
}
/**
* 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;
}
}