From 3dca3338e8fa588818b4b2f6d065f2b7beeee27f Mon Sep 17 00:00:00 2001 From: 0nlineSam Date: Sat, 2 Nov 2024 13:20:03 +0100 Subject: [PATCH] Analyzed & fixed duplicated Code --- .idea/inspectionProfiles/Project_Default.xml | 10 +++++ .idea/misc.xml | 6 ++- .../BoulderAndDiamondController.java | 25 +++++++---- .../BoulderDash/models/RockfordModel.java | 45 +++++++------------ .../enssat/BoulderDash/views/GroundView.java | 27 ++++++----- 5 files changed, 63 insertions(+), 50 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..73e8a682 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 4b09d600..6c8ad073 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,10 @@ - + + + \ No newline at end of file diff --git a/src/fr/enssat/BoulderDash/controllers/BoulderAndDiamondController.java b/src/fr/enssat/BoulderDash/controllers/BoulderAndDiamondController.java index 656481ff..0c3bacf1 100644 --- a/src/fr/enssat/BoulderDash/controllers/BoulderAndDiamondController.java +++ b/src/fr/enssat/BoulderDash/controllers/BoulderAndDiamondController.java @@ -86,10 +86,8 @@ public class BoulderAndDiamondController implements Runnable { * @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 spriteNameLeft = getElementSpriteName(x - 1, y); + String spriteNameRight = getElementSpriteName(x + 1, y); String way = ""; if(spriteNameLeft == "black"){ @@ -112,12 +110,10 @@ public class BoulderAndDiamondController implements Runnable { 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]; - String spriteNameBelow = elementBelow.getSpriteName(); - String spriteNameLeft = elementLeft.getSpriteName(); - String spriteNameRight = elementRight.getSpriteName(); + String spriteNameBelow = getElementSpriteName(x, y + 1); + String spriteNameLeft = getElementSpriteName(x - 1, y); + String spriteNameRight = getElementSpriteName(x + 1, y); // Then, process in case of the surrounding if (spriteNameBelow == "black") { @@ -161,4 +157,15 @@ public class BoulderAndDiamondController implements Runnable { this.levelModel.getGroundLevelModel()[x][y].setFalling(false); } } + + /** + * Returns the sprite name of a DisplayableElementModel + * + * @param x Horizontal position + * @param y Vertical position + */ + private String getElementSpriteName(int x, int y) { + DisplayableElementModel elementModel = this.levelModel.getGroundLevelModel()[x][y]; + return elementModel.getSpriteName(); + } } diff --git a/src/fr/enssat/BoulderDash/models/RockfordModel.java b/src/fr/enssat/BoulderDash/models/RockfordModel.java index e958ebf4..fd21f143 100644 --- a/src/fr/enssat/BoulderDash/models/RockfordModel.java +++ b/src/fr/enssat/BoulderDash/models/RockfordModel.java @@ -118,13 +118,8 @@ public class RockfordModel extends DisplayableElementModel { * Stops the Rockford movement */ public void startStaying() { - isCollisionDone = false; + resetState(); isStaying = true; - isRunningLeft = false; - isRunningRight = false; - isRunningUp = false; - isRunningDown = false; - previousTime = 0; currentFrame = 0; } @@ -132,52 +127,32 @@ public class RockfordModel extends DisplayableElementModel { * Starts moving Rockford to the left */ public void startRunningLeft() { - isCollisionDone = false; - isStaying = false; + resetState(); isRunningLeft = true; - isRunningRight = false; - isRunningUp = false; - isRunningDown = false; - previousTime = 0; } /** * Starts moving Rockford to the right */ public void startRunningRight() { - isCollisionDone = false; - isStaying = false; - isRunningLeft = false; + resetState(); isRunningRight = true; - isRunningUp = false; - isRunningDown = false; - previousTime = 0; } /** * Rockford running up */ public void startRunningUp() { - isCollisionDone = false; - isStaying = false; - isRunningLeft = false; - isRunningRight = false; + resetState(); isRunningUp = true; - isRunningDown = false; - previousTime = 0; } /** * Rockford running down */ public void startRunningDown() { - isCollisionDone = false; - isStaying = false; - isRunningLeft = false; - isRunningRight = false; - isRunningUp = false; + resetState(); isRunningDown = true; - previousTime = 0; } /** @@ -299,4 +274,14 @@ public class RockfordModel extends DisplayableElementModel { public void setHasExplosed(boolean hasExploded){ this.hasExploded = hasExploded; } + + private void resetState() { + isCollisionDone = false; + isStaying = false; + isRunningLeft = false; + isRunningRight = false; + isRunningUp = false; + isRunningDown = false; + previousTime = 0; + } } diff --git a/src/fr/enssat/BoulderDash/views/GroundView.java b/src/fr/enssat/BoulderDash/views/GroundView.java index 359bbcb4..9ac1c02f 100644 --- a/src/fr/enssat/BoulderDash/views/GroundView.java +++ b/src/fr/enssat/BoulderDash/views/GroundView.java @@ -44,11 +44,7 @@ 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); - } - } + drawGameTerrain(width, height, g); if(!this.levelModel.isGameRunning()) { if(!this.levelModel.getRockford().getHasExplosed()) { @@ -58,11 +54,7 @@ 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); - } - } + drawGameTerrain(width, height, g); if (this.levelModel.getShowCursor()) { g.drawImage( this.levelModel.getCursorImage(), @@ -74,6 +66,21 @@ public abstract class GroundView extends JPanel implements Observer { } } + /** + * Draws the game terrain + * + * @param width Map width + * @param height Map height + * @param g Map graphical object + */ + private void drawGameTerrain(int width, int height, Graphics g) { + 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); + } + } + } + /** * Set the view to inform the user that he won */