Kampfsystem: Hier Position des Zweihänders und neues feature zum editieren des offsets

This commit is contained in:
2026-10-15 14:04:34 +02:00
parent 6bb195da90
commit d19538bb63
12 changed files with 874 additions and 102 deletions

View File

@@ -0,0 +1,99 @@
package de.blight.common.model;
import com.google.gson.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
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.
*/
public final class WeaponAttachmentIO {
private static final Logger log = LoggerFactory.getLogger(WeaponAttachmentIO.class);
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final String CLASSPATH_FILE = "weapon_attachment.json";
private static final Map<String, WeaponCategoryAttachment> CONFIGS = new HashMap<>();
private static boolean loaded = false;
private WeaponAttachmentIO() {}
/** Gibt die Attachment-Konfiguration für die gegebene Kategorie zurück (lazy-load vom Classpath). */
public static WeaponCategoryAttachment get(ItemSubCategory cat) {
ensureLoaded();
if (cat == null) return new WeaponCategoryAttachment();
return CONFIGS.getOrDefault(cat.name(), new WeaponCategoryAttachment());
}
public static void set(ItemSubCategory cat, WeaponCategoryAttachment cfg) {
if (cat != null) CONFIGS.put(cat.name(), cfg);
}
/** Lädt weapon_attachment.json vom Classpath (für Runtime im Spiel). */
public static synchronized void loadFromClasspath() {
try (InputStream is = WeaponAttachmentIO.class.getClassLoader()
.getResourceAsStream(CLASSPATH_FILE)) {
if (is == null) {
log.warn("[WeaponAttachmentIO] {} nicht auf Classpath gefunden – Defaults verwendet",
CLASSPATH_FILE);
loaded = true;
return;
}
parse(new String(is.readAllBytes(), StandardCharsets.UTF_8));
loaded = true;
} catch (IOException e) {
log.warn("[WeaponAttachmentIO] Ladefehler: {}", e.getMessage());
loaded = true;
}
}
/** Lädt weapon_attachment.json vom Dateisystem (für Editor). */
public static void load(Path file) {
if (!Files.exists(file)) return;
try {
parse(Files.readString(file, StandardCharsets.UTF_8));
loaded = true;
} catch (IOException e) {
log.warn("[WeaponAttachmentIO] Ladefehler: {}", e.getMessage());
}
}
/** Speichert die aktuelle Konfiguration in eine JSON-Datei (nur im Editor). */
public static void save(Path file) {
try {
Files.writeString(file, GSON.toJson(CONFIGS), StandardCharsets.UTF_8);
log.info("[WeaponAttachmentIO] Gespeichert: {}", file);
} catch (IOException e) {
log.warn("[WeaponAttachmentIO] Speicherfehler: {}", e.getMessage());
}
}
/** Gibt eine Kopie der aktuellen Config-Map zurück (für Editor-Anzeige). */
public static Map<String, WeaponCategoryAttachment> getAll() {
ensureLoaded();
return new HashMap<>(CONFIGS);
}
private static synchronized void ensureLoaded() {
if (!loaded) loadFromClasspath();
}
private static void parse(String json) {
CONFIGS.clear();
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
for (var entry : obj.entrySet()) {
CONFIGS.put(entry.getKey(),
GSON.fromJson(entry.getValue(), WeaponCategoryAttachment.class));
}
log.info("[WeaponAttachmentIO] {} Kategorien geladen", CONFIGS.size());
}
}

View File

@@ -0,0 +1,18 @@
package de.blight.common.model;
import lombok.Getter;
import lombok.Setter;
/** Attachment-Parameter für eine Waffen-Kategorie (ersetzt den hardcodierten HAND_GRIP_OFFSET). */
@Getter
@Setter
public class WeaponCategoryAttachment {
/** Rechte Hand: Offset in Waffen-Local-Space. Standardwert = bisheriger HAND_GRIP_OFFSET. */
private float handOffX = ItemGrip.HAND_GRIP_OFFSET_X;
private float handOffY = ItemGrip.HAND_GRIP_OFFSET_Y;
private float handOffZ = ItemGrip.HAND_GRIP_OFFSET_Z;
/** Linke Hand (Zweihänder): Korrektur auf g2 in Waffen-Local-Space. */
private float hand2OffX = 0f;
private float hand2OffY = 0f;
private float hand2OffZ = 0f;
}