summaryrefslogtreecommitdiff
path: root/core/src/kz/ilotterytea/maxon/player
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-06-08 16:30:25 +0500
committerilotterytea <iltsu@alright.party>2024-06-09 19:27:32 +0500
commit604832dddef852a0a874b9dfbf3540a8a56736c0 (patch)
tree07604a0be9258f2c7568ad64d9dc0c7160b227f6 /core/src/kz/ilotterytea/maxon/player
parentdc99bd738b1a4b38112f767c3253724d0ff0587c (diff)
feat/upd: new savegame loader + updated constructors
Diffstat (limited to 'core/src/kz/ilotterytea/maxon/player')
-rw-r--r--core/src/kz/ilotterytea/maxon/player/DecalPlayer.java6
-rw-r--r--core/src/kz/ilotterytea/maxon/player/MaxonSavegame.java40
-rw-r--r--core/src/kz/ilotterytea/maxon/player/Savegame.java130
3 files changed, 133 insertions, 43 deletions
diff --git a/core/src/kz/ilotterytea/maxon/player/DecalPlayer.java b/core/src/kz/ilotterytea/maxon/player/DecalPlayer.java
index ca6d65e..60faf9b 100644
--- a/core/src/kz/ilotterytea/maxon/player/DecalPlayer.java
+++ b/core/src/kz/ilotterytea/maxon/player/DecalPlayer.java
@@ -14,9 +14,9 @@ public class DecalPlayer {
private int regionIndex;
private final Decal decal;
private final BoundingBox box;
- private final MaxonSavegame savegame;
+ private final Savegame savegame;
- public DecalPlayer(MaxonSavegame savegame, TextureRegion[] regions) {
+ public DecalPlayer(Savegame savegame, TextureRegion[] regions) {
this.savegame = savegame;
this.regions = regions;
@@ -55,7 +55,7 @@ public class DecalPlayer {
if (Intersector.intersectRayBounds(ray, box, intersection)) {
updateTextureRegion();
- savegame.points++;
+ savegame.increaseMoney(1);
}
}
diff --git a/core/src/kz/ilotterytea/maxon/player/MaxonSavegame.java b/core/src/kz/ilotterytea/maxon/player/MaxonSavegame.java
deleted file mode 100644
index e0fe34c..0000000
--- a/core/src/kz/ilotterytea/maxon/player/MaxonSavegame.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package kz.ilotterytea.maxon.player;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-
-/**
- * Save-game data class.
- * @author NotDankEnough
- * @since Alpha 1.2 (Oct 02, 2022)
- */
-public class MaxonSavegame implements Serializable {
- /** Earned Squish Points. */
- public long points = 0L;
- /** Multiplier. */
- public long multiplier = 1;
-
- /** Home inventory. */
- public ArrayList<Integer> inv = new ArrayList<>();
- /** Outside Inventory. */
- public ArrayList<Integer> outInv = new ArrayList<>();
-
- /** Seed. */
- public long seed = System.currentTimeMillis();
-
- /** Player name. */
- public String name = System.getProperty("user.name");
- /** Pet name. */
- public String petName = "Maxon";
- /** Pet ID. */
- public byte petId = 0;
-
- /** Elapsed time from game start. */
- public long elapsedTime = 0;
-
- /** Last timestamp when save game was used. */
- public long lastTimestamp = System.currentTimeMillis();
-
- /** Location. */
- public short roomId = 0;
-} \ No newline at end of file
diff --git a/core/src/kz/ilotterytea/maxon/player/Savegame.java b/core/src/kz/ilotterytea/maxon/player/Savegame.java
new file mode 100644
index 0000000..fb955c6
--- /dev/null
+++ b/core/src/kz/ilotterytea/maxon/player/Savegame.java
@@ -0,0 +1,130 @@
+package kz.ilotterytea.maxon.player;
+
+
+import com.badlogic.gdx.Gdx;
+import com.google.gson.Gson;
+import kz.ilotterytea.maxon.MaxonConstants;
+import kz.ilotterytea.maxon.utils.OsUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+import java.util.ArrayList;
+
+public class Savegame implements Serializable {
+ private static final File directory = new File(MaxonConstants.GAME_SAVEGAME_FOLDER);
+ private static final File file = new File(
+ String.format(
+ "%s/savegame.maxon",
+ (OsUtils.isAndroid || OsUtils.isIos)
+ ? Gdx.files.getExternalStoragePath()
+ : directory.getAbsolutePath()
+ )
+ );
+ private static final Gson gson = new Gson();
+ private static final Logger logger = LoggerFactory.getLogger(Savegame.class);
+
+ private double money = 0.0f, multiplier = 0.0f;
+ private final ArrayList<Integer> purchasedPets = new ArrayList<>();
+ private String name = System.getProperty("user.name", "Maxon");
+ private long elapsedTime = 0;
+ private boolean isNewlyCreated = true;
+
+ private Savegame() {}
+
+ public static Savegame load() {
+ if (!file.exists()) {
+ return new Savegame();
+ }
+
+ try {
+ FileInputStream fis = new FileInputStream(file);
+ ObjectInputStream ois = new ObjectInputStream(fis);
+
+ Savegame savegame = gson.fromJson(ois.readUTF(), Savegame.class);
+ ois.close();
+
+ savegame.isNewlyCreated = false;
+
+ logger.info("Loaded the savegame");
+
+ return savegame;
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to load savegame", e);
+ }
+ }
+
+ public void save() {
+ if (!directory.exists()) {
+ directory.mkdirs();
+ }
+
+ try {
+ FileOutputStream fos = new FileOutputStream(file);
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+
+ oos.writeUTF(gson.toJson(this));
+ oos.close();
+
+ logger.info("Saved the game");
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to save the game", e);
+ }
+ }
+
+ public double getMoney() {
+ return money;
+ }
+
+ public void setMoney(double money) {
+ this.money = money;
+ }
+
+ public void increaseMoney(double money) {
+ this.money += money;
+ }
+
+ public void decreaseMoney(double money) {
+ this.money -= money;
+ }
+
+ public double getMultiplier() {
+ return multiplier;
+ }
+
+ public void setMultiplier(double multiplier) {
+ this.multiplier = multiplier;
+ }
+
+ public void increaseMultiplier(double multiplier) {
+ this.multiplier += multiplier;
+ }
+
+ public void decreaseMultiplier(double multiplier) {
+ this.multiplier -= multiplier;
+ }
+
+ public ArrayList<Integer> getPurchasedPets() {
+ return purchasedPets;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public long getElapsedTime() {
+ return elapsedTime;
+ }
+
+ public void setElapsedTime(long elapsedTime) {
+ this.elapsedTime = elapsedTime;
+ }
+
+ public boolean isNewlyCreated() {
+ return isNewlyCreated;
+ }
+}