Add tier management system with JSON loading; refactor PopulationVisualizer for category handling

This commit is contained in:
2025-08-23 05:23:09 +02:00
parent bf272b4c2f
commit 4a3bc46081
8 changed files with 91 additions and 15 deletions

View File

@@ -9,10 +9,13 @@ namespace ParasiticGod.Scripts.Components;
[GlobalClass]
public partial class PopulationVisualizer : Node
{
public enum VisualCategory { Followers, Huts }
[Export] private Node2D _markersContainer;
[Export] private int _unitsPerMarker = 5;
[Export] private Array<TierDefinition> _tiers;
[Export] public VisualCategory Category { get; private set; }
private List<TierDefinition> _tiers;
private readonly List<FollowerMarker> _markers = [];
private long _lastKnownUnitCount = -1;
private int _lastKnownTierIndex = -1;
@@ -20,6 +23,19 @@ public partial class PopulationVisualizer : Node
public override void _Ready()
{
switch (Category)
{
case VisualCategory.Followers:
_tiers = GameBus.Instance.FollowerTiers;
break;
case VisualCategory.Huts:
_tiers = GameBus.Instance.HutTiers;
break;
default:
GD.PushError($"PopulationVisualizer has an invalid category: {Category}");
return;
}
foreach (var child in _markersContainer.GetChildren())
{
if (child is FollowerMarker marker)
@@ -40,7 +56,12 @@ public partial class PopulationVisualizer : Node
{
if (_isUpdating) return;
var currentUnitCount = (long)newState.Get(Stat.Followers);
long currentUnitCount = Category switch
{
VisualCategory.Followers => (long)newState.Get(Stat.Followers),
VisualCategory.Huts => (long)newState.Get(Stat.Followers),
_ => 0
};
var currentMarkersToShow = (int)currentUnitCount / _unitsPerMarker;
var lastMarkersToShow = (int)_lastKnownUnitCount / _unitsPerMarker;

View File

@@ -5,6 +5,7 @@ namespace ParasiticGod.Scripts.Core;
[GlobalClass]
public partial class TierDefinition : Resource
{
[Export] public PackedScene Scene { get; private set; }
[Export] public long Threshold { get; private set; }
[Export] public PackedScene Scene { get; set; }
[Export] public long Threshold { get; set; }
[Export] public Follower.FollowerTier TierEnum { get; set; }
}

15
Scripts/Core/TierDto.cs Normal file
View File

@@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace ParasiticGod.Scripts.Core;
public class TierDto
{
public Follower.FollowerTier TierEnum { get; set; }
public long Threshold { get; set; }
public string ScenePath { get; set; }
}
public class TierListDto
{
public List<TierDto> Tiers { get; set; }
}

View File

@@ -0,0 +1 @@
uid://4432c2r3q5vv

View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Linq;
using Godot;
using Newtonsoft.Json;
namespace ParasiticGod.Scripts.Core;
public static class TierLoader
{
public static List<TierDefinition> LoadTiersFromFile(string filePath)
{
var loadedTiers = new List<TierDefinition>();
var fileContent = FileAccess.GetFileAsString(filePath);
if (string.IsNullOrEmpty(fileContent))
{
GD.PushError($"Failed to read tier file or file is empty: {filePath}");
return loadedTiers;
}
var tierListDto = JsonConvert.DeserializeObject<TierListDto>(fileContent);
if (tierListDto?.Tiers == null)
{
GD.PushError($"Failed to deserialize tier list JSON or 'Tiers' array is missing: {filePath}");
return loadedTiers;
}
foreach (var dto in tierListDto.Tiers)
{
var tierDef = new TierDefinition
{
Threshold = dto.Threshold,
TierEnum = dto.TierEnum,
Scene = GD.Load<PackedScene>(dto.ScenePath)
};
loadedTiers.Add(tierDef);
}
GD.Print($"Loaded {loadedTiers.Count} follower tiers from {filePath}");
return loadedTiers.OrderBy(t => t.Threshold).ToList();
}
}

View File

@@ -0,0 +1 @@
uid://hgx8qu7etqhm

View File

@@ -10,6 +10,8 @@ public partial class GameBus : Node
{
public static GameBus Instance { get; private set; }
public Dictionary<string, MiracleDefinition> AllMiracles { get; private set; }
public List<TierDefinition> FollowerTiers { get; private set; }
public List<TierDefinition> HutTiers { get; private set; }
private readonly GameState _gameState = new();
private readonly GameLogic _gameLogic = new();
@@ -27,6 +29,8 @@ public partial class GameBus : Node
{
Instance = this;
AllMiracles = MiracleLoader.LoadMiraclesFromDirectory("user://Mods/Miracles");
FollowerTiers = TierLoader.LoadTiersFromFile("user://Mods/Tiers/follower_tiers.json");
HutTiers = TierLoader.LoadTiersFromFile("user://Mods/Tiers/hut_tiers.json");
}
public override void _ExitTree()