diff --git a/BuildManager.cs b/BuildManager.cs index 347373c..af55782 100644 --- a/BuildManager.cs +++ b/BuildManager.cs @@ -3,58 +3,50 @@ using Godot; namespace NodeWar; +[Singleton] public partial class BuildManager : Node { - public bool Building { get; set; } = false; + private bool Building { get; set; } - public Nodule BuildingNodule { get; set; } - - public override void _Ready() { } - - [Export] - private PackedScene _noduleScene; - - [Export] - private PackedScene _rootScene; + private Nodule BuildingNodule { get; set; } private Dictionary NoduleConnections { get; set; } = new(); public override void _Process(double delta) { - if (Input.IsActionJustPressed("build") && !Building) + if (Input.IsActionJustPressed(MyInput.Build) && !Building) { - Building = true; - BuildingNodule = _noduleScene.Instantiate(); - BuildingNodule.BuildMode = true; - - BuildingNodule.NoduleBuilt += () => - { - Building = false; - - // try connecting built nodule to connected ones - foreach (var connectedNodule in BuildingNodule.ConnectedNodules) - { - RegisterConnection(BuildingNodule, connectedNodule); - } - - BuildingNodule = null; - }; - - BuildingNodule.BuildingCanceled += () => - { - Building = false; - BuildingNodule = null; - }; - - GetParent().AddChild(BuildingNodule); + StartBuilding(); } } + private void StartBuilding() + { + BuildingNodule = Instantiator.Instantiate(); + + Building = true; + BuildingNodule.BuildMode = true; + + BuildingNodule.NoduleBuilt += () => + { + Building = false; + BuildingNodule = null; + }; + + BuildingNodule.BuildingCanceled += () => + { + Building = false; + BuildingNodule = null; + }; + + GetParent().AddChild(BuildingNodule); + } + public void RegisterConnection(Nodule a, Nodule b) { if (!NoduleConnections.ContainsKey(new NoduleConnection { A = a, B = b })) { - var root = _rootScene.Instantiate(); + var root = Instantiator.Instantiate(); GetParent().AddChild(root); root.ConnectNodules(a, b); diff --git a/ConnectionArea.cs b/ConnectionArea.cs new file mode 100644 index 0000000..fac6987 --- /dev/null +++ b/ConnectionArea.cs @@ -0,0 +1,69 @@ +using Godot; + +namespace NodeWar; + +[SceneTree] +public partial class ConnectionArea : Area2D +{ + [Signal] + public delegate void NoduleEnteredEventHandler(Nodule nodule); + + [Signal] + public delegate void NoduleExitedEventHandler(Nodule nodule); + + public override void _Ready() + { + AreaEntered += OnAreaEntered; + AreaExited += OnAreaExited; + } + + private void OnAreaEntered(Area2D area) + { + switch (area) + { + case Nodule nodule: + HandleNoduleEntered(nodule); + break; + case ConnectionArea connectionArea: + HandleNoduleEntered(connectionArea.GetParent() as Nodule); + break; + } + + return; + + void HandleNoduleEntered(Nodule newNodule) + { + if (newNodule.BuildMode) + { + return; + } + + EmitSignalNoduleEntered(newNodule); + } + } + + private void OnAreaExited(Area2D area) + { + switch (area) + { + case Nodule nodule: + HandleNoduleExited(nodule); + break; + case ConnectionArea connectionArea: + HandleNoduleExited(connectionArea.GetParent() as Nodule); + break; + } + + return; + + void HandleNoduleExited(Nodule newNodule) + { + if (newNodule.BuildMode) + { + return; + } + + EmitSignalNoduleExited(newNodule); + } + } +} diff --git a/ConnectionArea.cs.uid b/ConnectionArea.cs.uid new file mode 100644 index 0000000..3c9f2e4 --- /dev/null +++ b/ConnectionArea.cs.uid @@ -0,0 +1 @@ +uid://w5ba3yo120vc diff --git a/ConnectionArea.tscn b/ConnectionArea.tscn new file mode 100644 index 0000000..794aa53 --- /dev/null +++ b/ConnectionArea.tscn @@ -0,0 +1,17 @@ +[gd_scene format=3 uid="uid://c4oxc6kyp3muy"] + +[ext_resource type="Script" uid="uid://w5ba3yo120vc" path="res://ConnectionArea.cs" id="1_djo5v"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_bv6fd"] +size = Vector2(150, 150) + +[node name="ConnectionArea" type="Area2D" unique_id=943082837] +script = ExtResource("1_djo5v") + +[node name="Collision" type="CollisionShape2D" parent="." unique_id=260055498] +rotation = 0.7853982 +scale = Vector2(0.99999994, 0.99999994) +shape = SubResource("RectangleShape2D_bv6fd") + +[node name="Collision2" type="CollisionShape2D" parent="." unique_id=1676208642] +shape = SubResource("RectangleShape2D_bv6fd") diff --git a/Main.cs b/Main.cs new file mode 100644 index 0000000..ce7e148 --- /dev/null +++ b/Main.cs @@ -0,0 +1,11 @@ +using System; +using Godot; +using NodeWar; + +public partial class Main : Node2D +{ + public override void _Ready() + { + var asd = BuildManager.Instance; + } +} diff --git a/Main.cs.uid b/Main.cs.uid new file mode 100644 index 0000000..afede49 --- /dev/null +++ b/Main.cs.uid @@ -0,0 +1 @@ +uid://cmcnly8kwwjia diff --git a/MyInput.cs b/MyInput.cs new file mode 100644 index 0000000..9db798d --- /dev/null +++ b/MyInput.cs @@ -0,0 +1,6 @@ +using Godot; + +namespace NodeWar; + +[InputMap] +public static partial class MyInput; diff --git a/MyInput.cs.uid b/MyInput.cs.uid new file mode 100644 index 0000000..d25e703 --- /dev/null +++ b/MyInput.cs.uid @@ -0,0 +1 @@ +uid://qhvdkf6ohu4d diff --git a/MyRes.cs b/MyRes.cs new file mode 100644 index 0000000..7443a63 --- /dev/null +++ b/MyRes.cs @@ -0,0 +1,6 @@ +using Godot; + +namespace NodeWar; + +[ResourceTree("/")] +public static partial class MyRes; diff --git a/MyRes.cs.uid b/MyRes.cs.uid new file mode 100644 index 0000000..4185800 --- /dev/null +++ b/MyRes.cs.uid @@ -0,0 +1 @@ +uid://vqjanihdukjs diff --git a/Node War.csproj b/Node War.csproj index 43c5d47..5bd7b65 100644 --- a/Node War.csproj +++ b/Node War.csproj @@ -1,18 +1,13 @@ - + net10.0 - net9.0 + net9.0 true NodeWar 14 - + diff --git a/Node War.csproj.old b/Node War.csproj.old new file mode 100644 index 0000000..43c5d47 --- /dev/null +++ b/Node War.csproj.old @@ -0,0 +1,18 @@ + + + net10.0 + net9.0 + true + NodeWar + 14 + + + + + + diff --git a/Nodule.cs b/Nodule.cs index e431f02..ff0eca5 100644 --- a/Nodule.cs +++ b/Nodule.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using Godot; -using GodotUtilities; using NodeWar.scripts; namespace NodeWar; @@ -12,7 +11,7 @@ public enum NoduleType Connector, } -[Scene] +[SceneTree] public partial class Nodule : Area2D { private const float GrowthFactor = 0.01f; @@ -20,6 +19,8 @@ public partial class Nodule : Area2D [Export] private NoduleType _noduleType; + private bool _growAreaInside; + [Export] public float MaxRadius { get; set; } = 360; @@ -32,7 +33,7 @@ public partial class Nodule : Area2D [Export] public PackedScene RootScene { get; set; } - public List ConnectedNodules { get; set; } = []; + public List ConnectedNodules { get; } = []; [Signal] public delegate void NoduleBuiltEventHandler(); @@ -40,23 +41,20 @@ public partial class Nodule : Area2D [Signal] public delegate void BuildingCanceledEventHandler(); - [Node] - private Node2D _gfx; - - [Node] - private CollisionShape2D _baseCollision; - public override void _Ready() { - AreaEntered += OnAreaEntered; - AreaExited += OnAreaExited; + _.ConnectionArea.NoduleEntered += HandleNoduleEntered; + _.ConnectionArea.NoduleExited += HandleNoduleExited; switch (_noduleType) { case NoduleType.Base: - _gfx.Scale = new Vector2(2, 2); Modulate = Colors.Red; - _baseCollision.Disabled = false; + + _.Gfx.Get().Scale = new Vector2(2, 2); + _.BaseCollision.Disabled = false; + + BuildNode(); break; case NoduleType.Connector: break; @@ -67,44 +65,15 @@ public partial class Nodule : Area2D QueueRedraw(); } - private void OnAreaEntered(Area2D area) - { - if (area is Nodule nodule) - { - if (nodule.BuildMode) - { - return; - } - - ConnectedNodules.Add(nodule); - } - } - - private void OnAreaExited(Area2D area) - { - if (area is Nodule nodule) - { - if (nodule.BuildMode) - { - return; - } - - ConnectedNodules.Remove(nodule); - } - } - public override void _Process(double delta) { if (BuildMode) { GlobalPosition = GetGlobalMousePosition(); - if (Input.IsMouseButtonPressed(MouseButton.Left)) + if (Input.IsActionJustPressed(MyInput.MouseLeft)) { - EmitSignalNoduleBuilt(); - BuildMode = false; - - MouseEntered += OnMouseEntered; + BuildNode(); } if (Input.IsMouseButtonPressed(MouseButton.Right)) @@ -116,6 +85,14 @@ public partial class Nodule : Area2D } else { + if (Input.IsActionJustPressed(MyInput.MouseLeft) && _growAreaInside && !BuildMode) + { + var newNodule = Instantiator.Instantiate(); + newNodule.BuildMode = true; + GetParent().AddChild(newNodule); + newNodule.GlobalPosition = GlobalPosition; + } + Age += GrowthFactor * delta.ToFloat(); Age = MathF.Min(Age, 1); @@ -126,11 +103,6 @@ public partial class Nodule : Area2D QueueRedraw(); } - private void OnMouseEntered() - { - GD.Print("Mouse entered"); - } - public override void _Draw() { foreach (var nodule in ConnectedNodules) @@ -159,11 +131,35 @@ public partial class Nodule : Area2D } } - public override void _Notification(int what) + private void HandleNoduleEntered(Nodule nodule) { - if (what == NotificationSceneInstantiated) + ConnectedNodules.Add(nodule); + } + + private void HandleNoduleExited(Nodule nodule) + { + ConnectedNodules.Remove(nodule); + } + + private void BuildNode() + { + EmitSignalNoduleBuilt(); + BuildMode = false; + + // try connecting built nodule to connected ones + foreach (var connectedNodule in ConnectedNodules) { - WireNodes(); + BuildManager.Instance.RegisterConnection(this, connectedNodule); } + + _.GrowArea.Get().MouseEntered += () => + { + _growAreaInside = true; + }; + + _.GrowArea.Get().MouseExited += () => + { + _growAreaInside = false; + }; } } diff --git a/NoduleConnection.cs b/NoduleConnection.cs index 2741a60..1fca3b2 100644 --- a/NoduleConnection.cs +++ b/NoduleConnection.cs @@ -7,13 +7,36 @@ public class NoduleConnection public Nodule A { get; set; } public Nodule B { get; set; } - protected bool Equals(NoduleConnection other) + public override bool Equals(object obj) { - return Equals(A, other.A) && Equals(B, other.B); + if (ReferenceEquals(null, obj)) + return false; + if (ReferenceEquals(this, obj)) + return true; + + return obj.GetType() == GetType() && Equals((NoduleConnection)obj); + } + + private bool Equals(NoduleConnection other) + { + return (Equals(A, other.A) && Equals(B, other.B)) + || (Equals(A, other.B) && Equals(B, other.A)); } public override int GetHashCode() { - return HashCode.Combine(A, B); + var h1 = A?.GetHashCode() ?? 0; + var h2 = B?.GetHashCode() ?? 0; + return h1 ^ h2; } -} \ No newline at end of file + + public static bool operator ==(NoduleConnection left, NoduleConnection right) + { + return Equals(left, right); + } + + public static bool operator !=(NoduleConnection left, NoduleConnection right) + { + return !Equals(left, right); + } +} diff --git a/Root.cs b/Root.cs index 0fb5193..6a5636e 100644 --- a/Root.cs +++ b/Root.cs @@ -1,28 +1,18 @@ -using System.Linq; using Godot; -using GodotSharp.SourceGenerators; using GodotUtilities; namespace NodeWar; -[Scene] +[SceneTree] public partial class Root : Node2D { - [Node] - private Line2D _line2D; - - private Nodule a; - private Nodule b; - - public override void _Ready() - { - GD.Print(_line2D); - } + private Nodule _a; + private Nodule _b; public void ConnectNodules(Nodule a, Nodule b) { - this.a = a; - this.b = b; + _a = a; + _b = b; GlobalPosition = a.GlobalPosition; GenerateLinePoints(); @@ -30,7 +20,7 @@ public partial class Root : Node2D private void GenerateLinePoints() { - var direction = b.GlobalPosition - a.GlobalPosition; + var direction = _b.GlobalPosition - _a.GlobalPosition; var sectionLength = direction.Length() / 5; var points = new Vector2[5]; @@ -47,14 +37,6 @@ public partial class Root : Node2D }; } - _line2D.Points = points; - } - - public override void _Notification(int what) - { - if (what == NotificationSceneInstantiated) - { - WireNodes(); - } + _.Line2D.Points = points; } } diff --git a/main.tscn b/main.tscn index a8ac60f..3d34188 100644 --- a/main.tscn +++ b/main.tscn @@ -1,13 +1,12 @@ [gd_scene format=3 uid="uid://bf7croj2rk4q6"] +[ext_resource type="Script" uid="uid://cmcnly8kwwjia" path="res://Main.cs" id="1_272bh"] [ext_resource type="Script" uid="uid://rirna2vebukw" path="res://addons/strategy_cam/strategy_camera.gd" id="2_0xm2m"] -[ext_resource type="Script" uid="uid://c2ufhsf60fl83" path="res://BuildManager.cs" id="2_5vw27"] [ext_resource type="Script" uid="uid://de3jpss66xjfh" path="res://addons/curved_lines_2d/scalable_vector_shape_2d.gd" id="3_lquwl"] [ext_resource type="Script" uid="uid://dlbv4pit17dnu" path="res://addons/curved_lines_2d/scalable_arc.gd" id="4_7mycd"] [ext_resource type="PackedScene" uid="uid://b1a13w4ckxnub" path="res://gui.tscn" id="4_272bh"] -[ext_resource type="PackedScene" uid="uid://cd47em5shcnid" path="res://root.tscn" id="4_kek77"] [ext_resource type="Script" uid="uid://dl1t88tthmwts" path="res://addons/curved_lines_2d/scalable_arc_list.gd" id="5_272bh"] -[ext_resource type="PackedScene" uid="uid://cb07jvtr8x7i1" path="res://nodule.tscn" id="8_5vw27"] +[ext_resource type="PackedScene" uid="uid://cb07jvtr8x7i1" path="res://Nodule.tscn" id="8_5vw27"] [sub_resource type="Curve2D" id="Curve2D_5vw27"] resource_local_to_scene = true @@ -21,11 +20,7 @@ resource_local_to_scene = true script = ExtResource("5_272bh") [node name="Main" type="Node2D" unique_id=866253780] - -[node name="BuildManager" type="Node" parent="." unique_id=288291270] -script = ExtResource("2_5vw27") -_noduleScene = ExtResource("8_5vw27") -_rootScene = ExtResource("4_kek77") +script = ExtResource("1_272bh") [node name="CanvasLayer" type="CanvasLayer" parent="." unique_id=1010925224] diff --git a/nodule.tscn b/nodule.tscn index 1798c29..1714cc3 100644 --- a/nodule.tscn +++ b/nodule.tscn @@ -2,13 +2,11 @@ [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="PackedScene" uid="uid://cd47em5shcnid" path="res://Root.tscn" id="2_k6tuy"] +[ext_resource type="PackedScene" uid="uid://c4oxc6kyp3muy" path="res://ConnectionArea.tscn" id="3_0cigu"] [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="RectangleShape2D" id="RectangleShape2D_bv6fd"] -size = Vector2(150, 150) - [sub_resource type="CircleShape2D" id="CircleShape2D_k6tuy"] radius = 140.0 @@ -23,23 +21,21 @@ point_count = 5 resource_local_to_scene = true script = ExtResource("4_qiyup") +[sub_resource type="CircleShape2D" id="CircleShape2D_8l2gl"] +radius = 18.027756 + [node name="Nodule" type="Area2D" unique_id=698496795] script = ExtResource("1_beybw") _noduleType = 1 Age = 0.5 RootScene = ExtResource("2_k6tuy") -[node name="Collision" type="CollisionShape2D" parent="." unique_id=402084582] -rotation = 0.7853982 -shape = SubResource("RectangleShape2D_bv6fd") - -[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="ConnectionArea" parent="." unique_id=943082837 instance=ExtResource("3_0cigu")] + [node name="Gfx" type="Node2D" parent="." unique_id=1962026750 node_paths=PackedStringArray("polygon", "line")] z_index = 10 script = ExtResource("2_cahmm") @@ -67,3 +63,9 @@ width = 4.0 default_color = Color(0, 0, 0, 1) sharp_limit = 90.0 metadata/_edit_lock_ = true + +[node name="GrowArea" type="Area2D" parent="." unique_id=1372859589] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="GrowArea" unique_id=270303533] +shape = SubResource("CircleShape2D_8l2gl") +debug_color = Color(0.92081034, 0.076709054, 0.6616314, 0.41960785) diff --git a/project.godot b/project.godot index cd136ae..117d92b 100644 --- a/project.godot +++ b/project.godot @@ -88,6 +88,11 @@ build={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":66,"key_label":0,"unicode":98,"location":0,"echo":false,"script":null) ] } +mouse_left={ +"deadzone": 0.2, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null) +] +} [physics] diff --git a/root.tscn b/root.tscn index 3c2f906..1d16222 100644 --- a/root.tscn +++ b/root.tscn @@ -12,3 +12,4 @@ script = ExtResource("1_pyidc") [node name="Line2D" type="Line2D" parent="." unique_id=388168093] 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") +antialiased = true