-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion Add input validation and improve difficulty configurations. The
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() {
- 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 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 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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();
|
||||||||||||||||||||
/** | ||||||||||||||||||||
* Summary : | ||||||||||||||||||||
* This method runs for every frame like a continues flow , without any stop until we put stop to it. | ||||||||||||||||||||
|
@@ -24,7 +24,7 @@ public class GreenDinoComponent extends Component implements Dinosaur{ | |||||||||||||||||||
*/ | ||||||||||||||||||||
@Override | ||||||||||||||||||||
public void onUpdate(double ptf) { | ||||||||||||||||||||
entity.translateY(verticalSpeed); | ||||||||||||||||||||
entity.translateY(difficulty.getSpeed()); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
//The dinosaur shoots every 2 seconds | ||||||||||||||||||||
if (timer.elapsed(Duration.seconds(1.5)) && entity.getPosition().getY() > 0) | ||||||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||
.put("direction", direction.toPoint2D() ) | ||||||||||||||||||||
); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
There was a problem hiding this comment.
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:
Then update the
getInstance()
method:π Committable suggestion