Add RoadManager to manage road generation and update population visuals

This commit is contained in:
2025-08-23 03:45:02 +02:00
parent d198aed01f
commit 32c75c6fe8
10 changed files with 124 additions and 4 deletions

View File

@@ -6,5 +6,5 @@
[resource]
script = ExtResource("1_ei6wr")
Scene = ExtResource("1_uho8r")
Threshold = 40000
Threshold = 100
metadata/_custom_type_script = "uid://c7hh0cy0yrdt8"

View File

@@ -6,5 +6,5 @@
[resource]
script = ExtResource("2_vr1nw")
Scene = ExtResource("1_trlm4")
Threshold = 600000
Threshold = 400
metadata/_custom_type_script = "uid://c7hh0cy0yrdt8"

View File

@@ -6,5 +6,5 @@
[resource]
script = ExtResource("2_74x2g")
Scene = ExtResource("1_ex2nw")
Threshold = 1200000
Threshold = 1000
metadata/_custom_type_script = "uid://c7hh0cy0yrdt8"

View File

@@ -5,6 +5,7 @@
[node name="HutTier2" type="Node2D"]
script = ExtResource("1_22ax8")
Tier = 1
metadata/_custom_type_script = "uid://cj5libcgnhjml"
[node name="Hut" type="Sprite2D" parent="."]

View File

@@ -5,6 +5,7 @@
[node name="HutTier3" type="Node2D"]
script = ExtResource("1_ltla2")
Tier = 2
metadata/_custom_type_script = "uid://cj5libcgnhjml"
[node name="Hut" type="Sprite2D" parent="."]

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=20 format=3 uid="uid://bfil8sd154327"]
[gd_scene load_steps=21 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"]
@@ -18,6 +18,7 @@
[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/ForestVisualizer.cs" id="18_qdkat"]
[ext_resource type="Script" uid="uid://cw8gpeaq3yfjn" path="res://Scripts/RoadManager.cs" id="19_qdkat"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_cv8e0"]
shader = ExtResource("9_wgovn")
@@ -5593,3 +5594,8 @@ metadata/_custom_type_script = "uid://dj2wyrq07gfp2"
script = ExtResource("18_qdkat")
_treesContainer = NodePath("../ForestContainer")
metadata/_custom_type_script = "uid://furbvcmw31bx"
[node name="RoadManager" type="Node2D" parent="." node_paths=PackedStringArray("_markersContainer")]
script = ExtResource("19_qdkat")
_markersContainer = NodePath("../Hut Markers")
metadata/_custom_type_script = "uid://cw8gpeaq3yfjn"

View File

@@ -110,5 +110,7 @@ public partial class PopulationVisualizer : Node
_lastKnownUnitCount = currentUnitCount;
_lastKnownTierIndex = newTierIndex;
_isUpdating = false;
GameBus.Instance.NotifyPopulationVisualsUpdated();
}
}

103
Scripts/RoadManager.cs Normal file
View File

@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
using ParasiticGod.Scripts.Core;
using ParasiticGod.Scripts.Singletons;
namespace ParasiticGod.Scripts;
[GlobalClass]
public partial class RoadManager : Node2D
{
[Export] private Node2D _markersContainer;
[Export] private float _roadWidth = 4.0f;
[Export] private Color _roadColor = new("saddlebrown");
[Export] private Follower.FollowerTier _minimumTierForRoads = Follower.FollowerTier.Tier2;
private Line2D _roadNetwork;
public override void _Ready()
{
_roadNetwork = new Line2D
{
Width = _roadWidth,
DefaultColor = _roadColor,
};
AddChild(_roadNetwork);
Callable.From(() =>
{
GenerateRoads();
GameBus.Instance.PopulationVisualsUpdated += GenerateRoads;
}).CallDeferred();
}
public override void _ExitTree()
{
if (GameBus.Instance != null)
{
GameBus.Instance.PopulationVisualsUpdated -= GenerateRoads;
}
}
private void GenerateRoads()
{
_roadNetwork.ClearPoints();
var activeMarkers = _markersContainer.GetChildren()
.OfType<FollowerMarker>()
.Where(m => m.IsOccupied && m.FollowerInstance != null &&
m.FollowerInstance.Tier >= _minimumTierForRoads)
.ToList();
if (activeMarkers.Count < 2) return;
var treeNodes = new HashSet<FollowerMarker>();
var remainingNodes = new List<FollowerMarker>(activeMarkers);
var edges = new List<(Vector2, Vector2)>();
var startNode = remainingNodes[0];
treeNodes.Add(startNode);
remainingNodes.RemoveAt(0);
while (remainingNodes.Any())
{
FollowerMarker bestSource = null;
FollowerMarker bestDest = null;
var minDistanceSq = float.MaxValue;
foreach (var source in treeNodes)
{
foreach (var dest in remainingNodes)
{
var distSq = source.GlobalPosition.DistanceSquaredTo(dest.GlobalPosition);
if (distSq < minDistanceSq)
{
minDistanceSq = distSq;
bestSource = source;
bestDest = dest;
}
}
}
if (bestDest != null)
{
treeNodes.Add(bestDest);
remainingNodes.Remove(bestDest);
edges.Add((bestSource.Position, bestDest.Position));
}
else
{
break;
}
}
foreach (var (start, end) in edges)
{
_roadNetwork.AddPoint(start);
_roadNetwork.AddPoint(end);
}
}
}

View File

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

View File

@@ -18,6 +18,7 @@ public partial class GameBus : Node
public event Action<MiracleDefinition> MiraclePerformed;
public event Action<List<MiracleDefinition>> MiraclesUnlocked;
public event Action<MiracleDefinition> MiracleCompleted;
public event Action PopulationVisualsUpdated;
public override void _EnterTree()
{
@@ -74,6 +75,11 @@ public partial class GameBus : Node
}
}
public void NotifyPopulationVisualsUpdated()
{
PopulationVisualsUpdated?.Invoke();
}
public void SubscribeToStat(Stat stat, Action<double> listener) => _gameState.Subscribe(stat, listener);
public void UnsubscribeFromStat(Stat stat, Action<double> listener) => _gameState.Unsubscribe(stat, listener);