Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/macroscopic_stage/editor/MacroscopicEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected override void ResolveDerivedTypeNodeReferences()

protected override void InitEditor(bool fresh)
{
patchMapTab.SetMap(CurrentGame.GameWorld.Map);
patchMapTab.SetMap(CurrentGame.GameWorld.Map, CurrentGame.GameWorld.PlayerSpecies.ID);

base.InitEditor(fresh);

Expand Down
2 changes: 1 addition & 1 deletion src/microbe_stage/editor/MicrobeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected override void ResolveDerivedTypeNodeReferences()

protected override void InitEditor(bool fresh)
{
patchMapTab.SetMap(CurrentGame.GameWorld.Map);
patchMapTab.SetMap(CurrentGame.GameWorld.Map, CurrentGame.GameWorld.PlayerSpecies.ID);

base.InitEditor(fresh);

Expand Down
26 changes: 26 additions & 0 deletions src/microbe_stage/editor/PatchMapDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public partial class PatchMapDrawer : Control

#pragma warning disable CA2213
private PackedScene nodeScene = null!;
private PackedScene populationIndicatorScene = null!;
private Control patchNodeContainer = null!;
private Control lineContainer = null!;
#pragma warning restore CA2213
Expand Down Expand Up @@ -141,6 +142,11 @@ public Patch? SelectedPatch
/// </summary>
public Action<PatchMapDrawer>? OnSelectedPatchChanged { get; set; }

/// <summary>
/// Player species ID for player population indicator (dots on patch map)
/// </summary>
public uint PlayerSpeciesID { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could default to -1 (or uint.MaxValue if C# complains) so that this doesn't accidentally match anything if there's a bug elsewhere in the code that causes a species with ID = 0 to be created.


public override void _Ready()
{
base._Ready();
Expand All @@ -149,6 +155,8 @@ public override void _Ready()
lineContainer = GetNode<Control>(LineContainerPath);

nodeScene = GD.Load<PackedScene>("res://src/microbe_stage/editor/PatchMapNode.tscn");
populationIndicatorScene = GD.Load<PackedScene>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a single node so for performance I don't think this should be made as a separate scene, but just instantiate the nodes with the right texture (load the texture here) in this class.

"res://src/microbe_stage/editor/PatchMapPopulationIndicator.tscn");

if (DrawDefaultMapIfEmpty && Map == null)
{
Expand Down Expand Up @@ -1109,6 +1117,24 @@ private void AddPatchNode(Patch patch, Vector2 position)

patch.ApplyPatchEventVisuals(node);

var playerSpecies = patch.FindSpeciesByID(PlayerSpeciesID);
if (playerSpecies != null)
{
var playerPopulation = patch.GetSpeciesSimulationPopulation(playerSpecies);

for (var i = 0; i < playerPopulation * 0.001; ++i)
{
var indicator = populationIndicatorScene.Instantiate<PatchMapPopulationIndicator>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be worthwhile probably to have a cache of these objects as I think otherwise this will increase the amount of memory allocations quite a lot here.

Overall the architecture isn't far off from being pretty much exactly what I would have done for this feature. So if this is slightly tweaked and the visuals improved a bit, this PR will be good to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By cache you mean a list created in this create-patch-node method, a list in the patch map drawer class, or something else completely? I'm not questioning why, since you are much more experienced but I'm curious how it reduces allocations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Queue or a Stack in the patch map drawer class, so it can just create them once and then reuse them.

Once the queue is filled up once with unused objects, the next time the dots need to be put in the map, they can be pulled from the queue and refilled. So this way the dots are instantiated just once, instead of being instantiated on each map draw.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started to work on this but I realized that indicators are children of patch nodes, which are deleted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if they weren't? What if they were added as children to the patch map itself? AFAIK the patch nodes are added as children to the drawer node in order to position them and make them display at the right position on screen.

indicator.IndicatorPositionModifier = i;
indicator.Position = position;
indicator.MouseFilter = MouseFilterEnum.Ignore;
indicator.UpdateIndicator(node);
node.AddChild(indicator);

indicator.ShowBehindParent = true;
}
}

patchNodeContainer.AddChild(node);
nodes.Add(node.Patch, node);
}
Expand Down
3 changes: 2 additions & 1 deletion src/microbe_stage/editor/PatchMapEditorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public override void Init(TEditor owningEditor, bool fresh)
UpdateSeedLabel();
}

public void SetMap(PatchMap map)
public void SetMap(PatchMap map, uint playerSpeciesID)
{
mapDrawer.PlayerSpeciesID = playerSpeciesID;
mapDrawer.Map = map;
}

Expand Down
35 changes: 35 additions & 0 deletions src/microbe_stage/editor/PatchMapPopulationIndicator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Godot;

/// <summary>
/// Dots scattered around patch map nodes, indication population of player species
/// </summary>
public partial class PatchMapPopulationIndicator : Control
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's going to be a ton of these, so I think that actually this shouldn't be a node type with a script attached. And instead just I'd inline this code to the patch map drawer class (though it should be cleanly put into a separate section as it is already a pretty long file). I think the performance impact will be much less when there's just simple graphics used for the indicators rather than nodes with scripts attached.

{
private int positionModifier;

#pragma warning disable CA2213
[Export]
private TextureRect indicator = null!;
#pragma warning restore CA2213

public int IndicatorPositionModifier
{
get
{
return positionModifier;
}
set
{
positionModifier = value;
}
}

public void UpdateIndicator(Control parent)
{
var nodeModifier = parent.Position.LengthSquared();
var modifierSinus = Mathf.Sin(IndicatorPositionModifier);

indicator.Position = Size * 0.5f + new Vector2(0, 40).Rotated(nodeModifier * 20) + new Vector2(
0, modifierSinus * 50).Rotated(IndicatorPositionModifier * 6 * modifierSinus + nodeModifier);
}
}
19 changes: 19 additions & 0 deletions src/microbe_stage/editor/PatchMapPopulationIndicator.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[gd_scene load_steps=3 format=3 uid="uid://d368temjpdpxv"]

[ext_resource type="Script" path="res://src/microbe_stage/editor/PatchMapPopulationIndicator.cs" id="1_8o4yl"]
[ext_resource type="Texture2D" uid="uid://cs4ewaw3baruk" path="res://assets/textures/gui/bevel/GrabClick.svg" id="1_r6jqg"]

[node name="PatchMapPopulationIndicator" type="Control" node_paths=PackedStringArray("indicator")]
layout_mode = 3
anchors_preset = 0
offset_right = 64.0
offset_bottom = 64.0
script = ExtResource("1_8o4yl")
indicator = NodePath("TextureRect")

[node name="TextureRect" type="TextureRect" parent="."]
layout_mode = 0
offset_right = 10.0
offset_bottom = 10.0
texture = ExtResource("1_r6jqg")
expand_mode = 1
2 changes: 1 addition & 1 deletion src/multicellular_stage/editor/MulticellularEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected override void ResolveDerivedTypeNodeReferences()

protected override void InitEditor(bool fresh)
{
patchMapTab.SetMap(CurrentGame.GameWorld.Map);
patchMapTab.SetMap(CurrentGame.GameWorld.Map, CurrentGame.GameWorld.PlayerSpecies.ID);

base.InitEditor(fresh);

Expand Down