|
1 | 1 | package nl.hsleiden.joshuabloch; |
2 | 2 |
|
| 3 | +import com.almasb.fxgl.app.scene.Viewport; |
3 | 4 | import com.almasb.fxgl.core.serialization.Bundle; |
| 5 | +import com.almasb.fxgl.dsl.FXGL; |
4 | 6 | import com.almasb.fxgl.entity.level.Level; |
5 | 7 | import com.almasb.fxgl.profile.DataFile; |
6 | | -import com.almasb.fxgl.profile.SaveFile; |
7 | 8 | import com.almasb.fxgl.profile.SaveLoadHandler; |
8 | 9 | import nl.hsleiden.joshuabloch.game.EntityManager; |
9 | 10 | import org.jetbrains.annotations.NotNull; |
10 | 11 |
|
11 | 12 | import java.util.HashMap; |
| 13 | +import java.util.function.Function; |
12 | 14 |
|
13 | 15 | import static com.almasb.fxgl.dsl.FXGL.*; |
14 | 16 |
|
15 | 17 | public class LevelManager { |
16 | 18 |
|
17 | 19 | public int levelProgress = 0; |
| 20 | + public int currentLevelID = 0; |
18 | 21 | private String name; |
19 | 22 | private Level currentLevel; |
20 | 23 | private HashMap<Integer, Integer> highscores = new HashMap<>(); |
21 | 24 |
|
22 | | - public void addHandler() { |
| 25 | + private final ScoreCounter scoreCounter; |
| 26 | + private final Main main; |
| 27 | + |
| 28 | + public LevelManager(ScoreCounter scoreCounter, Main main) { |
| 29 | + this.scoreCounter = scoreCounter; |
| 30 | + this.main = main; |
| 31 | + } |
| 32 | + |
| 33 | + public void addSaveHandler() { |
23 | 34 | getSaveLoadService().addHandler(new SaveLoadHandler() { |
24 | 35 | @Override |
25 | 36 | public void onSave(@NotNull DataFile data) { |
26 | 37 | Bundle bundle; |
27 | 38 | try { |
28 | | - bundle = data.getBundle(name); |
29 | | - } catch(IllegalArgumentException e) { |
30 | 39 | bundle = new Bundle(name); |
| 40 | + bundle.put("progress", levelProgress); |
| 41 | + bundle.put("highscores", highscores); |
| 42 | + data.putBundle(bundle); |
| 43 | + } catch(IllegalArgumentException e) { |
| 44 | + bundle = data.getBundle(name); |
| 45 | + bundle.put("progress", levelProgress); |
| 46 | + bundle.put("highscores", highscores); |
| 47 | + data.putBundle(bundle); |
31 | 48 | } |
32 | | - System.out.println(bundle.getName()); |
33 | | - bundle.put("progress", levelProgress); |
34 | | - bundle.put("highscores", highscores); |
35 | | - data.putBundle(bundle); |
36 | 49 | } |
37 | 50 |
|
38 | 51 | @Override |
39 | 52 | public void onLoad(@NotNull DataFile data) { |
40 | | - Bundle bundle = data.getBundle(name); |
41 | | - levelProgress = bundle.get("progress"); |
42 | | - highscores = bundle.get("highscores"); |
43 | | - System.out.println((int) data.getBundle(name).get("progress")); |
| 53 | + Bundle bundle; |
| 54 | + try { |
| 55 | + bundle = data.getBundle(name); |
| 56 | + levelProgress = bundle.get("progress"); |
| 57 | + highscores = bundle.get("highscores"); |
| 58 | + } catch (Exception e) { |
| 59 | + System.out.println("Standard values"); |
| 60 | + levelProgress = 0; |
| 61 | + highscores = new HashMap<>(); |
| 62 | + } |
44 | 63 | } |
45 | 64 | }); |
46 | 65 | } |
47 | 66 |
|
48 | | - public void setName(String name) { |
| 67 | + public void setName(String name, Function<Void, Void> callback) { |
49 | 68 | this.name = name; |
| 69 | + loadSave(r -> callback.apply(null)); |
| 70 | + } |
| 71 | + |
| 72 | + public String getName() { |
| 73 | + return name; |
50 | 74 | } |
51 | 75 |
|
52 | | - public int getHighScore(int levelID) { |
53 | | - return highscores.getOrDefault(levelID, 0); |
| 76 | + public Integer getHighScore(int levelID) { |
| 77 | + return highscores.getOrDefault(levelID, null); |
54 | 78 | } |
55 | 79 |
|
56 | | - public void loadSave() { |
57 | | - getSaveLoadService().readAndLoadTask("highscores.sav").run(); |
| 80 | + public void loadSave(Function<Void, Void> callback) { |
| 81 | + if(getFileSystemService().exists("highscores.sav")) { |
| 82 | + System.out.println("exists"); |
| 83 | + getSaveLoadService().readAndLoadTask("highscores.sav").thenWrap(n -> callback.apply(null)).run(); |
| 84 | + } else { |
| 85 | + System.out.println("no exist"); |
| 86 | + levelProgress = 0; |
| 87 | + highscores = new HashMap<>(); |
| 88 | + writeSave(); |
| 89 | + System.out.println("Standard values"); |
| 90 | + callback.apply(null); |
| 91 | + } |
58 | 92 | } |
59 | 93 |
|
60 | 94 | public void writeSave() { |
61 | 95 | getSaveLoadService().saveAndWriteTask("highscores.sav").run(); |
62 | 96 | } |
63 | 97 |
|
64 | 98 | public void finished(int coins) { |
65 | | - highscores.put(levelProgress, coins); |
66 | | - levelProgress++; |
| 99 | + if(getHighScore(currentLevelID) != null && getHighScore(currentLevelID) < coins) { |
| 100 | + highscores.put(currentLevelID, coins); |
| 101 | + } else if(getHighScore(currentLevelID) == null) { |
| 102 | + highscores.put(currentLevelID, coins); |
| 103 | + } |
| 104 | + if(currentLevelID == levelProgress && levelProgress != 3) levelProgress++; //Do not increment when playing already unlocked levels |
| 105 | + writeSave(); |
67 | 106 | } |
68 | 107 |
|
69 | 108 | public Level getCurrentLevel() { |
70 | 109 | return currentLevel; |
71 | 110 | } |
72 | 111 |
|
73 | | - public void start() { |
| 112 | + public void start(int levelID) { |
| 113 | + currentLevelID = levelID; |
| 114 | + FXGL.getGameController().startNewGame(); |
| 115 | + } |
| 116 | + |
| 117 | + public void initGame() { |
74 | 118 | getGameWorld().addEntityFactory(new EntityManager(this)); |
75 | | - currentLevel = setLevelFromMap("tmx/level_" + (levelProgress + 1) + ".tmx"); |
| 119 | + currentLevel = setLevelFromMap("tmx/level_" + (currentLevelID + 1) + ".tmx"); |
| 120 | + //currentLevel = setLevelFromMap("tmx/level_1.tmx"); |
| 121 | + |
| 122 | + main.player = spawn("player", 50, 50); |
| 123 | + |
| 124 | + scoreCounter.showCost(Integer.parseInt(currentLevel.getProperties().getString("cost"))); |
| 125 | + |
76 | 126 | spawn("background"); |
| 127 | + |
| 128 | + Viewport viewport = getGameScene().getViewport(); |
| 129 | + viewport.setBounds(-1500, 0, 130 * 32, getAppHeight()); |
| 130 | + viewport.bindToEntity(main.player, getAppWidth() / 2f, getAppHeight() /2f); |
| 131 | + viewport.setLazy(true); |
77 | 132 | } |
78 | 133 |
|
79 | 134 | } |
0 commit comments