Skip to content
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

5 difficulty levels added for issue #96 #120

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;

// 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);
}

// 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();
/**
* 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());

//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())
.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
Loading