-
-
Notifications
You must be signed in to change notification settings - Fork 162
Persists achievements when we start the game/get a new one #342
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 5 commits
7270a02
b031ed9
52a432f
8f8d940
de127f2
ee87307
68a73a4
2a86198
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
| 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,14 +1,10 @@ | ||||||||||||||||||||
| package com.dinosaur.dinosaurexploder.achievements; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import com.almasb.fxgl.dsl.FXGL; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Contributor
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. [💄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; | ||||||||||||||||||||
|
|
@@ -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()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Contributor
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. [💄Spotless - Format] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||
| public int getRewardCoins() { | ||||||||||||||||||||
| return rewardCoins; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Contributor
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. [💄Spotless - Format] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||
| 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; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Contributor
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. [💄Spotless - Format] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| 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
Contributor
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. [💄Spotless - Format] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| List<Achievement> emptyList = new ArrayList<>(); | ||||||||||||||||||||
| currentAchievement = achievementManager.loadAchievement(); | ||||||||||||||||||||
| achievementManager.saveAchievement(emptyList); | ||||||||||||||||||||
| achievementManager.init(); | ||||||||||||||||||||
|
Comment on lines
+22
to
+25
Contributor
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. [💄Spotless - Format] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| } | ||||||||||||||||||||
|
Contributor
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. [💄Spotless - Format] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| @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); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
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.
[💄Spotless - Format] reported by reviewdog 🐶