Compare commits

..

2 Commits

6 changed files with 203 additions and 4 deletions

View File

@@ -106,8 +106,8 @@ public class EventHandler {
panel.gameState = GameState.DIALOGUE;
panel.player.attackCancel = true;
panel.playSE(2);
panel.ui.currentDialogue = "You drank the holy water.\nYour life has been recovered!\nYou progress has been saved.";
panel.player.life = panel.player.maxLife;
panel.ui.currentDialogue = "You saved your progress!";
//panel.player.life = panel.player.maxLife;
canTouchEvent = false;
panel.assetSetter.setMonster();
try {

View File

@@ -39,6 +39,7 @@ public class KeyHandler implements KeyListener {
System.out.printf("Loading config...%n");
try {
panel.config.load();
panel.saveLoad.load();
panel.gameState = GameState.PLAY;
panel.playMusic(0);
} catch (Exception e) {

View File

@@ -0,0 +1,88 @@
package de.miaurizius.jgame2d.data;
import de.miaurizius.jgame2d.core.GamePanel;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Config {
private final GamePanel panel;
private final HashMap<String, String> settings = new HashMap<>();
public Config(GamePanel panel) throws IOException {
this.panel = panel;
for (Setting option : Setting.values()) settings.put(option.name, null);
new File("gamedata").mkdirs();
new File("gamedata/save").mkdirs();
new File("gamedata/config").createNewFile();
}
// GENERAL
public final void save() {
try {
insertToHash();
BufferedWriter writer = new BufferedWriter(new FileWriter("gamedata/config"));
settings.forEach((invoke, value) -> {
try {
writer.write(invoke + ": " + value);
writer.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public final void load() {
try (BufferedReader reader = new BufferedReader(new FileReader("gamedata/config"))) {
List<String> lines = reader.lines().toList();
if (lines.isEmpty()) return;
Map<String, String> configMap = new HashMap<>();
for (String line : lines) {
String[] parts = line.split(":\\s*", 2);
if (parts.length == 2) {
configMap.put(parts[0].trim(), parts[1].trim());
}
}
for (Setting setting : Setting.values()) {
String value = configMap.get(setting.name);
if (value != null) {
switch (setting) {
case FULLSCREEN -> panel.fullscreen = Boolean.parseBoolean(value);
case MUSICVOLUME -> panel.music.volumeScale = Integer.parseInt(value);
case SFXVOLUME -> panel.sfx.volumeScale = Integer.parseInt(value);
}
}
}
insertToHash();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// HELP FUNCTIONS
private void insertToHash() {
settings.put(Setting.FULLSCREEN.name, String.valueOf(panel.fullscreen));
settings.put(Setting.MUSICVOLUME.name, String.valueOf(panel.music.volumeScale));
settings.put(Setting.SFXVOLUME.name, String.valueOf(panel.sfx.volumeScale));
}
private enum Setting {
FULLSCREEN("fullscreen"),
MUSICVOLUME("music-vol"),
SFXVOLUME("sfx-vol");
private final String name;
Setting(String name) {
this.name = name;
}
}
}

View File

@@ -0,0 +1,28 @@
package de.miaurizius.jgame2d.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class DataStorage implements Serializable {
// PLAYER STATS
int level;
int maxLife;
int life;
int maxMana;
int strength;
int dexterity;
int attack;
int defense;
int exp;
int nextLevelExp;
int coins;
int worldX, worldY;
// PLAYER INVENTORY
List<String> itemNames = new ArrayList<>();
List<Integer> itemAmounts = new ArrayList<>();
}

View File

@@ -0,0 +1,82 @@
package de.miaurizius.jgame2d.data;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.Entity;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
public class SaveLoad {
GamePanel panel;
public SaveLoad(GamePanel panel) {
this.panel = panel;
}
public Entity getObject(String itemName) {
Entity obj;
try {
obj = (Entity) Class.forName(itemName).getDeclaredConstructor().newInstance(panel);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException(e);
}
return obj;
}
public void save() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("gamedata/save/savegame.dat"));
DataStorage ds = new DataStorage();
// PLAYER STATS
ds.level = panel.player.level;
ds.maxLife = panel.player.maxLife;
ds.life = panel.player.life;
ds.maxMana = panel.player.maxMana;
ds.strength = panel.player.strength;
ds.dexterity = panel.player.dexterity;
ds.attack = panel.player.attack;
ds.defense = panel.player.defense;
ds.exp = panel.player.exp;
ds.nextLevelExp = panel.player.nextLevelExp;
ds.coins = panel.player.coins;
ds.worldX = panel.player.worldX;
ds.worldY = panel.player.worldY;
// PLAYER INVENTORY
for(int i = 0; i < panel.player.inventory.size(); i++) {
ds.itemNames.add(panel.player.inventory.get(i).getClass().getName());
ds.itemAmounts.add(panel.player.inventory.get(i).amt);
}
// WRITE
oos.writeObject(ds);
}
public void load() throws ClassNotFoundException, IOException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("gamedata/save/savegame.dat"));
DataStorage ds = (DataStorage) ois.readObject();
panel.player.level = ds.level;
panel.player.maxLife = ds.maxLife;
panel.player.life = ds.life;
panel.player.maxMana = ds.maxMana;
panel.player.strength = ds.strength;
panel.player.dexterity = ds.dexterity;
panel.player.attack = ds.attack;
panel.player.defense = ds.defense;
panel.player.exp = ds.exp;
panel.player.nextLevelExp = ds.nextLevelExp;
panel.player.coins = ds.coins;
panel.player.worldX = ds.worldX;
panel.player.worldY = ds.worldY;
for(int i = 0; i < ds.itemNames.size(); i++) {
Entity obj = getObject(ds.itemNames.get(i));
obj.amt = ds.itemAmounts.get(i);
panel.player.inventory.add(obj);
}
}
}

View File

@@ -14,7 +14,7 @@ import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
@@ -80,7 +80,7 @@ public class Entity {
public Entity currentShield;
public Entity currentLight;
public Projectile projectile;
public ArrayList<Entity> inventory = new ArrayList<>();
public List<Entity> inventory = new ArrayList<>();
public final int maxInvSize = 20;
// ITEM ATTRIBUTES