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

@@ -1,4 +1,4 @@
[gd_scene load_steps=28 format=3 uid="uid://bfil8sd154327"]
[gd_scene load_steps=20 format=3 uid="uid://bfil8sd154327"]
[ext_resource type="Script" uid="uid://t71ewkpa5uqs" path="res://Scenes/Main/Main.cs" id="1_p8rbg"]
[ext_resource type="Script" uid="uid://b77vh831r1e3c" path="res://Scenes/Main/MiraclePanel.cs" id="2_hcu3t"]
@@ -14,16 +14,8 @@
[ext_resource type="AudioStream" uid="uid://dv0i7xw8o5ac0" path="res://Sfx/UI_SFX_Pack_61_5.wav" id="8_4etfk"]
[ext_resource type="Script" uid="uid://dj2wyrq07gfp2" path="res://Scripts/Components/PopulationVisualizer.cs" id="8_cv8e0"]
[ext_resource type="PackedScene" uid="uid://8w7tvsgkev1y" path="res://Scenes/tree.tscn" id="8_hcu3t"]
[ext_resource type="Resource" uid="uid://8ooxfo2wdbhu" path="res://Resources/Tiers/Followers/follower_tier_1.tres" id="9_hkvnm"]
[ext_resource type="Shader" uid="uid://bf8nk145fjkgh" path="res://Shaders/corruption_shader.gdshader" id="9_wgovn"]
[ext_resource type="Resource" uid="uid://cejeb3467iiyl" path="res://Resources/Tiers/Followers/follower_tier_2.tres" id="10_5ci8a"]
[ext_resource type="PackedScene" uid="uid://cqkye7yykakns" path="res://Scenes/Followers/FollowerMarker.tscn" id="11_5ci8a"]
[ext_resource type="Resource" uid="uid://q0rha23lx4wl" path="res://Resources/Tiers/Followers/follower_tier_3.tres" id="11_18xdc"]
[ext_resource type="Resource" uid="uid://i1oo0q84q8ps" path="res://Resources/Tiers/Followers/follower_tier_4.tres" id="12_epx8f"]
[ext_resource type="Resource" uid="uid://bwu8k7cyjhf8c" path="res://Resources/Tiers/Followers/follower_tier_5.tres" id="13_hcu3t"]
[ext_resource type="Resource" uid="uid://bbkbssvptkyvh" path="res://Resources/Tiers/Huts/hut_tier_1.tres" id="14_18xdc"]
[ext_resource type="Resource" uid="uid://co2sdpwpajjqi" path="res://Resources/Tiers/Huts/hut_tier_2.tres" id="15_epx8f"]
[ext_resource type="Resource" uid="uid://b8k30qsd434dp" path="res://Resources/Tiers/Huts/hut_tier_3.tres" id="16_hcu3t"]
[ext_resource type="Script" uid="uid://furbvcmw31bx" path="res://Scripts/Components/ForestVisualizer.cs" id="18_qdkat"]
[ext_resource type="Script" uid="uid://cw8gpeaq3yfjn" path="res://Scripts/Components/RoadManager.cs" id="19_qdkat"]
@@ -5557,14 +5549,13 @@ position = Vector2(243.12, -124.88)
script = ExtResource("8_cv8e0")
_markersContainer = NodePath("../Followers Markers")
_unitsPerMarker = 1
_tiers = Array[Object]([ExtResource("9_hkvnm"), ExtResource("10_5ci8a"), ExtResource("11_18xdc"), ExtResource("12_epx8f"), ExtResource("13_hcu3t")])
metadata/_custom_type_script = "uid://dj2wyrq07gfp2"
[node name="HutPopulationVisualizer" type="Node" parent="." node_paths=PackedStringArray("_markersContainer")]
script = ExtResource("8_cv8e0")
_markersContainer = NodePath("../Hut Markers")
_unitsPerMarker = 1
_tiers = Array[Object]([ExtResource("14_18xdc"), ExtResource("15_epx8f"), ExtResource("16_hcu3t")])
Category = 1
metadata/_custom_type_script = "uid://dj2wyrq07gfp2"
[node name="ForestVisualizer" type="Node" parent="." node_paths=PackedStringArray("_treesContainer")]

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()