nice crooked connections und energy UI

This commit is contained in:
2026-05-21 22:41:05 +02:00
parent 661e62e8b8
commit 9c7c414bc9
7 changed files with 185 additions and 29 deletions

29
Gui.cs
View File

@@ -1,13 +1,38 @@
using System.Globalization;
using Godot;
using NodeWar;
using GodotUtilities;
namespace NodeWar;
[Scene]
public partial class Gui : Control
{
[Node]
private Label _infoLabel;
[Node]
private Label _energyGeneration;
[Node]
private Label _energyConsumption;
[Node]
private Label _energyStorage;
public override void _Ready()
{
_infoLabel = GetNode<Label>("%InfoLabel");
_energyGeneration.Text = 1f.ToString(CultureInfo.InvariantCulture);
_energyConsumption.Text = 0f.ToString(CultureInfo.InvariantCulture);
_energyStorage.Text = 100f.ToString(CultureInfo.InvariantCulture);
Autoloads.EventBus.SelectionChanged += text => _infoLabel.Text = text;
}
public override void _Notification(int what)
{
if (what == NotificationSceneInstantiated)
{
WireNodes();
}
}
}

View File

@@ -1,23 +1,28 @@
using System;
using System.Collections.Generic;
using Godot;
using GodotUtilities;
using NodeWar.scripts;
namespace NodeWar;
public enum NoduleType
{
Base,
Connector,
}
[Scene]
public partial class Nodule : Area2D
{
private const float GrowthFactor = 0.01f;
[Export]
public float Radius { get; set; } = 120;
private NoduleType _noduleType;
[Export]
public float MaxRadius { get; set; } = 360;
[Export]
public CollisionShape2D Collision { get; set; }
[Export]
public bool BuildMode { get; set; } = false;
@@ -35,13 +40,30 @@ public partial class Nodule : Area2D
[Signal]
public delegate void BuildingCanceledEventHandler();
[Node]
private Node2D _gfx;
[Node]
private CollisionShape2D _baseCollision;
public override void _Ready()
{
Radius = Age * MaxRadius;
AreaEntered += OnAreaEntered;
AreaExited += OnAreaExited;
switch (_noduleType)
{
case NoduleType.Base:
_gfx.Scale = new Vector2(2, 2);
Modulate = Colors.Red;
_baseCollision.Disabled = false;
break;
case NoduleType.Connector:
break;
default:
throw new ArgumentOutOfRangeException();
}
QueueRedraw();
}
@@ -97,7 +119,6 @@ public partial class Nodule : Area2D
Age += GrowthFactor * delta.ToFloat();
Age = MathF.Min(Age, 1);
Radius = Age * MaxRadius;
QueueRedraw();
}
@@ -127,13 +148,22 @@ public partial class Nodule : Area2D
}
}
if (BuildMode)
var value = 200 * Age;
if (!BuildMode)
{
DrawCircle(Vector2.Zero, Radius, new Color(Colors.DarkOliveGreen, 0.2f));
DrawRect(
new Rect2(-value / 2, -value / 2, value, value),
new Color(Colors.WhiteSmoke, 0.4f)
);
}
else
}
public override void _Notification(int what)
{
DrawCircle(Vector2.Zero, Radius, new Color(Colors.WhiteSmoke, 0.4f));
if (what == NotificationSceneInstantiated)
{
WireNodes();
}
}
}

36
Root.cs
View File

@@ -1,4 +1,6 @@
using System.Linq;
using Godot;
using GodotSharp.SourceGenerators;
using GodotUtilities;
namespace NodeWar;
@@ -7,17 +9,45 @@ namespace NodeWar;
public partial class Root : Node2D
{
[Node]
private Line2D line2D;
private Line2D _line2D;
private Nodule a;
private Nodule b;
public override void _Ready()
{
GD.Print(line2D);
GD.Print(_line2D);
}
public void ConnectNodules(Nodule a, Nodule b)
{
this.a = a;
this.b = b;
GlobalPosition = a.GlobalPosition;
line2D.Points = [Vector2.Zero, b.GlobalPosition - a.GlobalPosition];
GenerateLinePoints();
}
private void GenerateLinePoints()
{
var direction = b.GlobalPosition - a.GlobalPosition;
var sectionLength = direction.Length() / 5;
var points = new Vector2[5];
for (var i = 0; i < 5; i++)
{
var randomOffset = new Vector2(MathUtil.RNG.Randf(), MathUtil.RNG.Randf());
points[i] = i switch
{
0 => Vector2.Zero,
4 => direction,
_ => (direction.Normalized() + (randomOffset * 0.1f)).Normalized()
* sectionLength
* i,
};
}
_line2D.Points = points;
}
public override void _Notification(int what)

View File

@@ -2,6 +2,13 @@
[ext_resource type="Script" uid="uid://7jql2b33svuv" path="res://Gui.cs" id="1_80edf"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_80edf"]
bg_color = Color(0.7245737, 0.5838865, 0, 1)
corner_radius_top_left = 8
corner_radius_top_right = 8
corner_radius_bottom_right = 8
corner_radius_bottom_left = 8
[sub_resource type="LabelSettings" id="LabelSettings_k6omp"]
line_spacing = -2.0
font_size = 14
@@ -15,6 +22,58 @@ grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_80edf")
[node name="Top" type="PanelContainer" parent="." unique_id=1900556325]
layout_mode = 0
offset_left = 8.0
offset_top = 8.0
offset_right = 406.0
offset_bottom = 58.0
[node name="MarginContainer" type="MarginContainer" parent="Top" unique_id=653072330]
layout_mode = 2
theme_override_constants/margin_left = 8
theme_override_constants/margin_right = 8
[node name="HBoxContainer" type="HBoxContainer" parent="Top/MarginContainer" unique_id=1812335530]
layout_mode = 2
theme_override_constants/separation = 4
[node name="VBoxContainer" type="VBoxContainer" parent="Top/MarginContainer/HBoxContainer" unique_id=523855758]
custom_minimum_size = Vector2(54, 0)
layout_mode = 2
theme_override_constants/separation = -4
alignment = 1
[node name="EnergyGeneration" type="Label" parent="Top/MarginContainer/HBoxContainer/VBoxContainer" unique_id=358814115]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 8
text = "XX.XX"
[node name="EnergyConsumption" type="Label" parent="Top/MarginContainer/HBoxContainer/VBoxContainer" unique_id=343891974]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 8
theme_override_colors/font_color = Color(0.8701904, 0, 0.20575893, 1)
text = "-XX.XX"
[node name="EnergyProgressBar" type="ProgressBar" parent="Top/MarginContainer/HBoxContainer" unique_id=1761250517]
unique_name_in_owner = true
custom_minimum_size = Vector2(0, 25)
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 4
theme_override_styles/fill = SubResource("StyleBoxFlat_80edf")
value = 37.15
show_percentage = false
[node name="EnergyStorage" type="Label" parent="Top/MarginContainer/HBoxContainer" unique_id=1992827443]
unique_name_in_owner = true
custom_minimum_size = Vector2(80, 0)
layout_mode = 2
size_flags_horizontal = 0
text = "MM,MMM"
[node name="PanelContainer" type="PanelContainer" parent="." unique_id=2071011957]
layout_mode = 1
anchors_preset = 2

View File

@@ -63,3 +63,4 @@ translation_speed = 300.0
metadata/_custom_type_script = "uid://rirna2vebukw"
[node name="Nodule" parent="." unique_id=698496795 instance=ExtResource("8_5vw27")]
_noduleType = 0

View File

@@ -2,12 +2,15 @@
[ext_resource type="Script" uid="uid://7em4o0ud8sgj" path="res://Nodule.cs" id="1_beybw"]
[ext_resource type="Script" uid="uid://de3jpss66xjfh" path="res://addons/curved_lines_2d/scalable_vector_shape_2d.gd" id="2_cahmm"]
[ext_resource type="PackedScene" uid="uid://cd47em5shcnid" path="res://root.tscn" id="2_k6tuy"]
[ext_resource type="Script" uid="uid://dlbv4pit17dnu" path="res://addons/curved_lines_2d/scalable_arc.gd" id="3_fnyqb"]
[ext_resource type="Script" uid="uid://dl1t88tthmwts" path="res://addons/curved_lines_2d/scalable_arc_list.gd" id="4_qiyup"]
[sub_resource type="CircleShape2D" id="CircleShape2D_7mycd"]
resource_local_to_scene = true
radius = 200.0
[sub_resource type="RectangleShape2D" id="RectangleShape2D_bv6fd"]
size = Vector2(150, 150)
[sub_resource type="CircleShape2D" id="CircleShape2D_k6tuy"]
radius = 140.0
[sub_resource type="Curve2D" id="Curve2D_272bh"]
resource_local_to_scene = true
@@ -20,16 +23,24 @@ point_count = 5
resource_local_to_scene = true
script = ExtResource("4_qiyup")
[node name="Nodule" type="Area2D" unique_id=698496795 node_paths=PackedStringArray("Collision")]
[node name="Nodule" type="Area2D" unique_id=698496795]
script = ExtResource("1_beybw")
Radius = 10.0
Collision = NodePath("Collision")
_noduleType = 1
Age = 0.5
RootScene = ExtResource("2_k6tuy")
[node name="Collision" type="CollisionShape2D" parent="." unique_id=402084582]
shape = SubResource("CircleShape2D_7mycd")
rotation = 0.7853982
shape = SubResource("RectangleShape2D_bv6fd")
[node name="Ellipse2" type="Node2D" parent="." unique_id=1962026750 node_paths=PackedStringArray("polygon", "line")]
[node name="Collision2" type="CollisionShape2D" parent="." unique_id=1073775238]
shape = SubResource("RectangleShape2D_bv6fd")
[node name="BaseCollision" type="CollisionShape2D" parent="." unique_id=1607878878]
shape = SubResource("CircleShape2D_k6tuy")
disabled = true
[node name="Gfx" type="Node2D" parent="." unique_id=1962026750 node_paths=PackedStringArray("polygon", "line")]
z_index = 10
script = ExtResource("2_cahmm")
polygon = NodePath("Fill")
@@ -44,12 +55,12 @@ size = Vector2(20, 20)
rx = 10.0
ry = 10.0
[node name="Fill" type="Polygon2D" parent="Ellipse2" unique_id=1355608579]
[node name="Fill" type="Polygon2D" parent="Gfx" unique_id=1355608579]
color = Color(0.21759033, 0.88248014, 0.8984375, 1)
polygon = PackedVector2Array(10, 0, 9.948373, 1.022467, 9.796842, 2.0153925, 9.550433, 2.9737506, 9.214172, 3.8925154, 8.793085, 4.766661, 8.292197, 5.5911617, 7.7165356, 6.3609915, 7.071125, 7.071125, 6.360992, 7.7165356, 5.591162, 8.292197, 4.766661, 8.793085, 3.8925154, 9.214172, 2.9737508, 9.550432, 2.0153925, 9.796842, 1.022467, 9.948373, 0, 10, -1.022467, 9.948373, -2.0153925, 9.796842, -2.9737506, 9.550433, -3.8925154, 9.214172, -4.766661, 8.793085, -5.5911617, 8.292197, -6.3609915, 7.7165356, -7.071125, 7.071125, -7.7165356, 6.360992, -8.292197, 5.591162, -8.793085, 4.766661, -9.214172, 3.8925154, -9.550432, 2.9737508, -9.796842, 2.0153925, -9.948373, 1.022467, -10, 0, -9.948373, -1.022467, -9.796842, -2.0153925, -9.550433, -2.9737506, -9.214172, -3.8925154, -8.793085, -4.766661, -8.292197, -5.5911617, -7.7165356, -6.3609915, -7.071125, -7.071125, -6.360992, -7.7165356, -5.591162, -8.292197, -4.766661, -8.793085, -3.8925154, -9.214172, -2.9737508, -9.550432, -2.0153925, -9.796842, -1.022467, -9.948373, 0, -10, 1.022467, -9.948373, 2.0153925, -9.796842, 2.9737506, -9.550433, 3.8925154, -9.214172, 4.766661, -8.793085, 5.5911617, -8.292197, 6.3609915, -7.7165356, 7.071125, -7.071125, 7.7165356, -6.360992, 8.292197, -5.591162, 8.793085, -4.766661, 9.214172, -3.8925154, 9.550432, -2.9737508, 9.796842, -2.0153925, 9.948373, -1.022467)
metadata/_edit_lock_ = true
[node name="Stroke" type="Line2D" parent="Ellipse2" unique_id=1428670823]
[node name="Stroke" type="Line2D" parent="Gfx" unique_id=1428670823]
points = PackedVector2Array(10, 0, 9.948373, 1.022467, 9.796842, 2.0153925, 9.550433, 2.9737506, 9.214172, 3.8925154, 8.793085, 4.766661, 8.292197, 5.5911617, 7.7165356, 6.3609915, 7.071125, 7.071125, 6.360992, 7.7165356, 5.591162, 8.292197, 4.766661, 8.793085, 3.8925154, 9.214172, 2.9737508, 9.550432, 2.0153925, 9.796842, 1.022467, 9.948373, 0, 10, -1.022467, 9.948373, -2.0153925, 9.796842, -2.9737506, 9.550433, -3.8925154, 9.214172, -4.766661, 8.793085, -5.5911617, 8.292197, -6.3609915, 7.7165356, -7.071125, 7.071125, -7.7165356, 6.360992, -8.292197, 5.591162, -8.793085, 4.766661, -9.214172, 3.8925154, -9.550432, 2.9737508, -9.796842, 2.0153925, -9.948373, 1.022467, -10, 0, -9.948373, -1.022467, -9.796842, -2.0153925, -9.550433, -2.9737506, -9.214172, -3.8925154, -8.793085, -4.766661, -8.292197, -5.5911617, -7.7165356, -6.3609915, -7.071125, -7.071125, -6.360992, -7.7165356, -5.591162, -8.292197, -4.766661, -8.793085, -3.8925154, -9.214172, -2.9737508, -9.550432, -2.0153925, -9.796842, -1.022467, -9.948373, 0, -10, 1.022467, -9.948373, 2.0153925, -9.796842, 2.9737506, -9.550433, 3.8925154, -9.214172, 4.766661, -8.793085, 5.5911617, -8.292197, 6.3609915, -7.7165356, 7.071125, -7.071125, 7.7165356, -6.360992, 8.292197, -5.591162, 8.793085, -4.766661, 9.214172, -3.8925154, 9.550432, -2.9737508, 9.796842, -2.0153925, 9.948373, -1.022467)
closed = true
width = 4.0

View File

@@ -3,12 +3,12 @@
[ext_resource type="Script" uid="uid://but18j8vnbom4" path="res://Root.cs" id="1_pyidc"]
[sub_resource type="Curve" id="Curve_pq8q7"]
_data = [Vector2(0.051515155, 0.8803681), 0.0, 0.0, 0, 0, Vector2(0.054545455, 0.47361964), 0.0, 0.0, 0, 0, Vector2(0.087878786, 0.8165644), 0.0, 0.0, 0, 0, Vector2(0.12727273, 0.32208586), 0.0, 0.0, 0, 0, Vector2(0.19090909, 0.67386234), 0.0, 0.0, 0, 0, Vector2(0.34242424, 0.7876313), 0.0, 0.0, 0, 0, Vector2(0.38181818, 0.34601223), 0.0, 0.0, 0, 0, Vector2(0.530303, 0.4815951), 0.0, 0.0, 0, 0, Vector2(0.7151515, 0.66503066), 0.0, 0.0, 0, 0, Vector2(0.75454545, 0.3141104), 0.0, 0.0, 0, 0, Vector2(0.8575757, 0.38588953), 0.0, 0.0, 0, 0, Vector2(0.8636364, 0.7766871), 0.0, 0.0, 0, 0, Vector2(0.9, 0.8165644), 0.0, 0.0, 0, 0, Vector2(0.94545454, 0.37791407), 0.0, 0.0, 0, 0]
point_count = 14
_data = [Vector2(0, 0.22374105), 0.0, 0.0, 0, 0, Vector2(0.055172414, 0.6165468), 0.0, 0.0, 0, 0, Vector2(0.120689645, 0.27050364), 0.0, 0.0, 0, 0, Vector2(0.16206896, 0.7177748), 0.0, 0.0, 0, 0, Vector2(0.26551723, 0.21506107), 0.0, 0.0, 0, 0, Vector2(0.27586204, 0.7194245), 0.0, 0.0, 0, 0, Vector2(0.37931037, 0.32661873), 0.0, 0.0, 0, 0, Vector2(0.4137931, 0.7755396), 0.0, 0.0, 0, 0, Vector2(0.4862069, 0.18633103), 0.0, 0.0, 0, 0, Vector2(0.55172414, 0.7194245), 0.0, 0.0, 0, 0, Vector2(0.6137931, 0.24244606), 0.0, 0.0, 0, 0, Vector2(0.6758621, 0.59784174), 0.0, 0.0, 0, 0, Vector2(0.72068965, 0.19568348), 0.0, 0.0, 0, 0, Vector2(0.77586204, 0.6258993), 0.0, 0.0, 0, 0, Vector2(0.8344827, 0.19568348), 0.0, 0.0, 0, 0, Vector2(0.9137931, 0.1489209), 0.0, 0.0, 0, 0, Vector2(0.9862069, 0.6258993), 0.0, 0.0, 0, 0, Vector2(1, 0.20503604), 0.0, 0.0, 0, 0]
point_count = 18
[node name="Root" type="Node2D" unique_id=1632219197]
script = ExtResource("1_pyidc")
[node name="Line2D" type="Line2D" parent="." unique_id=388168093]
points = PackedVector2Array(0, 0, -25, -59, -53, -88, -94, -96, -130, -114, -151, -125, -167, -148, -188, -168, -207, -199, -242, -220)
points = PackedVector2Array(16.4, 0, -25, -59, -53, -88, -94, -96, -130, -114, -151, -125, -167, -148, -188, -168, -207, -199, -243.105, -220)
width_curve = SubResource("Curve_pq8q7")