Analyzed & fixed duplicated Code

This commit is contained in:
0nlineSam 2024-11-02 13:20:03 +01:00
parent fd3dfb35ca
commit 3dca3338e8
5 changed files with 63 additions and 50 deletions

View File

@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="24" name="Java" />
</Languages>
</inspection_tool>
</profile>
</component>

6
.idea/misc.xml generated
View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK"> <component name="PWA">
<option name="enabled" value="true" />
<option name="wasEnabledAtLeastOnce" value="true" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/classes" /> <output url="file://$PROJECT_DIR$/classes" />
</component> </component>
</project> </project>

View File

@ -86,10 +86,8 @@ public class BoulderAndDiamondController implements Runnable {
* @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]; String spriteNameLeft = getElementSpriteName(x - 1, y);
DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y]; String spriteNameRight = getElementSpriteName(x + 1, y);
String spriteNameLeft = elementLeft.getSpriteName();
String spriteNameRight = elementRight.getSpriteName();
String way = ""; String way = "";
if(spriteNameLeft == "black"){ if(spriteNameLeft == "black"){
@ -112,12 +110,10 @@ public class BoulderAndDiamondController implements Runnable {
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 elementRight = this.levelModel.getGroundLevelModel()[x + 1][y];
String spriteNameBelow = elementBelow.getSpriteName(); String spriteNameBelow = getElementSpriteName(x, y + 1);
String spriteNameLeft = elementLeft.getSpriteName(); String spriteNameLeft = getElementSpriteName(x - 1, y);
String spriteNameRight = elementRight.getSpriteName(); String spriteNameRight = getElementSpriteName(x + 1, y);
// Then, process in case of the surrounding // Then, process in case of the surrounding
if (spriteNameBelow == "black") { if (spriteNameBelow == "black") {
@ -161,4 +157,15 @@ public class BoulderAndDiamondController implements Runnable {
this.levelModel.getGroundLevelModel()[x][y].setFalling(false); 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();
}
} }

View File

@ -118,13 +118,8 @@ public class RockfordModel extends DisplayableElementModel {
* Stops the Rockford movement * Stops the Rockford movement
*/ */
public void startStaying() { public void startStaying() {
isCollisionDone = false; resetState();
isStaying = true; isStaying = true;
isRunningLeft = false;
isRunningRight = false;
isRunningUp = false;
isRunningDown = false;
previousTime = 0;
currentFrame = 0; currentFrame = 0;
} }
@ -132,52 +127,32 @@ public class RockfordModel extends DisplayableElementModel {
* Starts moving Rockford to the left * Starts moving Rockford to the left
*/ */
public void startRunningLeft() { public void startRunningLeft() {
isCollisionDone = false; resetState();
isStaying = false;
isRunningLeft = true; isRunningLeft = true;
isRunningRight = false;
isRunningUp = false;
isRunningDown = false;
previousTime = 0;
} }
/** /**
* Starts moving Rockford to the right * Starts moving Rockford to the right
*/ */
public void startRunningRight() { public void startRunningRight() {
isCollisionDone = false; resetState();
isStaying = false;
isRunningLeft = false;
isRunningRight = true; isRunningRight = true;
isRunningUp = false;
isRunningDown = false;
previousTime = 0;
} }
/** /**
* Rockford running up * Rockford running up
*/ */
public void startRunningUp() { public void startRunningUp() {
isCollisionDone = false; resetState();
isStaying = false;
isRunningLeft = false;
isRunningRight = false;
isRunningUp = true; isRunningUp = true;
isRunningDown = false;
previousTime = 0;
} }
/** /**
* Rockford running down * Rockford running down
*/ */
public void startRunningDown() { public void startRunningDown() {
isCollisionDone = false; resetState();
isStaying = false;
isRunningLeft = false;
isRunningRight = false;
isRunningUp = false;
isRunningDown = true; isRunningDown = true;
previousTime = 0;
} }
/** /**
@ -299,4 +274,14 @@ public class RockfordModel extends DisplayableElementModel {
public void setHasExplosed(boolean hasExploded){ public void setHasExplosed(boolean hasExploded){
this.hasExploded = hasExploded; this.hasExploded = hasExploded;
} }
private void resetState() {
isCollisionDone = false;
isStaying = false;
isRunningLeft = false;
isRunningRight = false;
isRunningUp = false;
isRunningDown = false;
previousTime = 0;
}
} }

View File

@ -44,11 +44,7 @@ public abstract class GroundView extends JPanel implements Observer {
public void drawTerrain(int width, int height, Graphics g) { public void drawTerrain(int width, int height, Graphics g) {
// Draw items // Draw items
if (this.levelModel.getMode() == "game") { if (this.levelModel.getMode() == "game") {
for (int x = 0; x < width; x++) { drawGameTerrain(width, height, g);
for (int y = 0; y < height; y++) {
g.drawImage(this.levelModel.getImage(x, y), (x * 16), (y * 16), this);
}
}
if(!this.levelModel.isGameRunning()) { if(!this.levelModel.isGameRunning()) {
if(!this.levelModel.getRockford().getHasExplosed()) { if(!this.levelModel.getRockford().getHasExplosed()) {
@ -58,11 +54,7 @@ public abstract class GroundView extends JPanel implements Observer {
} }
} }
} else { } else {
for (int x = 0; x < width; x++) { drawGameTerrain(width, height, g);
for (int y = 0; y < height; y++) {
g.drawImage(this.levelModel.getImage(x, y), (x * 16), (y * 16), this);
}
}
if (this.levelModel.getShowCursor()) { if (this.levelModel.getShowCursor()) {
g.drawImage( g.drawImage(
this.levelModel.getCursorImage(), 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 * Set the view to inform the user that he won
*/ */