Skip to content
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
Binary file added achievements.ser
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package com.dinosaur.dinosaurexploder.achievements;

public abstract class Achievement {
import com.almasb.fxgl.dsl.FXGL;

Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change

protected boolean completed = false;
import java.io.Serializable;

public boolean isCompleted() {
return completed;
}
public abstract class Achievement implements Serializable {

public abstract void update(double tpf);
protected boolean completed = false;
protected int rewardCoins;
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
protected boolean completed = false;
protected int rewardCoins;
protected boolean completed = false;
protected int rewardCoins;


public abstract void onDinosaurKilled();
public boolean isCompleted() {
return completed;
}
Comment on lines +12 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public boolean isCompleted() {
return completed;
}
public boolean isCompleted() {
return completed;
}


public void onComplete(String description) {
FXGL.getNotificationService().pushNotification("Achievement unlocked: " + description);
}
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public void onComplete(String description) {
FXGL.getNotificationService().pushNotification("Achievement unlocked: " + description);
}
public void onComplete(String description) {
FXGL.getNotificationService().pushNotification("Achievement unlocked: " + description);
}


public int getRewardCoins() {
return rewardCoins;
}
Comment on lines +20 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public int getRewardCoins() {
return rewardCoins;
}
public int getRewardCoins() {
return rewardCoins;
}


public abstract void update(double tpf);
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public abstract void update(double tpf);
public abstract void update(double tpf);


public abstract Boolean onDinosaurKilled();
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public abstract Boolean onDinosaurKilled();
public abstract Boolean onDinosaurKilled();

}
Original file line number Diff line number Diff line change
@@ -1,50 +1,82 @@
package com.dinosaur.dinosaurexploder.achievements;

import com.dinosaur.dinosaurexploder.constants.GameConstants;

Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class AchievementManager {

private final List<Achievement> allAchievements = new ArrayList<>();
private final List<Achievement> activeAchievements = new ArrayList<>();

public AchievementManager() {
// Register all available achievements here
allAchievements.add(new KillCountAchievement(10, 50));
allAchievements.add(new KillCountAchievement(20, 100));
}

// Called once when the game starts
public void init() {
if (allAchievements.isEmpty()) return;

activeAchievements.addAll(allAchievements);
}

// Called every frame
public void update(double tpf) {
for (Achievement achievement : activeAchievements) {
if (!achievement.isCompleted()) {
achievement.update(tpf);
}
}
}

// Called when a dinosaur is killed
public void notifyDinosaurKilled() {
for (Achievement achievement : activeAchievements) {
achievement.onDinosaurKilled();
}
}

public List<Achievement> getActiveAchievements() {
return activeAchievements;
}

public Achievement getActiveAchievement() {
if (activeAchievements.isEmpty()) {
return null;
}
return activeAchievements.get(0);
}
private final List<Achievement> allAchievements = new ArrayList<>();
private final List<Achievement> activeAchievements = new ArrayList<>();
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
private final List<Achievement> allAchievements = new ArrayList<>();
private final List<Achievement> activeAchievements = new ArrayList<>();
private final List<Achievement> allAchievements = new ArrayList<>();
private final List<Achievement> activeAchievements = new ArrayList<>();


public AchievementManager() {
// Register all available achievements here
allAchievements.add(new KillCountAchievement(1, 50));
allAchievements.add(new KillCountAchievement(2, 50));
allAchievements.add(new KillCountAchievement(10, 50));
allAchievements.add(new KillCountAchievement(20, 100));
}
Comment on lines +14 to +20
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public AchievementManager() {
// Register all available achievements here
allAchievements.add(new KillCountAchievement(1, 50));
allAchievements.add(new KillCountAchievement(2, 50));
allAchievements.add(new KillCountAchievement(10, 50));
allAchievements.add(new KillCountAchievement(20, 100));
}
public AchievementManager() {
// Register all available achievements here
allAchievements.add(new KillCountAchievement(1, 50));
allAchievements.add(new KillCountAchievement(2, 50));
allAchievements.add(new KillCountAchievement(10, 50));
allAchievements.add(new KillCountAchievement(20, 100));
}


// Called once when the game starts
public void init() {
if (allAchievements.isEmpty()) return;
Comment on lines +22 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
// Called once when the game starts
public void init() {
if (allAchievements.isEmpty()) return;
// Called once when the game starts
public void init() {
if (allAchievements.isEmpty()) return;


activeAchievements.addAll(loadAchievement());
if (activeAchievements.isEmpty()) {
saveAchievement(allAchievements);
activeAchievements.addAll(allAchievements);
}
}
Comment on lines +26 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
activeAchievements.addAll(loadAchievement());
if (activeAchievements.isEmpty()) {
saveAchievement(allAchievements);
activeAchievements.addAll(allAchievements);
}
}
activeAchievements.addAll(loadAchievement());
if (activeAchievements.isEmpty()) {
saveAchievement(allAchievements);
activeAchievements.addAll(allAchievements);
}
}


// Called every frame
public void update(double tpf) {
for (Achievement achievement : activeAchievements) {
if (!achievement.isCompleted()) {
achievement.update(tpf);
}
}
}
Comment on lines +33 to +40
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
// Called every frame
public void update(double tpf) {
for (Achievement achievement : activeAchievements) {
if (!achievement.isCompleted()) {
achievement.update(tpf);
}
}
}
// Called every frame
public void update(double tpf) {
for (Achievement achievement : activeAchievements) {
if (!achievement.isCompleted()) {
achievement.update(tpf);
}
}
}


// Called when a dinosaur is killed
public void notifyDinosaurKilled() {
for (Achievement achievement : activeAchievements) {
Boolean complete = achievement.onDinosaurKilled();
}
saveAchievement(activeAchievements);
}
Comment on lines +42 to +48
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
// Called when a dinosaur is killed
public void notifyDinosaurKilled() {
for (Achievement achievement : activeAchievements) {
Boolean complete = achievement.onDinosaurKilled();
}
saveAchievement(activeAchievements);
}
// Called when a dinosaur is killed
public void notifyDinosaurKilled() {
for (Achievement achievement : activeAchievements) {
Boolean complete = achievement.onDinosaurKilled();
}
saveAchievement(activeAchievements);
}


public List<Achievement> getActiveAchievements() {
return activeAchievements;
}
Comment on lines +50 to +52
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public List<Achievement> getActiveAchievements() {
return activeAchievements;
}
public List<Achievement> getActiveAchievements() {
return activeAchievements;
}


public Achievement getActiveAchievement() {
if (activeAchievements.isEmpty()) {
return null;
}
return activeAchievements.getFirst();
}
Comment on lines +54 to +59
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public Achievement getActiveAchievement() {
if (activeAchievements.isEmpty()) {
return null;
}
return activeAchievements.getFirst();
}
public Achievement getActiveAchievement() {
if (activeAchievements.isEmpty()) {
return null;
}
return activeAchievements.getFirst();
}


/// Get the list of achievement save in the achievement.ser file
public List<Achievement> loadAchievement() {
List<Achievement> achievementFromFile = new ArrayList<>();
try (ObjectInputStream in =
new ObjectInputStream(new FileInputStream(GameConstants.ACHIEVEMENTS_FILE))) {
achievementFromFile = (List<Achievement>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("Failed to load achievements from file");
}
return achievementFromFile;
}
Comment on lines +61 to +71
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
/// Get the list of achievement save in the achievement.ser file
public List<Achievement> loadAchievement() {
List<Achievement> achievementFromFile = new ArrayList<>();
try (ObjectInputStream in =
new ObjectInputStream(new FileInputStream(GameConstants.ACHIEVEMENTS_FILE))) {
achievementFromFile = (List<Achievement>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("Failed to load achievements from file");
}
return achievementFromFile;
}
/// Get the list of achievement save in the achievement.ser file
public List<Achievement> loadAchievement() {
List<Achievement> achievementFromFile = new ArrayList<>();
try (ObjectInputStream in =
new ObjectInputStream(new FileInputStream(GameConstants.ACHIEVEMENTS_FILE))) {
achievementFromFile = (List<Achievement>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("Failed to load achievements from file");
}
return achievementFromFile;
}


/// Save activeAchievement in the achievement.ser file
public void saveAchievement(List<Achievement> listToSave) {
try (ObjectOutputStream out =
new ObjectOutputStream(new FileOutputStream(GameConstants.ACHIEVEMENTS_FILE))) {
out.writeObject(listToSave);
} catch (IOException e) {
System.err.println("Error saving achievement : " + e.getMessage());
}
}
Comment on lines +73 to +81
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
/// Save activeAchievement in the achievement.ser file
public void saveAchievement(List<Achievement> listToSave) {
try (ObjectOutputStream out =
new ObjectOutputStream(new FileOutputStream(GameConstants.ACHIEVEMENTS_FILE))) {
out.writeObject(listToSave);
} catch (IOException e) {
System.err.println("Error saving achievement : " + e.getMessage());
}
}
/// Save activeAchievement in the achievement.ser file
public void saveAchievement(List<Achievement> listToSave) {
try (ObjectOutputStream out =
new ObjectOutputStream(new FileOutputStream(GameConstants.ACHIEVEMENTS_FILE))) {
out.writeObject(listToSave);
} catch (IOException e) {
System.err.println("Error saving achievement : " + e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,49 +1,37 @@
package com.dinosaur.dinosaurexploder.achievements;

import com.almasb.fxgl.dsl.FXGL;

Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change

public class KillCountAchievement extends Achievement {

private final int targetKills;
private final int rewardCoins;
private final int targetKills;
private int currentKills = 0;

private int currentKills = 0;
private boolean completed = false;
public KillCountAchievement(int targetKills, int rewardCoins) {
this.targetKills = targetKills;
this.rewardCoins = rewardCoins;
}

public KillCountAchievement(int targetKills, int rewardCoins) {
this.targetKills = targetKills;
this.rewardCoins = rewardCoins;
}
public String getDescription() {
return "Kill " + targetKills + " dinosaurs";
}
Comment on lines +6 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
private final int targetKills;
private int currentKills = 0;
private int currentKills = 0;
private boolean completed = false;
public KillCountAchievement(int targetKills, int rewardCoins) {
this.targetKills = targetKills;
this.rewardCoins = rewardCoins;
}
public KillCountAchievement(int targetKills, int rewardCoins) {
this.targetKills = targetKills;
this.rewardCoins = rewardCoins;
}
public String getDescription() {
return "Kill " + targetKills + " dinosaurs";
}
private final int targetKills;
private int currentKills = 0;


public String getDescription() {
return "Kill " + targetKills + " dinosaurs";
}
public Boolean onDinosaurKilled() {
if (completed) return (true);
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
public Boolean onDinosaurKilled() {
if (completed) return (true);
public KillCountAchievement(int targetKills, int rewardCoins) {
this.targetKills = targetKills;
this.rewardCoins = rewardCoins;
}


public boolean isCompleted() {
return completed;
}
currentKills++;
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
currentKills++;
public String getDescription() {
return "Kill " + targetKills + " dinosaurs";
}


public void onDinosaurKilled() {
if (completed) return;
if (currentKills >= targetKills) {
completed = true;
onComplete(getDescription());
return (true);
}
return (false);
}
Comment on lines +23 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
if (currentKills >= targetKills) {
completed = true;
onComplete(getDescription());
return (true);
}
return (false);
}
public Boolean onDinosaurKilled() {
if (completed) return (true);


currentKills++;
@Override
public void update(double tpf) {
// Not needed for count-based achievement
}
Comment on lines +31 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
@Override
public void update(double tpf) {
// Not needed for count-based achievement
}
currentKills++;


if (currentKills >= targetKills) {
completed = true;
onComplete();
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
if (currentKills >= targetKills) {
completed = true;
onComplete(getDescription());
return (true);
}
return (false);
}

@Override
public void update(double tpf) {
// Not needed for count-based achievement
}

public void onComplete() {
FXGL.getNotificationService().pushNotification("Achievement unlocked: " + getDescription());
}

public int getRewardCoins() {
return rewardCoins;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
}
@Override
public void update(double tpf) {
// Not needed for count-based achievement
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private GameConstants() {}
*/
public static final String HIGH_SCORE_FILE = "highScore.ser";
public static final String TOTAL_COINS_FILE = "totalCoins.ser";
public static final String ACHIEVEMENTS_FILE = "achievements.ser";

/*
* CONSTANTS FOR TEXT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.dinosaur.dinosaurexploder.achievements;

import com.dinosaur.dinosaurexploder.utils.LevelManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Comment on lines +4 to +7
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


public class AchievementTest {

private LevelManager levelManager;
private List<Achievement> currentAchievement = new ArrayList<>();
AchievementManager achievementManager = new AchievementManager();

@BeforeEach
void setUp() {
Comment on lines +15 to +20
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
private LevelManager levelManager;
private List<Achievement> currentAchievement = new ArrayList<>();
AchievementManager achievementManager = new AchievementManager();
@BeforeEach
void setUp() {
private LevelManager levelManager;
private List<Achievement> currentAchievement = new ArrayList<>();
AchievementManager achievementManager = new AchievementManager();


List<Achievement> emptyList = new ArrayList<>();
currentAchievement = achievementManager.loadAchievement();
achievementManager.saveAchievement(emptyList);
achievementManager.init();
Comment on lines +22 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
List<Achievement> emptyList = new ArrayList<>();
currentAchievement = achievementManager.loadAchievement();
achievementManager.saveAchievement(emptyList);
achievementManager.init();
@BeforeEach
void setUp() {


}
Copy link
Contributor

Choose a reason for hiding this comment

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

[💄Spotless - Format] reported by reviewdog 🐶

Suggested change
}
List<Achievement> emptyList = new ArrayList<>();
currentAchievement = achievementManager.loadAchievement();
achievementManager.saveAchievement(emptyList);
achievementManager.init();
}


@Test
void achievementSaveInFile() {
List<Achievement> listToCheck;

achievementManager.getActiveAchievement().completed = true;
achievementManager.saveAchievement(achievementManager.getActiveAchievements());
listToCheck = achievementManager.loadAchievement();
assert listToCheck.getFirst().isCompleted();
}

@AfterEach
void setAchievementBack() {

achievementManager.saveAchievement(currentAchievement);
}
}
Loading