Compare commits
3 Commits
39fc7d549a
...
befb117d85
| Author | SHA1 | Date | |
|---|---|---|---|
|
befb117d85
|
|||
|
54a2ee3022
|
|||
|
ce3f590715
|
@@ -1,85 +0,0 @@
|
||||
package de.miaurizius.jgame2d.core;
|
||||
|
||||
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/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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import de.miaurizius.jgame2d.aStar.PathFinder;
|
||||
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||
import de.miaurizius.jgame2d.core.enums.Map;
|
||||
import de.miaurizius.jgame2d.core.handlers.*;
|
||||
import de.miaurizius.jgame2d.data.Config;
|
||||
import de.miaurizius.jgame2d.data.SaveLoad;
|
||||
import de.miaurizius.jgame2d.entity.Entity;
|
||||
import de.miaurizius.jgame2d.entity.Player;
|
||||
import de.miaurizius.jgame2d.environment.EnvironmentManager;
|
||||
@@ -61,6 +63,7 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
public PathFinder pFinder = new PathFinder(this);
|
||||
public EnvironmentManager eManager = new EnvironmentManager(this);
|
||||
public MiniMap mapMan = new MiniMap(this);
|
||||
public SaveLoad saveLoad = new SaveLoad(this);
|
||||
Thread gameThread;
|
||||
|
||||
// ENTITY AND OBJECT
|
||||
@@ -263,18 +266,16 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
fScreenWidth = Boot.window.getWidth();
|
||||
fScreenHeight = Boot.window.getHeight();
|
||||
}
|
||||
public void retry() {
|
||||
public void resetGame(boolean restart) {
|
||||
player.setDefaultPositions();
|
||||
player.restoreLife();
|
||||
player.restoreStatus();
|
||||
assetSetter.setNPC();
|
||||
assetSetter.setMonster();
|
||||
}
|
||||
public void restart() {
|
||||
|
||||
if(!restart) return;
|
||||
player.setDefaultValues();
|
||||
assetSetter.setObject();
|
||||
assetSetter.setNPC();
|
||||
assetSetter.setMonster();
|
||||
assetSetter.setITiles();
|
||||
eManager.lighting.resetDay();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -736,6 +736,7 @@ public class UI {
|
||||
if(commandNum == 1) {
|
||||
graphics2d.drawString(">", textX-25, textY);
|
||||
if(panel.keyH.spacePressed) optionState = OptionState.OVERVIEW;
|
||||
panel.resetGame(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.miaurizius.jgame2d.core.handlers;
|
||||
|
||||
import de.miaurizius.jgame2d.core.Boot;
|
||||
import de.miaurizius.jgame2d.core.GamePanel;
|
||||
import de.miaurizius.jgame2d.core.enums.Direction;
|
||||
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||
@@ -7,6 +8,7 @@ import de.miaurizius.jgame2d.core.enums.Map;
|
||||
import de.miaurizius.jgame2d.entity.Entity;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class EventHandler {
|
||||
|
||||
@@ -104,10 +106,15 @@ 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!\nBut all monsters respawned... \nGood luck!";
|
||||
panel.ui.currentDialogue = "You drank the holy water.\nYour life has been recovered!\nYou progress has been saved.";
|
||||
panel.player.life = panel.player.maxLife;
|
||||
canTouchEvent = false;
|
||||
panel.assetSetter.setMonster();
|
||||
try {
|
||||
panel.saveLoad.save();
|
||||
} catch (Exception e) {
|
||||
Boot.logger.log(Level.SEVERE, "Saving game failed!", e);
|
||||
}
|
||||
}
|
||||
private void changeMap(Map map, int col, int row) {
|
||||
panel.gameState = GameState.TRANSITION;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.miaurizius.jgame2d.core.handlers;
|
||||
|
||||
import de.miaurizius.jgame2d.core.Boot;
|
||||
import de.miaurizius.jgame2d.core.GamePanel;
|
||||
import de.miaurizius.jgame2d.core.UI;
|
||||
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||
@@ -7,6 +8,7 @@ import de.miaurizius.jgame2d.core.enums.Map;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class KeyHandler implements KeyListener {
|
||||
|
||||
@@ -34,7 +36,14 @@ public class KeyHandler implements KeyListener {
|
||||
panel.playMusic(0);
|
||||
break;
|
||||
case 1:
|
||||
// add later
|
||||
System.out.printf("Loading config...%n");
|
||||
try {
|
||||
panel.config.load();
|
||||
panel.gameState = GameState.PLAY;
|
||||
panel.playMusic(0);
|
||||
} catch (Exception e) {
|
||||
Boot.logger.log(Level.SEVERE, "Failed to load config", e);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
System.exit(0);
|
||||
@@ -152,12 +161,12 @@ public class KeyHandler implements KeyListener {
|
||||
if(panel.ui.commandNum == 0) {
|
||||
panel.gameState = GameState.PLAY;
|
||||
panel.playMusic(0);
|
||||
panel.retry();
|
||||
panel.resetGame(false);
|
||||
}
|
||||
if(panel.ui.commandNum == 1) {
|
||||
panel.gameState = GameState.TITLE;
|
||||
panel.ui.commandNum = 0;
|
||||
panel.restart();
|
||||
panel.resetGame(true);
|
||||
panel.stopMusic();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,10 +327,10 @@ public class Entity {
|
||||
int nextWorldY = user.getTopY();
|
||||
|
||||
switch(user.direction) {
|
||||
case UP -> nextWorldY = user.getTopY()-1;
|
||||
case DOWN -> nextWorldY = user.getBottomY()+1;
|
||||
case LEFT -> nextWorldX = user.getLeftX()-1;
|
||||
case RIGHT -> nextWorldX = user.getRightX()+1;
|
||||
case UP -> nextWorldY = user.getTopY()-panel.player.speed;
|
||||
case DOWN -> nextWorldY = user.getBottomY()+panel.player.speed;
|
||||
case LEFT -> nextWorldX = user.getLeftX()-panel.player.speed;
|
||||
case RIGHT -> nextWorldX = user.getRightX()+panel.player.speed;
|
||||
}
|
||||
int col = nextWorldX / panel.tileSize;
|
||||
int row = nextWorldY / panel.tileSize;
|
||||
|
||||
@@ -311,9 +311,14 @@ public class Player extends Entity {
|
||||
worldY = panel.tileSize * 21;
|
||||
direction = Direction.DOWN;
|
||||
}
|
||||
public void restoreLife() {
|
||||
public void restoreStatus() {
|
||||
life = maxLife;
|
||||
invincible = false;
|
||||
transparent = false;
|
||||
attacking = false;
|
||||
guarding = false;
|
||||
knockback = false;
|
||||
lightUpdated = true;
|
||||
}
|
||||
public void getSleepingImage(BufferedImage image) {
|
||||
down1 = image;
|
||||
@@ -346,6 +351,7 @@ public class Player extends Entity {
|
||||
coins = 500;
|
||||
currentWeapon = new SwordNormalObj(panel);
|
||||
currentShield = new ShieldWoodObj(panel);
|
||||
currentLight = null;
|
||||
projectile = new FireballObj(panel);
|
||||
attack = getAttack();
|
||||
defense = getDefense();
|
||||
|
||||
@@ -75,6 +75,11 @@ public class Lighting {
|
||||
}
|
||||
}
|
||||
}
|
||||
public void resetDay() {
|
||||
dayState = DayState.DAY;
|
||||
filterAlpha = 0f;
|
||||
dayCount = 0;
|
||||
}
|
||||
|
||||
// ...
|
||||
public void setLightSource() {
|
||||
|
||||
Reference in New Issue
Block a user