basic Party Manager
This commit is contained in:
parent
c141e6a169
commit
6f1d5ea681
10 changed files with 158 additions and 8 deletions
65
scripts/core/CreatureRegistry.cs
Normal file
65
scripts/core/CreatureRegistry.cs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
|
||||
public partial class CreatureRegistry : Node
|
||||
{
|
||||
public record CreatureDef(
|
||||
string id,
|
||||
string name,
|
||||
List<string> types,
|
||||
List<string> biomes,
|
||||
List<string> time_windows,
|
||||
int base_hp,
|
||||
int base_atk,
|
||||
int base_def,
|
||||
int capture_difficulty
|
||||
);
|
||||
|
||||
public record CreatureFile(int schema, List<CreatureDef> creatures);
|
||||
|
||||
private readonly Dictionary<string, CreatureDef> _byId = new();
|
||||
|
||||
public CreatureDef Get(string id)
|
||||
{
|
||||
if (_byId.TryGetValue(id, out var def)) return def;
|
||||
throw new KeyNotFoundException($"Unknown creature id: {id}");
|
||||
}
|
||||
|
||||
public bool TryGet(string id, out CreatureDef def) => _byId.TryGetValue(id, out def!);
|
||||
|
||||
[Export] public NodePath PackManagerPath { get; set; }
|
||||
private PackManager? _packs;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_packs = GetNodeOrNull<PackManager>(PackManagerPath);
|
||||
if (_packs == null)
|
||||
{
|
||||
GD.PrintErr("CreatureRegistry: PackManagerPath not set.");
|
||||
return;
|
||||
}
|
||||
|
||||
var path = _packs.Resolve("data/creatures/creatures.json");
|
||||
if (path == null)
|
||||
{
|
||||
GD.PrintErr("CreatureRegistry: Could not resolve data/creatures/creatures.json");
|
||||
return;
|
||||
}
|
||||
|
||||
var json = FileAccess.GetFileAsString(path);
|
||||
var file = JsonSerializer.Deserialize<CreatureFile>(json);
|
||||
|
||||
if (file?.creatures == null)
|
||||
{
|
||||
GD.PrintErr("CreatureRegistry: Failed to parse creatures.json");
|
||||
return;
|
||||
}
|
||||
|
||||
_byId.Clear();
|
||||
foreach (var c in file.creatures)
|
||||
_byId[c.id] = c;
|
||||
|
||||
GD.Print($"CreatureRegistry: loaded {_byId.Count} creatures");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue