Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dots indicating player population on patch map #5880

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions assets/textures/gui/bevel/MapDotIndicator.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions assets/textures/gui/bevel/MapDotIndicator.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://cbkfqk0lk6dhl"
path="res://.godot/imported/MapDotIndicator.svg-0118f3fb709521b7aea1c36f416e97e1.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://assets/textures/gui/bevel/MapDotIndicator.svg"
dest_files=["res://.godot/imported/MapDotIndicator.svg-0118f3fb709521b7aea1c36f416e97e1.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
5 changes: 5 additions & 0 deletions simulation_parameters/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,11 @@ public static class Constants

public const float PATCH_GENERATION_CHANCE_BANANA_BIOME = 0.03f;

/// <summary>
/// How many indicator dots on patch map are displayed per amount of population
/// </summary>
public const float PLAYER_POPULATION_INDICATORS_PER_POPULATION = 1.0f / 300.0f;

/// <summary>
/// If set to true, then physics debug draw gets enabled when the game starts
/// </summary>
Expand Down
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
67 changes: 66 additions & 1 deletion src/microbe_stage/editor/PatchMapDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public partial class PatchMapDrawer : Control
[Export]
public NodePath LineContainerPath = null!;

public uint PlayerSpeciesID = int.MaxValue;

#pragma warning disable CA2213
[Export]
public ShaderMaterial MonochromeMaterial = null!;
Expand All @@ -47,6 +49,10 @@ public partial class PatchMapDrawer : Control
private PackedScene nodeScene = null!;
private Control patchNodeContainer = null!;
private Control lineContainer = null!;
private Texture2D indicatorTexture = null!;

[Export]
private Control populationIndicatorContainer = null!;
#pragma warning restore CA2213

private PatchMap map = null!;
Expand All @@ -55,6 +61,10 @@ public partial class PatchMapDrawer : Control

private bool alreadyDrawn;

private int nextIndicatorIndex;

private List<Control> playerSpeciesPopulationIndicators = new();

private Dictionary<Patch, bool>? patchEnableStatusesToBeApplied;

private Patch? selectedPatch;
Expand Down Expand Up @@ -147,7 +157,7 @@ public override void _Ready()

patchNodeContainer = GetNode<Control>(PatchNodeContainerPath);
lineContainer = GetNode<Control>(LineContainerPath);

indicatorTexture = GD.Load<Texture2D>("res://assets/textures/gui/bevel/MapDotIndicator.svg");
nodeScene = GD.Load<PackedScene>("res://src/microbe_stage/editor/PatchMapNode.tscn");

if (DrawDefaultMapIfEmpty && Map == null)
Expand Down Expand Up @@ -1051,6 +1061,8 @@ private void RebuildMap()
nodes.Clear();
connections.Clear();

nextIndicatorIndex = 0;

if (Map == null)
{
SelectedPatch = null;
Expand All @@ -1062,6 +1074,12 @@ private void RebuildMap()
AddPatchNode(entry.Value, entry.Value.ScreenCoordinates);
}

// Hide excess cached indicators
for (var i = nextIndicatorIndex; i < playerSpeciesPopulationIndicators.Count - 1; ++i)
{
playerSpeciesPopulationIndicators[i].Hide();
}

bool runNodeSelectionsUpdate = true;

if (SelectedPatch != null)
Expand Down Expand Up @@ -1109,10 +1127,57 @@ private void AddPatchNode(Patch patch, Vector2 position)

patch.ApplyPatchEventVisuals(node);

var playerSpecies = patch.FindSpeciesByID(PlayerSpeciesID);
if (playerSpecies != null)
{
AddPlayerPopulationIndicators(patch, playerSpecies, node, position);
}

patchNodeContainer.AddChild(node);
nodes.Add(node.Patch, node);
}

private void AddPlayerPopulationIndicators(Patch patch, Species playerSpecies, Control node, Vector2 position)
{
var playerPopulationIndicatorAmount = (int)Math.Ceiling(patch.GetSpeciesSimulationPopulation(playerSpecies) *
Constants.PLAYER_POPULATION_INDICATORS_PER_POPULATION);

for (int i = 0; i < playerPopulationIndicatorAmount; ++i)
{
var noCached = nextIndicatorIndex >= playerSpeciesPopulationIndicators.Count;

Control indicator;
if (noCached)
{
indicator = new TextureRect
{
Texture = indicatorTexture,
ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize,
Size = new Vector2(6, 6),
};

indicator.MouseFilter = MouseFilterEnum.Ignore;
populationIndicatorContainer.AddChild(indicator);
playerSpeciesPopulationIndicators.Add(indicator);
}
else
{
indicator = playerSpeciesPopulationIndicators[nextIndicatorIndex];

indicator.Show();
}

var nodeModifier = node.Position.LengthSquared();
var modifierSinus = MathF.Sin(i);

indicator.Position = position + node.Size * 0.5f + new Vector2(0, 40)
.Rotated(nodeModifier * 30) +
new Vector2(0, modifierSinus * 40).Rotated(i * 6 * modifierSinus + nodeModifier);

++nextIndicatorIndex;
}
}

private void BuildPatchToRegionConnections(PatchRegion region1, PatchRegion region2, Vector2 regionPoint,
Vector2 regionPreviousPoint, MapElementVisibility regionVisibility)
{
Expand Down
29 changes: 20 additions & 9 deletions src/microbe_stage/editor/PatchMapDrawer.tscn
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=4 format=3 uid="uid://ui60hm6q35fb"]

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

[sub_resource type="ShaderMaterial" id=1]
shader = ExtResource( 2 )
[sub_resource type="ShaderMaterial" id="1"]
shader = ExtResource("2")

[node name="PatchMapDrawer" type="Control"]
[node name="PatchMapDrawer" type="Control" node_paths=PackedStringArray("populationIndicatorContainer")]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 1
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
size_flags_vertical = 3
script = ExtResource( 1 )
mouse_filter = 1
script = ExtResource("1")
DrawDefaultMapIfEmpty = true
PatchNodeContainerPath = NodePath("PatchNodeContainer")
LineContainerPath = NodePath("LineContainer")
MonochromeMaterial = SubResource( 1 )
MonochromeMaterial = SubResource("1")
populationIndicatorContainer = NodePath("PopulationIndicatorContainer")

[node name="PopulationIndicatorContainer" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0

[node name="LineContainer" type="Control" parent="."]
anchors_preset = 0

[node name="PatchNodeContainer" type="Control" parent="."]
anchors_preset = 0
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
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