diff --git a/Resources/Tiers/Huts/hut_tier_1.tres b/Resources/Tiers/Huts/hut_tier_1.tres index 59c042b..a8008e1 100644 --- a/Resources/Tiers/Huts/hut_tier_1.tres +++ b/Resources/Tiers/Huts/hut_tier_1.tres @@ -6,5 +6,5 @@ [resource] script = ExtResource("1_ei6wr") Scene = ExtResource("1_uho8r") -Threshold = 40000 +Threshold = 100 metadata/_custom_type_script = "uid://c7hh0cy0yrdt8" diff --git a/Resources/Tiers/Huts/hut_tier_2.tres b/Resources/Tiers/Huts/hut_tier_2.tres index faadb81..5b39f99 100644 --- a/Resources/Tiers/Huts/hut_tier_2.tres +++ b/Resources/Tiers/Huts/hut_tier_2.tres @@ -6,5 +6,5 @@ [resource] script = ExtResource("2_vr1nw") Scene = ExtResource("1_trlm4") -Threshold = 600000 +Threshold = 400 metadata/_custom_type_script = "uid://c7hh0cy0yrdt8" diff --git a/Resources/Tiers/Huts/hut_tier_3.tres b/Resources/Tiers/Huts/hut_tier_3.tres index 3534c61..000d110 100644 --- a/Resources/Tiers/Huts/hut_tier_3.tres +++ b/Resources/Tiers/Huts/hut_tier_3.tres @@ -6,5 +6,5 @@ [resource] script = ExtResource("2_74x2g") Scene = ExtResource("1_ex2nw") -Threshold = 1200000 +Threshold = 1000 metadata/_custom_type_script = "uid://c7hh0cy0yrdt8" diff --git a/Scenes/Huts/hut_tier_2.tscn b/Scenes/Huts/hut_tier_2.tscn index 05af487..2f525dc 100644 --- a/Scenes/Huts/hut_tier_2.tscn +++ b/Scenes/Huts/hut_tier_2.tscn @@ -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="."] diff --git a/Scenes/Huts/hut_tier_3.tscn b/Scenes/Huts/hut_tier_3.tscn index 7ef5509..a572975 100644 --- a/Scenes/Huts/hut_tier_3.tscn +++ b/Scenes/Huts/hut_tier_3.tscn @@ -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="."] diff --git a/Scenes/Main/Main.tscn b/Scenes/Main/Main.tscn index c6df25d..ed20915 100644 --- a/Scenes/Main/Main.tscn +++ b/Scenes/Main/Main.tscn @@ -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" diff --git a/Scripts/PopulationVisualizer.cs b/Scripts/PopulationVisualizer.cs index b8aa805..aebc7e3 100644 --- a/Scripts/PopulationVisualizer.cs +++ b/Scripts/PopulationVisualizer.cs @@ -110,5 +110,7 @@ public partial class PopulationVisualizer : Node _lastKnownUnitCount = currentUnitCount; _lastKnownTierIndex = newTierIndex; _isUpdating = false; + + GameBus.Instance.NotifyPopulationVisualsUpdated(); } } \ No newline at end of file diff --git a/Scripts/RoadManager.cs b/Scripts/RoadManager.cs new file mode 100644 index 0000000..7225cdb --- /dev/null +++ b/Scripts/RoadManager.cs @@ -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() + .Where(m => m.IsOccupied && m.FollowerInstance != null && + m.FollowerInstance.Tier >= _minimumTierForRoads) + .ToList(); + + if (activeMarkers.Count < 2) return; + + var treeNodes = new HashSet(); + var remainingNodes = new List(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); + } + } +} \ No newline at end of file diff --git a/Scripts/RoadManager.cs.uid b/Scripts/RoadManager.cs.uid new file mode 100644 index 0000000..914f80c --- /dev/null +++ b/Scripts/RoadManager.cs.uid @@ -0,0 +1 @@ +uid://cw8gpeaq3yfjn diff --git a/Scripts/Singletons/GameBus.cs b/Scripts/Singletons/GameBus.cs index 48b7809..10bc7ac 100644 --- a/Scripts/Singletons/GameBus.cs +++ b/Scripts/Singletons/GameBus.cs @@ -18,6 +18,7 @@ public partial class GameBus : Node public event Action MiraclePerformed; public event Action> MiraclesUnlocked; public event Action 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 listener) => _gameState.Subscribe(stat, listener); public void UnsubscribeFromStat(Stat stat, Action listener) => _gameState.Unsubscribe(stat, listener);