Skip to content

5 difficulty levels added for issue #96 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/main/java/com/dinosaur/dinosaurexploder/model/Difficulty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.dinosaur.dinosaurexploder.model;

import java.util.Random;

public class Difficulty {
private final double speed;
private final double minAngleOffset;
private final double maxAngleOffset;
private final Random random = new Random();

public Difficulty(double speed, double min, double max) {
assert max > min;
this.speed = speed;
this.minAngleOffset = min;
this.maxAngleOffset = max;
}
public double getSpeed() {
return speed;
}

public double getAngleOffset() {
if (minAngleOffset == maxAngleOffset) {
return minAngleOffset;
}
return minAngleOffset + (maxAngleOffset - minAngleOffset) * random.nextDouble();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.dinosaur.dinosaurexploder.model;

public class GameSettings {
// Declare private static instance of the class
private static GameSettings instance;

// Global variables
private int difficultyLevel;
private Difficulty difficulty;
Comment on lines +3 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Singleton implementation lacks thread safety.

The singleton pattern is implemented properly for single-threaded scenarios, but lacks thread safety for multi-threaded environments.

Consider using the initialization-on-demand holder idiom for thread-safe lazy initialization:

 public class GameSettings {
-    // Declare private static instance of the class
-    private static GameSettings instance;
+    // Private static holder class for lazy initialization
+    private static class InstanceHolder {
+        private static final GameSettings INSTANCE = new GameSettings();
+    }

     // Global variables
     private int difficultyLevel;
     private Difficulty difficulty;

Then update the getInstance() method:

     // Public static method to provide access to the instance
     public static GameSettings getInstance() {
-        if (instance == null) {
-            instance = new GameSettings();
-        }
-        return instance;
+        return InstanceHolder.INSTANCE;
     }
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public class GameSettings {
// Declare private static instance of the class
private static GameSettings instance;
// Global variables
private int difficultyLevel;
private Difficulty difficulty;
public class GameSettings {
// Private static holder class for lazy initialization
private static class InstanceHolder {
private static final GameSettings INSTANCE = new GameSettings();
}
// Global variables
private int difficultyLevel;
private Difficulty difficulty;
// Public static method to provide access to the instance
public static GameSettings getInstance() {
return InstanceHolder.INSTANCE;
}
// ... other methods ...
}


// Private constructor to prevent instantiation from other classes
private GameSettings() {
difficultyLevel = 1;
difficulty = createDifficulty();
}

private Difficulty createDifficulty() {
double speed, min, max;
switch (difficultyLevel) {
case 1:
speed = 1.0;
min = 90;
max = 90;
break;
case 2:
speed = 2.0;
min = 90;
max = 90;
break;
case 3:
speed = 2.5;
min = 90;
max = 90;
break;
case 4:
speed = 2.5;
min = 22.5;
max = 112.5;
break;
case 5:
speed = 3.0;
min = 45;
max = 135;
break;
default:
throw new IllegalArgumentException("Unknown difficulty level!");
}
return new Difficulty(speed, min, max);
}
Comment on lines +17 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Add input validation and improve difficulty configurations.

The createDifficulty() method handles unknown difficulty levels with an exception, but the input is not validated in the setter.

  1. Add validation in setDifficultyLevel:
     public void setDifficultyLevel(int level) {
+        if (level < 1 || level > 5) {
+            throw new IllegalArgumentException("Difficulty level must be between 1 and 5");
+        }
         this.difficultyLevel = level;
         difficulty = createDifficulty();
     }
  1. Consider using constants for difficulty parameters to improve readability:
+    // Difficulty constants
+    private static final double[] DIFFICULTY_SPEEDS = {1.0, 2.0, 2.5, 2.5, 3.0};
+    private static final double[] MIN_ANGLE_OFFSETS = {90, 90, 90, 22.5, 45};
+    private static final double[] MAX_ANGLE_OFFSETS = {90, 90, 90, 112.5, 135};
+
     private Difficulty createDifficulty() {
-        double speed, min, max;
-        switch (difficultyLevel) {
-            case 1:
-                speed = 1.0;
-                min = 90;
-                max = 90;
-                break;
-            case 2:
-                speed = 2.0;
-                min = 90;
-                max = 90;
-                break;
-            case 3:
-                speed = 2.5;
-                min = 90;
-                max = 90;
-                break;
-            case 4:
-                speed = 2.5;
-                min = 22.5;
-                max = 112.5;
-                break;
-            case 5:
-                speed = 3.0;
-                min = 45;
-                max = 135;
-                break;
-            default:
-                throw new IllegalArgumentException("Unknown difficulty level!");
+        if (difficultyLevel < 1 || difficultyLevel > 5) {
+            throw new IllegalArgumentException("Unknown difficulty level!");
         }
-        return new Difficulty(speed, min, max);
+        int index = difficultyLevel - 1;
+        return new Difficulty(
+            DIFFICULTY_SPEEDS[index],
+            MIN_ANGLE_OFFSETS[index],
+            MAX_ANGLE_OFFSETS[index]
+        );
     }
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private Difficulty createDifficulty() {
double speed, min, max;
switch (difficultyLevel) {
case 1:
speed = 1.0;
min = 90;
max = 90;
break;
case 2:
speed = 2.0;
min = 90;
max = 90;
break;
case 3:
speed = 2.5;
min = 90;
max = 90;
break;
case 4:
speed = 2.5;
min = 22.5;
max = 112.5;
break;
case 5:
speed = 3.0;
min = 45;
max = 135;
break;
default:
throw new IllegalArgumentException("Unknown difficulty level!");
}
return new Difficulty(speed, min, max);
}
public void setDifficultyLevel(int level) {
if (level < 1 || level > 5) {
throw new IllegalArgumentException("Difficulty level must be between 1 and 5");
}
this.difficultyLevel = level;
difficulty = createDifficulty();
}
// Difficulty constants
private static final double[] DIFFICULTY_SPEEDS = {1.0, 2.0, 2.5, 2.5, 3.0};
private static final double[] MIN_ANGLE_OFFSETS = {90, 90, 90, 22.5, 45};
private static final double[] MAX_ANGLE_OFFSETS = {90, 90, 90, 112.5, 135};
private Difficulty createDifficulty() {
if (difficultyLevel < 1 || difficultyLevel > 5) {
throw new IllegalArgumentException("Unknown difficulty level!");
}
int index = difficultyLevel - 1;
return new Difficulty(
DIFFICULTY_SPEEDS[index],
MIN_ANGLE_OFFSETS[index],
MAX_ANGLE_OFFSETS[index]
);
}


// Public static method to provide access to the instance
public static GameSettings getInstance() {
if (instance == null) {
instance = new GameSettings();
}
return instance;
}

// Getters and setters for the global variables

public Difficulty getDifficulty() {
return difficulty;
}

public void setDifficultyLevel(int level) {
this.difficultyLevel = level;
difficulty = createDifficulty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* This class extends Component and Implements the Dinosaur Classes and Handles the Shooting and Updating the Dino
*/
public class GreenDinoComponent extends Component implements Dinosaur{
double verticalSpeed = 1.5;
private LocalTimer timer = FXGL.newLocalTimer();
public Difficulty difficulty = GameSettings.getInstance().getDifficulty();
private final LocalTimer timer = FXGL.newLocalTimer();
Comment on lines +17 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Difficulty should be dynamically accessed, not stored.

The current implementation initializes the difficulty field only once, which means it won't reflect changes to the difficulty level during gameplay.

-    public Difficulty difficulty = GameSettings.getInstance().getDifficulty();
+    // Don't store the difficulty directly, access it when needed
     private final LocalTimer timer = FXGL.newLocalTimer();

Committable suggestion skipped: line range outside the PR's diff.

/**
* Summary :
* This method runs for every frame like a continues flow , without any stop until we put stop to it.
Expand All @@ -24,7 +24,7 @@ public class GreenDinoComponent extends Component implements Dinosaur{
*/
@Override
public void onUpdate(double ptf) {
entity.translateY(verticalSpeed);
entity.translateY(difficulty.getSpeed());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Update to dynamically access difficulty settings.

Since the difficulty field is initialized only once and won't update when changed in GameSettings, you should directly access the current difficulty.

-        entity.translateY(difficulty.getSpeed());
+        entity.translateY(GameSettings.getInstance().getDifficulty().getSpeed());
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
entity.translateY(difficulty.getSpeed());
entity.translateY(GameSettings.getInstance().getDifficulty().getSpeed());


//The dinosaur shoots every 2 seconds
if (timer.elapsed(Duration.seconds(1.5)) && entity.getPosition().getY() > 0)
Expand All @@ -41,9 +41,9 @@ public void onUpdate(double ptf) {
public void shoot() {
FXGL.play(GameConstants.ENEMYSHOOT_SOUND);
Point2D center = entity.getCenter();
Vec2 direction = Vec2.fromAngle(entity.getRotation() +90);
Vec2 direction = Vec2.fromAngle(entity.getRotation() + difficulty.getAngleOffset());
spawn("basicEnemyProjectile",
new SpawnData(center.getX() + 50 +3, center.getY())
new SpawnData(center.getX(), center.getY())
Comment on lines +44 to +46
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Access current difficulty settings dynamically.

Similar to the issue with movement speed, the angle offset calculation should also dynamically access the current difficulty settings.

-        Vec2 direction = Vec2.fromAngle(entity.getRotation() + difficulty.getAngleOffset());
+        Vec2 direction = Vec2.fromAngle(entity.getRotation() + GameSettings.getInstance().getDifficulty().getAngleOffset());
         spawn("basicEnemyProjectile",
                 new SpawnData(center.getX(), center.getY())

The change to use entity's center position for spawning projectiles is a good improvement over the previous offset approach.

πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Vec2 direction = Vec2.fromAngle(entity.getRotation() + difficulty.getAngleOffset());
spawn("basicEnemyProjectile",
new SpawnData(center.getX() + 50 +3, center.getY())
new SpawnData(center.getX(), center.getY())
Vec2 direction = Vec2.fromAngle(
entity.getRotation() + GameSettings.getInstance().getDifficulty().getAngleOffset()
);
spawn("basicEnemyProjectile",
new SpawnData(center.getX(), center.getY())

.put("direction", direction.toPoint2D() )
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onUpdate(double ptf)
lifeText.setFill(Color.RED);
lifeText.setFont(Font.font(GameConstants.ARCADECLASSIC_FONTNAME, 20));

// Adjusting Hearts with respect to text and eachother
// Adjusting Hearts with respect to text and each other
test1.setLayoutY(10);
test2.setLayoutY(10);
test3.setLayoutY(10);
Expand Down
Loading