Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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,29 @@
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

import java.io.Serializable;

public abstract class Achievement implements Serializable {

protected boolean completed = false;
protected int rewardCoins;


public boolean isCompleted() {
return completed;
}


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

public int getRewardCoins() {
return rewardCoins;
}

public abstract void update(double tpf);

public abstract void onDinosaurKilled();
public abstract Boolean onDinosaurKilled();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dinosaur.dinosaurexploder.achievements;

import com.dinosaur.dinosaurexploder.constants.GameConstants;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -8,8 +10,11 @@ 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(1, 50));
allAchievements.add(new KillCountAchievement(2, 50));
allAchievements.add(new KillCountAchievement(10, 50));
allAchievements.add(new KillCountAchievement(20, 100));
}
Expand All @@ -18,7 +23,12 @@ public AchievementManager() {
public void init() {
if (allAchievements.isEmpty()) return;

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

// Called every frame
Expand All @@ -33,8 +43,10 @@ public void update(double tpf) {
// Called when a dinosaur is killed
public void notifyDinosaurKilled() {
for (Achievement achievement : activeAchievements) {
achievement.onDinosaurKilled();
Boolean complete = achievement.onDinosaurKilled();
}
saveAchievement(activeAchievements);

}

public List<Achievement> getActiveAchievements() {
Expand All @@ -45,6 +57,28 @@ public Achievement getActiveAchievement() {
if (activeAchievements.isEmpty()) {
return null;
}
return activeAchievements.get(0);
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;
}

/// 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,14 +1,10 @@
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 int currentKills = 0;
private boolean completed = false;

public KillCountAchievement(int targetKills, int rewardCoins) {
this.targetKills = targetKills;
Expand All @@ -19,31 +15,23 @@ public String getDescription() {
return "Kill " + targetKills + " dinosaurs";
}

public boolean isCompleted() {
return completed;
}

public void onDinosaurKilled() {
if (completed) return;
public Boolean onDinosaurKilled() {
if (completed) return(true);

currentKills++;

if (currentKills >= targetKills) {
completed = true;
onComplete();
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());
}

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 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,43 @@
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;
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