|
1 | 1 | package com.dinosaur.dinosaurexploder.achievements; |
2 | 2 |
|
3 | 3 | import com.dinosaur.dinosaurexploder.constants.GameConstants; |
| 4 | + |
4 | 5 | import java.io.*; |
5 | 6 | import java.util.ArrayList; |
6 | 7 | import java.util.List; |
7 | 8 |
|
8 | 9 | public class AchievementManager { |
9 | 10 |
|
10 | | - private final List<Achievement> allAchievements = new ArrayList<>(); |
11 | | - private final List<Achievement> activeAchievements = new ArrayList<>(); |
12 | | - |
13 | | - |
14 | | - public AchievementManager() { |
15 | | - // Register all available achievements here |
16 | | - allAchievements.add(new KillCountAchievement(1, 50)); |
17 | | - allAchievements.add(new KillCountAchievement(2, 50)); |
18 | | - allAchievements.add(new KillCountAchievement(10, 50)); |
19 | | - allAchievements.add(new KillCountAchievement(20, 100)); |
20 | | - } |
| 11 | + private final List<Achievement> allAchievements = new ArrayList<>(); |
| 12 | + private final List<Achievement> activeAchievements = new ArrayList<>(); |
21 | 13 |
|
22 | | - // Called once when the game starts |
23 | | - public void init() { |
24 | | - if (allAchievements.isEmpty()) return; |
| 14 | + public AchievementManager() { |
| 15 | + // Register all available achievements here |
| 16 | + allAchievements.add(new KillCountAchievement(1, 50)); |
| 17 | + allAchievements.add(new KillCountAchievement(2, 50)); |
| 18 | + allAchievements.add(new KillCountAchievement(10, 50)); |
| 19 | + allAchievements.add(new KillCountAchievement(20, 100)); |
| 20 | + } |
25 | 21 |
|
26 | | - activeAchievements.addAll(loadAchievement()); |
27 | | - if (activeAchievements.isEmpty()) |
28 | | - { |
29 | | - saveAchievement(allAchievements); |
30 | | - activeAchievements.addAll(allAchievements); |
31 | | - } |
32 | | - } |
| 22 | + // Called once when the game starts |
| 23 | + public void init() { |
| 24 | + if (allAchievements.isEmpty()) return; |
33 | 25 |
|
34 | | - // Called every frame |
35 | | - public void update(double tpf) { |
36 | | - for (Achievement achievement : activeAchievements) { |
37 | | - if (!achievement.isCompleted()) { |
38 | | - achievement.update(tpf); |
39 | | - } |
40 | | - } |
41 | | - } |
| 26 | + activeAchievements.addAll(loadAchievement()); |
| 27 | + if (activeAchievements.isEmpty()) { |
| 28 | + saveAchievement(allAchievements); |
| 29 | + activeAchievements.addAll(allAchievements); |
| 30 | + } |
| 31 | + } |
42 | 32 |
|
43 | | - // Called when a dinosaur is killed |
44 | | - public void notifyDinosaurKilled() { |
45 | | - for (Achievement achievement : activeAchievements) { |
46 | | - Boolean complete = achievement.onDinosaurKilled(); |
47 | | - } |
48 | | - saveAchievement(activeAchievements); |
| 33 | + // Called every frame |
| 34 | + public void update(double tpf) { |
| 35 | + for (Achievement achievement : activeAchievements) { |
| 36 | + if (!achievement.isCompleted()) { |
| 37 | + achievement.update(tpf); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
49 | 41 |
|
50 | | - } |
| 42 | + // Called when a dinosaur is killed |
| 43 | + public void notifyDinosaurKilled() { |
| 44 | + for (Achievement achievement : activeAchievements) { |
| 45 | + Boolean complete = achievement.onDinosaurKilled(); |
| 46 | + } |
| 47 | + saveAchievement(activeAchievements); |
| 48 | + } |
51 | 49 |
|
52 | | - public List<Achievement> getActiveAchievements() { |
53 | | - return activeAchievements; |
54 | | - } |
| 50 | + public List<Achievement> getActiveAchievements() { |
| 51 | + return activeAchievements; |
| 52 | + } |
55 | 53 |
|
56 | | - public Achievement getActiveAchievement() { |
57 | | - if (activeAchievements.isEmpty()) { |
58 | | - return null; |
59 | | - } |
60 | | - return activeAchievements.getFirst(); |
61 | | - } |
| 54 | + public Achievement getActiveAchievement() { |
| 55 | + if (activeAchievements.isEmpty()) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + return activeAchievements.getFirst(); |
| 59 | + } |
62 | 60 |
|
63 | | -///Get the list of achievement save in the achievement.ser file |
64 | | - public List<Achievement> loadAchievement() { |
65 | | - List<Achievement> achievementFromFile = new ArrayList<>(); |
66 | | - try (ObjectInputStream in = |
67 | | - new ObjectInputStream(new FileInputStream(GameConstants.ACHIEVEMENTS_FILE))) { |
68 | | - achievementFromFile= (List<Achievement>) in.readObject(); |
69 | | - } catch (IOException | ClassNotFoundException e) { |
70 | | - System.out.println("Failed to load achievements from file"); |
71 | | - } |
72 | | - return achievementFromFile; |
73 | | - } |
| 61 | + /// Get the list of achievement save in the achievement.ser file |
| 62 | + public List<Achievement> loadAchievement() { |
| 63 | + List<Achievement> achievementFromFile = new ArrayList<>(); |
| 64 | + try (ObjectInputStream in = |
| 65 | + new ObjectInputStream(new FileInputStream(GameConstants.ACHIEVEMENTS_FILE))) { |
| 66 | + achievementFromFile = (List<Achievement>) in.readObject(); |
| 67 | + } catch (IOException | ClassNotFoundException e) { |
| 68 | + System.out.println("Failed to load achievements from file"); |
| 69 | + } |
| 70 | + return achievementFromFile; |
| 71 | + } |
74 | 72 |
|
75 | | - /// Save activeAchievement in the achievement.ser file |
76 | | - public void saveAchievement(List<Achievement> listToSave) { |
77 | | - try (ObjectOutputStream out = |
78 | | - new ObjectOutputStream(new FileOutputStream(GameConstants.ACHIEVEMENTS_FILE))) { |
79 | | - out.writeObject(listToSave); |
80 | | - } catch (IOException e) { |
81 | | - System.err.println("Error saving achievement : " + e.getMessage()); |
82 | | - } |
83 | | - } |
| 73 | + /// Save activeAchievement in the achievement.ser file |
| 74 | + public void saveAchievement(List<Achievement> listToSave) { |
| 75 | + try (ObjectOutputStream out = |
| 76 | + new ObjectOutputStream(new FileOutputStream(GameConstants.ACHIEVEMENTS_FILE))) { |
| 77 | + out.writeObject(listToSave); |
| 78 | + } catch (IOException e) { |
| 79 | + System.err.println("Error saving achievement : " + e.getMessage()); |
| 80 | + } |
| 81 | + } |
84 | 82 | } |
0 commit comments