Skip to content

Commit 4268a2c

Browse files
committed
Caching, visuals and performance tweaks
1 parent 608718f commit 4268a2c

File tree

6 files changed

+198
-72
lines changed

6 files changed

+198
-72
lines changed

assets/textures/gui/bevel/MapDotIndicator.svg

Lines changed: 80 additions & 0 deletions
Loading
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://cbkfqk0lk6dhl"
6+
path="res://.godot/imported/MapDotIndicator.svg-0118f3fb709521b7aea1c36f416e97e1.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://assets/textures/gui/bevel/MapDotIndicator.svg"
14+
dest_files=["res://.godot/imported/MapDotIndicator.svg-0118f3fb709521b7aea1c36f416e97e1.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=true
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
35+
svg/scale=1.0
36+
editor/scale_with_editor_scale=false
37+
editor/convert_colors_with_editor_theme=false

src/microbe_stage/editor/PatchMapDrawer.cs

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
[gd_scene load_steps=4 format=2]
1+
[gd_scene load_steps=4 format=3 uid="uid://ui60hm6q35fb"]
22

3-
[ext_resource path="res://src/microbe_stage/editor/PatchMapDrawer.cs" type="Script" id=1]
4-
[ext_resource path="res://shaders/Monochrome.gdshader" type="Shader" id=2]
3+
[ext_resource type="Script" path="res://src/microbe_stage/editor/PatchMapDrawer.cs" id="1"]
4+
[ext_resource type="Shader" path="res://shaders/Monochrome.gdshader" id="2"]
55

6-
[sub_resource type="ShaderMaterial" id=1]
7-
shader = ExtResource( 2 )
6+
[sub_resource type="ShaderMaterial" id="1"]
7+
shader = ExtResource("2")
88

9-
[node name="PatchMapDrawer" type="Control"]
9+
[node name="PatchMapDrawer" type="Control" node_paths=PackedStringArray("populationIndicatorContainer")]
10+
layout_mode = 3
11+
anchors_preset = 15
1012
anchor_right = 1.0
1113
anchor_bottom = 1.0
12-
mouse_filter = 1
1314
size_flags_horizontal = 3
1415
size_flags_vertical = 3
15-
script = ExtResource( 1 )
16+
mouse_filter = 1
17+
script = ExtResource("1")
1618
DrawDefaultMapIfEmpty = true
1719
PatchNodeContainerPath = NodePath("PatchNodeContainer")
1820
LineContainerPath = NodePath("LineContainer")
19-
MonochromeMaterial = SubResource( 1 )
21+
MonochromeMaterial = SubResource("1")
22+
populationIndicatorContainer = NodePath("PopulationIndicatorContainer")
2023

2124
[node name="LineContainer" type="Control" parent="."]
25+
anchors_preset = 0
2226

2327
[node name="PatchNodeContainer" type="Control" parent="."]
28+
anchors_preset = 0
29+
30+
[node name="PopulationIndicatorContainer" type="Control" parent="."]
31+
layout_mode = 3
32+
anchors_preset = 0

src/microbe_stage/editor/PatchMapPopulationIndicator.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
[gd_scene load_steps=3 format=3 uid="uid://d368temjpdpxv"]
1+
[gd_scene load_steps=2 format=3 uid="uid://d368temjpdpxv"]
22

3-
[ext_resource type="Script" path="res://src/microbe_stage/editor/PatchMapPopulationIndicator.cs" id="1_8o4yl"]
4-
[ext_resource type="Texture2D" uid="uid://cs4ewaw3baruk" path="res://assets/textures/gui/bevel/GrabClick.svg" id="1_r6jqg"]
3+
[ext_resource type="Texture2D" uid="uid://cbkfqk0lk6dhl" path="res://assets/textures/gui/bevel/MapDotIndicator.svg" id="2_15hpe"]
54

6-
[node name="PatchMapPopulationIndicator" type="Control" node_paths=PackedStringArray("indicator")]
7-
layout_mode = 3
8-
anchors_preset = 0
9-
offset_right = 64.0
10-
offset_bottom = 64.0
11-
script = ExtResource("1_8o4yl")
12-
indicator = NodePath("TextureRect")
13-
14-
[node name="TextureRect" type="TextureRect" parent="."]
15-
layout_mode = 0
16-
offset_right = 10.0
17-
offset_bottom = 10.0
18-
texture = ExtResource("1_r6jqg")
5+
[node name="PatchMapPopulationIndicator" type="TextureRect"]
6+
offset_right = 6.0
7+
offset_bottom = 6.0
8+
texture = ExtResource("2_15hpe")
199
expand_mode = 1

0 commit comments

Comments
 (0)