Kampfsystem - hier: Anker beim Kurzschwert, sowohl für Helden als auch NPC

This commit is contained in:
2026-10-26 12:09:37 +01:00
parent 238776cfde
commit f189e6d81f
6 changed files with 139 additions and 10 deletions

View File

@@ -9,11 +9,14 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
/**
* Lädt und speichert per-Kategorie Schild-Attachment-Konfiguration (shield_attachment.json).
* Siehe WeaponAttachmentIO: liest vom Dateisystem (Dev-Umgebung) und prüft bei jedem Zugriff
* die mtime der Datei, damit Editor-Änderungen ohne Neustart ankommen.
*/
public final class ShieldAttachmentIO {
@@ -21,8 +24,17 @@ public final class ShieldAttachmentIO {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final String CLASSPATH_FILE = "shield_attachment.json";
private static final String[] ASSET_BASES = {
"blight-assets/src/main/resources",
"../blight-assets/src/main/resources",
"assets",
".",
};
private static final Map<String, ShieldCategoryAttachment> CONFIGS = new HashMap<>();
private static boolean loaded = false;
private static boolean loaded = false;
private static Path cachedFile = null;
private static long cachedMtime = -1;
private ShieldAttachmentIO() {}
@@ -77,9 +89,33 @@ public final class ShieldAttachmentIO {
}
private static synchronized void ensureLoaded() {
Path file = resolveFile();
if (file != null) {
try {
long mtime = Files.getLastModifiedTime(file).toMillis();
if (!file.equals(cachedFile) || mtime != cachedMtime) {
parse(Files.readString(file, StandardCharsets.UTF_8));
cachedFile = file;
cachedMtime = mtime;
}
loaded = true;
return;
} catch (IOException e) {
log.warn("[ShieldAttachmentIO] Ladefehler: {}", e.getMessage());
}
}
if (!loaded) loadFromClasspath();
}
/** Sucht {@code shield_attachment.json} im Dateisystem der Dev-Umgebung (Editor/Gradle-Run). */
private static Path resolveFile() {
for (String base : ASSET_BASES) {
Path p = Paths.get(base, CLASSPATH_FILE);
if (Files.isRegularFile(p)) return p;
}
return null;
}
private static void parse(String json) {
CONFIGS.clear();
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();

View File

@@ -9,12 +9,16 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
/**
* Lädt und speichert per-Kategorie Waffen-Attachment-Konfiguration (weapon_attachment.json).
* Singleton-Map: wird beim ersten Zugriff automatisch vom Classpath geladen.
* Singleton-Map: wird vom Dateisystem geladen (Dev-Umgebung, siehe ASSET_BASES) und bei jedem
* Zugriff gegen die mtime der Datei geprüft, damit Änderungen aus dem Editor ohne Neustart
* ankommen. Nur wenn keine Quelldatei auffindbar ist (z.B. gepackter Build), wird einmalig
* vom Classpath geladen.
*/
public final class WeaponAttachmentIO {
@@ -22,8 +26,19 @@ public final class WeaponAttachmentIO {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final String CLASSPATH_FILE = "weapon_attachment.json";
/** Siehe AnimationLibrary.ASSET_BASES (blight-game) – hier dupliziert, da blight-common
* keine Abhängigkeit auf blight-game haben darf. */
private static final String[] ASSET_BASES = {
"blight-assets/src/main/resources",
"../blight-assets/src/main/resources",
"assets",
".",
};
private static final Map<String, WeaponCategoryAttachment> CONFIGS = new HashMap<>();
private static boolean loaded = false;
private static boolean loaded = false;
private static Path cachedFile = null;
private static long cachedMtime = -1;
private WeaponAttachmentIO() {}
@@ -84,9 +99,33 @@ public final class WeaponAttachmentIO {
}
private static synchronized void ensureLoaded() {
Path file = resolveFile();
if (file != null) {
try {
long mtime = Files.getLastModifiedTime(file).toMillis();
if (!file.equals(cachedFile) || mtime != cachedMtime) {
parse(Files.readString(file, StandardCharsets.UTF_8));
cachedFile = file;
cachedMtime = mtime;
}
loaded = true;
return;
} catch (IOException e) {
log.warn("[WeaponAttachmentIO] Ladefehler: {}", e.getMessage());
}
}
if (!loaded) loadFromClasspath();
}
/** Sucht {@code weapon_attachment.json} im Dateisystem der Dev-Umgebung (Editor/Gradle-Run). */
private static Path resolveFile() {
for (String base : ASSET_BASES) {
Path p = Paths.get(base, CLASSPATH_FILE);
if (Files.isRegularFile(p)) return p;
}
return null;
}
private static void parse(String json) {
CONFIGS.clear();
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();