Skip to content

Commit 38f4065

Browse files
5 difficulty levels added for issue jvondermarck#96
1 parent a49292a commit 38f4065

File tree

6 files changed

+192
-117
lines changed

6 files changed

+192
-117
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.dinosaur.dinosaurexploder.model;
2+
3+
import java.util.Random;
4+
5+
public class Difficulty {
6+
private final double speed;
7+
private final double minAngleOffset;
8+
private final double maxAngleOffset;
9+
private final Random random = new Random();
10+
11+
public Difficulty(double speed, double min, double max) {
12+
assert max > min;
13+
this.speed = speed;
14+
this.minAngleOffset = min;
15+
this.maxAngleOffset = max;
16+
}
17+
public double getSpeed() {
18+
return speed;
19+
}
20+
21+
public double getAngleOffset() {
22+
if (minAngleOffset == maxAngleOffset) {
23+
return minAngleOffset;
24+
}
25+
return minAngleOffset + (maxAngleOffset - minAngleOffset) * random.nextDouble();
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.dinosaur.dinosaurexploder.model;
2+
3+
public class GameSettings {
4+
// Declare private static instance of the class
5+
private static GameSettings instance;
6+
7+
// Global variables
8+
private int difficultyLevel;
9+
private Difficulty difficulty;
10+
11+
// Private constructor to prevent instantiation from other classes
12+
private GameSettings() {
13+
difficultyLevel = 1;
14+
difficulty = createDifficulty();
15+
}
16+
17+
private Difficulty createDifficulty() {
18+
double speed, min, max;
19+
switch (difficultyLevel) {
20+
case 1:
21+
speed = 1.0;
22+
min = 90;
23+
max = 90;
24+
break;
25+
case 2:
26+
speed = 2.0;
27+
min = 90;
28+
max = 90;
29+
break;
30+
case 3:
31+
speed = 2.5;
32+
min = 90;
33+
max = 90;
34+
break;
35+
case 4:
36+
speed = 2.5;
37+
min = 22.5;
38+
max = 112.5;
39+
break;
40+
case 5:
41+
speed = 3.0;
42+
min = 45;
43+
max = 135;
44+
break;
45+
default:
46+
throw new IllegalArgumentException("Unknown difficulty level!");
47+
}
48+
return new Difficulty(speed, min, max);
49+
}
50+
51+
// Public static method to provide access to the instance
52+
public static GameSettings getInstance() {
53+
if (instance == null) {
54+
instance = new GameSettings();
55+
}
56+
return instance;
57+
}
58+
59+
// Getters and setters for the global variables
60+
61+
public Difficulty getDifficulty() {
62+
return difficulty;
63+
}
64+
65+
public void setDifficultyLevel(int level) {
66+
this.difficultyLevel = level;
67+
difficulty = createDifficulty();
68+
}
69+
}

src/main/java/com/dinosaur/dinosaurexploder/model/GreenDinoComponent.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* This class extends Component and Implements the Dinosaur Classes and Handles the Shooting and Updating the Dino
1515
*/
1616
public class GreenDinoComponent extends Component implements Dinosaur{
17-
double verticalSpeed = 1.5;
18-
private LocalTimer timer = FXGL.newLocalTimer();
17+
public Difficulty difficulty = GameSettings.getInstance().getDifficulty();
18+
private final LocalTimer timer = FXGL.newLocalTimer();
1919
/**
2020
* Summary :
2121
* 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{
2424
*/
2525
@Override
2626
public void onUpdate(double ptf) {
27-
entity.translateY(verticalSpeed);
27+
entity.translateY(difficulty.getSpeed());
2828

2929
//The dinosaur shoots every 2 seconds
3030
if (timer.elapsed(Duration.seconds(1.5)) && entity.getPosition().getY() > 0)
@@ -41,9 +41,9 @@ public void onUpdate(double ptf) {
4141
public void shoot() {
4242
FXGL.play(GameConstants.ENEMYSHOOT_SOUND);
4343
Point2D center = entity.getCenter();
44-
Vec2 direction = Vec2.fromAngle(entity.getRotation() +90);
44+
Vec2 direction = Vec2.fromAngle(entity.getRotation() + difficulty.getAngleOffset());
4545
spawn("basicEnemyProjectile",
46-
new SpawnData(center.getX() + 50 +3, center.getY())
46+
new SpawnData(center.getX(), center.getY())
4747
.put("direction", direction.toPoint2D() )
4848
);
4949
}

src/main/java/com/dinosaur/dinosaurexploder/model/LifeComponent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void onUpdate(double ptf)
3939
lifeText.setFill(Color.RED);
4040
lifeText.setFont(Font.font(GameConstants.ARCADECLASSIC_FONTNAME, 20));
4141

42-
// Adjusting Hearts with respect to text and eachother
42+
// Adjusting Hearts with respect to text and each other
4343
test1.setLayoutY(10);
4444
test2.setLayoutY(10);
4545
test3.setLayoutY(10);

0 commit comments

Comments
 (0)