summaryrefslogtreecommitdiff
path: root/core/src/com/ilotterytea/maxoning/utils
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2022-10-19 22:06:16 +0600
committerilotterytea <iltsu@alright.party>2022-10-19 22:06:16 +0600
commitbac4182ef9d1df55b3e2daad8be82f966a92b0dd (patch)
treee08143c54939c527a931fd23690f8fd6e73fc2b4 /core/src/com/ilotterytea/maxoning/utils
parent2434fafcdb07521c0db76037483d0c36c4280c2e (diff)
parentc792b675e340ade7e4fd6bd4e67265ba2b4cb1b5 (diff)
Resolved merge conflict
Diffstat (limited to 'core/src/com/ilotterytea/maxoning/utils')
-rw-r--r--core/src/com/ilotterytea/maxoning/utils/AssetLoading.java27
-rw-r--r--core/src/com/ilotterytea/maxoning/utils/formatters/NumberFormatter.java32
-rw-r--r--core/src/com/ilotterytea/maxoning/utils/math/Math.java13
-rw-r--r--core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java98
4 files changed, 135 insertions, 35 deletions
diff --git a/core/src/com/ilotterytea/maxoning/utils/AssetLoading.java b/core/src/com/ilotterytea/maxoning/utils/AssetLoading.java
index c8b76ba..aba31fa 100644
--- a/core/src/com/ilotterytea/maxoning/utils/AssetLoading.java
+++ b/core/src/com/ilotterytea/maxoning/utils/AssetLoading.java
@@ -3,6 +3,7 @@ package com.ilotterytea.maxoning.utils;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.ilotterytea.maxoning.anim.SpriteUtils;
import com.ilotterytea.maxoning.player.MaxonItemEnum;
import com.ilotterytea.maxoning.player.MaxonItemRegister;
@@ -10,10 +11,14 @@ import com.ilotterytea.maxoning.ui.AnimatedImage;
public class AssetLoading {
public static void queue(AssetManager am) {
-
- // Textures:
- am.load("sprites/supadank.png", Texture.class);
-
+ // Texture atlases:
+ am.load("sprites/env/environment.atlas", TextureAtlas.class);
+ am.load("sprites/gui/brand.atlas", TextureAtlas.class);
+ am.load("sprites/gui/icons.atlas", TextureAtlas.class);
+ am.load("sprites/gui/ilotterytea.atlas", TextureAtlas.class);
+ am.load("sprites/gui/widgets.atlas", TextureAtlas.class);
+
+ // Cat item textures:
am.load("sprites/sheet/loadingCircle.png", Texture.class);
am.load("sprites/sheet/bror.png", Texture.class);
am.load("sprites/sheet/manlooshka.png", Texture.class);
@@ -31,20 +36,6 @@ public class AssetLoading {
am.load("sprites/sheet/aeae.png", Texture.class);
am.load("sprites/sheet/succat.png", Texture.class);
- am.load("sprites/white.png", Texture.class);
- am.load("sprites/black.png", Texture.class);
- am.load("sprites/brand.png", Texture.class);
- am.load("sprites/ilotterytea.png", Texture.class);
-
- am.load("sprites/menu/tile_1.png", Texture.class);
- am.load("sprites/menu/tile_2.png", Texture.class);
-
- // // Ninepatches:
- am.load("sprites/ui/sqrbutton.png", Texture.class);
- am.load("sprites/ui/sqrbutton_down.png", Texture.class);
- am.load("sprites/ui/sqrbutton_over.png", Texture.class);
- am.load("sprites/ui/sqrbutton_disabled.png", Texture.class);
-
// Music:
am.load("mus/menu/mus_menu_intro.ogg", Music.class);
am.load("mus/menu/mus_menu_loop.ogg", Music.class);
diff --git a/core/src/com/ilotterytea/maxoning/utils/formatters/NumberFormatter.java b/core/src/com/ilotterytea/maxoning/utils/formatters/NumberFormatter.java
new file mode 100644
index 0000000..96c0258
--- /dev/null
+++ b/core/src/com/ilotterytea/maxoning/utils/formatters/NumberFormatter.java
@@ -0,0 +1,32 @@
+package com.ilotterytea.maxoning.utils.formatters;
+
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.TreeMap;
+
+public class NumberFormatter {
+ private static final NavigableMap<Long, String> suffixes = new TreeMap<>();
+ static {
+ suffixes.put(1_000L, "k");
+ suffixes.put(1_000_000L, "M");
+ suffixes.put(1_000_000_000L, "G");
+ suffixes.put(1_000_000_000_000L, "T");
+ suffixes.put(1_000_000_000_000_000L, "P");
+ suffixes.put(1_000_000_000_000_000_000L, "E");
+ }
+
+ public static String format(long value) {
+ //Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here
+ if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1);
+ if (value < 0) return "-" + format(-value);
+ if (value < 1000) return Long.toString(value); //deal with easy case
+
+ Map.Entry<Long, String> e = suffixes.floorEntry(value);
+ Long divideBy = e.getKey();
+ String suffix = e.getValue();
+
+ long truncated = value / (divideBy / 10); //the number part of the output times 10
+ boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10);
+ return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
+ }
+}
diff --git a/core/src/com/ilotterytea/maxoning/utils/math/Math.java b/core/src/com/ilotterytea/maxoning/utils/math/Math.java
new file mode 100644
index 0000000..0bb8aa8
--- /dev/null
+++ b/core/src/com/ilotterytea/maxoning/utils/math/Math.java
@@ -0,0 +1,13 @@
+package com.ilotterytea.maxoning.utils.math;
+
+public class Math {
+ /**
+ * Get random number from min value to max value
+ * @param min Minimal value
+ * @param max Maximum value
+ * @return Random number between minimal and maximum values
+ */
+ public static int getRandomNumber(int min, int max) {
+ return (int) ((java.lang.Math.random() * (max - min)) + min);
+ }
+}
diff --git a/core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java b/core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java
index a10d98a..3e64c45 100644
--- a/core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java
+++ b/core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java
@@ -1,35 +1,99 @@
package com.ilotterytea.maxoning.utils.serialization;
+import com.badlogic.gdx.utils.Null;
+import com.google.gson.Gson;
import com.ilotterytea.maxoning.MaxonConstants;
-import com.ilotterytea.maxoning.player.MaxonPlayer;
+import com.ilotterytea.maxoning.player.MaxonSavegame;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.*;
+import java.util.ArrayList;
+/**
+ * External game data system control.
+ * @author NotDankEnough
+ * @since Alpha 1.0
+ */
public class GameDataSystem {
private static final File dir = new File(MaxonConstants.GAME_SAVEGAME_FOLDER);
- private static final File file = new File(dir.getPath() + "/savegame.sav");
+ private static final Gson gson = new Gson();
+ private static final Logger log = LoggerFactory.getLogger(GameDataSystem.class.getSimpleName());
- public static boolean exists() { return file.exists(); }
+ /**
+ * Get all savefiles from savegame directory (/.Maxoning/savegames/)
+ * @return Array of MaxonSavegames
+ * @see MaxonSavegame
+ */
+ public static ArrayList<MaxonSavegame> getSavegames() {
+ ArrayList<MaxonSavegame> saves = new ArrayList<>();
+ File[] files = dir.listFiles();
- public static void SaveData(MaxonPlayer player) throws IOException {
- if (!dir.exists()) {
- dir.mkdirs();
+ assert files != null;
+ for (File file : files) {
+ try {
+
+ FileInputStream fis = new FileInputStream(file);
+ ObjectInputStream ois = new ObjectInputStream(fis);
+
+ MaxonSavegame sav = gson.fromJson(ois.readUTF(), MaxonSavegame.class);
+ saves.add(sav);
+
+ ois.close();
+ fis.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
- FileOutputStream fo = new FileOutputStream(file);
- ObjectOutputStream out = new ObjectOutputStream(fo);
- out.writeObject(player);
- out.close();
+ return saves;
+ }
+
+ /**
+ * Convert <b>MaxonSavegame</b> class to <b>JSON</b> string and write in UTF-8 encoding (I'm sorry, encryption enjoyers).
+ * @param savegame Save game object.
+ * @param file_name File name.
+ * @see MaxonSavegame
+ */
+ public static void save(@NotNull MaxonSavegame savegame, @NotNull String file_name) {
+ try {
+ log.info("Saving the game...");
+ FileOutputStream fos = new FileOutputStream(String.format("%s/%s", dir.getAbsolutePath(), file_name));
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+
+ oos.writeUTF(gson.toJson(savegame));
+ oos.close();
+ log.info(String.format("Success! Savegame located at %s/%s", dir.getAbsolutePath(), file_name));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
- public static MaxonPlayer LoadData() throws IOException, ClassNotFoundException {
- FileInputStream fi = new FileInputStream(file);
- ObjectInputStream oi = new ObjectInputStream(fi);
+ /**
+ * Reading a <b>JSON</b> string from the file and convert to <b>MaxonSavegame</b> class.
+ * @param file_name File name. If null - it will get the first file by last modified time.
+ * @return Filled <b>MaxonSavegame</b> class
+ * @see MaxonSavegame
+ */
+ public static MaxonSavegame load(@Null String file_name) {
+ MaxonSavegame sav = new MaxonSavegame();
+
+ if (new File(dir.getAbsolutePath() + "/" + file_name).exists()) {
+ try {
+ log.info(String.format("Trying to get the savegame at %s/%s...", dir.getAbsolutePath(), file_name));
+ FileInputStream fis = new FileInputStream(String.format("%s/%s", dir.getAbsolutePath(), file_name));
+ ObjectInputStream oos = new ObjectInputStream(fis);
- MaxonPlayer pl = (MaxonPlayer) oi.readObject();
+ sav = gson.fromJson(oos.readUTF(), MaxonSavegame.class);
+ oos.close();
+
+ log.info(String.format("Successfully loaded the savegame from %s/%s!", dir.getAbsolutePath(), file_name));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
- oi.close();
- fi.close();
- return pl;
+ return sav;
}
}