@@ -11,6 +11,8 @@ public partial class PatchMapDrawer : Control
1111 [ Export ]
1212 public bool DrawDefaultMapIfEmpty ;
1313
14+ public uint PlayerSpeciesID = int . MaxValue ;
15+
1416 [ Export ( PropertyHint . ColorNoAlpha ) ]
1517 public Color InterConnectionColor = Colors . WebGreen ;
1618
@@ -48,6 +50,9 @@ public partial class PatchMapDrawer : Control
4850 private PackedScene populationIndicatorScene = null ! ;
4951 private Control patchNodeContainer = null ! ;
5052 private Control lineContainer = null ! ;
53+
54+ [ Export ]
55+ private Control populationIndicatorContainer = null ! ;
5156#pragma warning restore CA2213
5257
5358 private PatchMap map = null ! ;
@@ -56,6 +61,8 @@ public partial class PatchMapDrawer : Control
5661
5762 private bool alreadyDrawn ;
5863
64+ private List < Control > playerSpeciesPopulationIndicators = new ( ) ;
65+
5966 private Dictionary < Patch , bool > ? patchEnableStatusesToBeApplied ;
6067
6168 private Patch ? selectedPatch ;
@@ -142,11 +149,6 @@ public Patch? SelectedPatch
142149 /// </summary>
143150 public Action < PatchMapDrawer > ? OnSelectedPatchChanged { get ; set ; }
144151
145- /// <summary>
146- /// Player species ID for player population indicator (dots on patch map)
147- /// </summary>
148- public uint PlayerSpeciesID { get ; set ; }
149-
150152 public override void _Ready ( )
151153 {
152154 base . _Ready ( ) ;
@@ -1055,6 +1057,13 @@ private void DrawPatchLinks()
10551057 /// </summary>
10561058 private void RebuildMap ( )
10571059 {
1060+ playerSpeciesPopulationIndicators = new ( ) ;
1061+ foreach ( var node in populationIndicatorContainer . GetChildren ( ) )
1062+ {
1063+ if ( node is Control indicator )
1064+ playerSpeciesPopulationIndicators . Add ( indicator ) ;
1065+ }
1066+
10581067 patchNodeContainer . FreeChildren ( ) ;
10591068 nodes . Clear ( ) ;
10601069 connections . Clear ( ) ;
@@ -1120,18 +1129,54 @@ private void AddPatchNode(Patch patch, Vector2 position)
11201129 var playerSpecies = patch . FindSpeciesByID ( PlayerSpeciesID ) ;
11211130 if ( playerSpecies != null )
11221131 {
1123- var playerPopulation = patch . GetSpeciesSimulationPopulation ( playerSpecies ) ;
1132+ var playerPopulationIndicatorAmount = ( int ) Math . Ceiling (
1133+ patch . GetSpeciesSimulationPopulation ( playerSpecies ) * 0.004 ) ;
1134+
1135+ var indicatorExcess = Mathf . Clamp (
1136+ playerSpeciesPopulationIndicators . Count - playerPopulationIndicatorAmount , 0 , playerSpeciesPopulationIndicators . Count ) ;
1137+
1138+ for ( int i = 0 ; i < indicatorExcess ; ++ i )
1139+ {
1140+ playerSpeciesPopulationIndicators . Last ( ) . QueueFree ( ) ;
1141+ }
11241142
1125- for ( var i = 0 ; i < playerPopulation * 0.001 ; ++ i )
1143+ // Trip the list to keep it clean of disposed objects
1144+ var range = playerSpeciesPopulationIndicators . Count - indicatorExcess ;
1145+ if ( range > 0 )
1146+ playerSpeciesPopulationIndicators = playerSpeciesPopulationIndicators . GetRange ( 0 , range ) ;
1147+
1148+ for ( int i = 0 ; i < playerPopulationIndicatorAmount ; ++ i )
11261149 {
1127- var indicator = populationIndicatorScene . Instantiate < PatchMapPopulationIndicator > ( ) ;
1128- indicator . IndicatorPositionModifier = i ;
1150+ var noCached = i >= playerSpeciesPopulationIndicators . Count ;
1151+
1152+ Control indicator ;
1153+ if ( noCached )
1154+ {
1155+ indicator = populationIndicatorScene . Instantiate < Control > ( ) ;
1156+ }
1157+ else
1158+ {
1159+ indicator = playerSpeciesPopulationIndicators [ i ] ;
1160+ }
1161+
11291162 indicator . Position = position ;
11301163 indicator . MouseFilter = MouseFilterEnum . Ignore ;
1131- indicator . UpdateIndicator ( node ) ;
1132- node . AddChild ( indicator ) ;
11331164
1134- indicator . ShowBehindParent = true ;
1165+ var nodeModifier = node . Position . LengthSquared ( ) ;
1166+ var modifierSinus = Mathf . Sin ( i ) ;
1167+
1168+ if ( noCached )
1169+ {
1170+ patchNodeContainer . AddChild ( indicator ) ;
1171+ }
1172+
1173+ playerSpeciesPopulationIndicators . Remove ( indicator ) ;
1174+
1175+ indicator . Position = node . Position + new Vector2 ( 0 , 20 )
1176+ . Rotated ( nodeModifier * 30 ) + new Vector2 ( 0 , modifierSinus * 50 ) . Rotated (
1177+ i * 6 * modifierSinus + nodeModifier ) ;
1178+
1179+ indicator . ZIndex = - 1 ;
11351180 }
11361181 }
11371182
0 commit comments