diff --git a/src/fr/enssat/BoulderDash/controllers/BoulderAndDiamondController.java b/src/fr/enssat/BoulderDash/controllers/BoulderAndDiamondController.java index 656481ff..7bb3b240 100644 --- a/src/fr/enssat/BoulderDash/controllers/BoulderAndDiamondController.java +++ b/src/fr/enssat/BoulderDash/controllers/BoulderAndDiamondController.java @@ -7,7 +7,7 @@ import fr.enssat.BoulderDash.helpers.AudioLoadHelper; /** * ElementPositionUpdateHelper - * + *
* 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. @@ -16,149 +16,156 @@ import fr.enssat.BoulderDash.helpers.AudioLoadHelper; * @since 2015-06-19 */ public class BoulderAndDiamondController implements Runnable { - private LevelModel levelModel; + private LevelModel levelModel; private AudioLoadHelper audioLoadHelper; - private Thread elementMovingThread; + private Thread elementMovingThread; - /** - * Class constructor - * - * @param levelModel Level model - */ - public BoulderAndDiamondController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) { - this.levelModel = levelModel; + /** + * Class constructor + * + * @param levelModel Level model + */ + public BoulderAndDiamondController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) { + this.levelModel = levelModel; this.audioLoadHelper = audioLoadHelper; // Start thread - this.elementMovingThread = new Thread(this); - this.elementMovingThread.start(); - } + 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 + /** + * 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]; + */ + 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 + // 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; + } + } + } + } + } + + private String getSpriteNameLeft(int x, int y) { + DisplayableElementModel elementLeft = this.levelModel.getGroundLevelModel()[x - 1][y]; + return elementLeft.getSpriteName(); + } + + private String getSpriteNameRight(int x, int y) { + DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y]; + return elementRight.getSpriteName(); + } + + private String getSpriteNameBelow(int x, int y) { + DisplayableElementModel elementBelow = getElementBelow(x, y); + return elementBelow.getSpriteName(); + } + + private DisplayableElementModel getElementBelow(int x, int y) { + return this.levelModel.getGroundLevelModel()[x][y + 1]; + } + + /** + * 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; - } + * @param x Horizontal position + * @param y Vertical position + */ + private String expandWall(int x, int y) { + String way = ""; + if (getSpriteNameLeft(x, y) == "black") { + this.levelModel.expandThisWallToLeft(x, y); + way = "left"; + } + if (getSpriteNameRight(x, y) == "black") { + this.levelModel.expandThisWallToRight(x, y); + way = "right"; + } + return way; + } - /** - * 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]; + /** + * Manages the fall of elements + * + * @param x Horizontal position + * @param y Vertical position + */ + private void manageFall(int x, int y) { - String spriteNameBelow = elementBelow.getSpriteName(); - String spriteNameLeft = elementLeft.getSpriteName(); - String spriteNameRight = elementRight.getSpriteName(); - - // 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); + // Then, process in case of the surrounding + String spriteNameBelow = getSpriteNameBelow(x, y); + 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.audioLoadHelper.playSound("die"); - try { - Thread.sleep(25); - } catch (InterruptedException e) { - e.printStackTrace(); - } + 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); - } - } + 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 (getElementBelow(x, y).isDestructible() && spriteNameBelow != "dirt" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) { + this.levelModel.exploseThisBrickWall(x, y); + } else if (getSpriteNameLeft(x, y) == "rockford" && this.levelModel.getRockford().isRunningRight() && this.levelModel.getGroundLevelModel()[x + 1][y].getSpriteName() == "black") { + this.levelModel.moveThisBoulderToRight(x, y); + } else if (getSpriteNameRight(x, y) == "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); + } + } } diff --git a/src/fr/enssat/BoulderDash/models/RockfordModel.java b/src/fr/enssat/BoulderDash/models/RockfordModel.java index e958ebf4..36f0f046 100644 --- a/src/fr/enssat/BoulderDash/models/RockfordModel.java +++ b/src/fr/enssat/BoulderDash/models/RockfordModel.java @@ -114,16 +114,22 @@ public class RockfordModel extends DisplayableElementModel { } } - /** - * Stops the Rockford movement - */ - public void startStaying() { + private void resetMovement(){ isCollisionDone = false; - isStaying = true; + isStaying = false; isRunningLeft = false; isRunningRight = false; isRunningUp = false; isRunningDown = false; + } + + /** + * Stops the Rockford movement + */ + public void startStaying() { + resetMovement(); + isStaying = true; + previousTime = 0; currentFrame = 0; } @@ -132,12 +138,9 @@ public class RockfordModel extends DisplayableElementModel { * Starts moving Rockford to the left */ public void startRunningLeft() { - isCollisionDone = false; - isStaying = false; + resetMovement(); isRunningLeft = true; - isRunningRight = false; - isRunningUp = false; - isRunningDown = false; + previousTime = 0; } @@ -145,12 +148,9 @@ public class RockfordModel extends DisplayableElementModel { * Starts moving Rockford to the right */ public void startRunningRight() { - isCollisionDone = false; - isStaying = false; - isRunningLeft = false; + resetMovement(); isRunningRight = true; - isRunningUp = false; - isRunningDown = false; + previousTime = 0; } @@ -158,12 +158,9 @@ public class RockfordModel extends DisplayableElementModel { * Rockford running up */ public void startRunningUp() { - isCollisionDone = false; - isStaying = false; - isRunningLeft = false; - isRunningRight = false; + resetMovement(); isRunningUp = true; - isRunningDown = false; + previousTime = 0; } @@ -171,12 +168,9 @@ public class RockfordModel extends DisplayableElementModel { * Rockford running down */ public void startRunningDown() { - isCollisionDone = false; - isStaying = false; - isRunningLeft = false; - isRunningRight = false; - isRunningUp = false; + resetMovement(); isRunningDown = true; + previousTime = 0; } diff --git a/src/fr/enssat/BoulderDash/views/GroundView.java b/src/fr/enssat/BoulderDash/views/GroundView.java index 359bbcb4..8236dce8 100644 --- a/src/fr/enssat/BoulderDash/views/GroundView.java +++ b/src/fr/enssat/BoulderDash/views/GroundView.java @@ -43,13 +43,12 @@ public abstract class GroundView extends JPanel implements Observer { */ public void drawTerrain(int width, int height, Graphics g) { // Draw items - if (this.levelModel.getMode() == "game") { - for (int x = 0; x < width; x++) { - for (int y = 0; y < height; y++) { - g.drawImage(this.levelModel.getImage(x, y), (x * 16), (y * 16), this); - } + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + g.drawImage(this.levelModel.getImage(x, y), (x * 16), (y * 16), this); } - + } + if (this.levelModel.getMode() == "game") { if(!this.levelModel.isGameRunning()) { if(!this.levelModel.getRockford().getHasExplosed()) { this.displayWin(); @@ -58,11 +57,6 @@ public abstract class GroundView extends JPanel implements Observer { } } } else { - for (int x = 0; x < width; x++) { - for (int y = 0; y < height; y++) { - g.drawImage(this.levelModel.getImage(x, y), (x * 16), (y * 16), this); - } - } if (this.levelModel.getShowCursor()) { g.drawImage( this.levelModel.getCursorImage(),