a bit more stuff
This commit is contained in:
@@ -5,7 +5,7 @@ namespace NodeWar;
|
||||
|
||||
public partial class BuildManager : Node
|
||||
{
|
||||
private bool Building { get; set; }
|
||||
public bool IsBuilding { get; set; }
|
||||
|
||||
private Nodule BuildingNodule { get; set; }
|
||||
|
||||
@@ -20,7 +20,7 @@ public partial class BuildManager : Node
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (Input.IsActionJustPressed(Scripts.Globals.MyInput.Build) && !Building)
|
||||
if (Input.IsActionJustPressed(Scripts.Globals.MyInput.Build) && !IsBuilding)
|
||||
{
|
||||
StartBuilding();
|
||||
}
|
||||
@@ -30,18 +30,18 @@ public partial class BuildManager : Node
|
||||
{
|
||||
BuildingNodule = Instantiator.Instantiate<Nodule>();
|
||||
|
||||
Building = true;
|
||||
IsBuilding = true;
|
||||
BuildingNodule.BuildMode = true;
|
||||
|
||||
BuildingNodule.NoduleBuilt += () =>
|
||||
{
|
||||
Building = false;
|
||||
IsBuilding = false;
|
||||
BuildingNodule = null;
|
||||
};
|
||||
|
||||
BuildingNodule.BuildingCanceled += () =>
|
||||
{
|
||||
Building = false;
|
||||
IsBuilding = false;
|
||||
BuildingNodule = null;
|
||||
};
|
||||
|
||||
|
||||
@@ -24,9 +24,6 @@ public partial class ConnectionArea : Area2D
|
||||
case Nodule nodule:
|
||||
HandleNoduleEntered(nodule);
|
||||
break;
|
||||
case ConnectionArea connectionArea:
|
||||
HandleNoduleEntered(connectionArea.GetParent() as Nodule);
|
||||
break;
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -49,9 +46,6 @@ public partial class ConnectionArea : Area2D
|
||||
case Nodule nodule:
|
||||
HandleNoduleExited(nodule);
|
||||
break;
|
||||
case ConnectionArea connectionArea:
|
||||
HandleNoduleExited(connectionArea.GetParent() as Nodule);
|
||||
break;
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -66,4 +60,9 @@ public partial class ConnectionArea : Area2D
|
||||
EmitSignalNoduleExited(newNodule);
|
||||
}
|
||||
}
|
||||
|
||||
public float GetRadius()
|
||||
{
|
||||
return ((CircleShape2D)_.CollisionShape2D.Shape).Radius;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,11 @@
|
||||
|
||||
[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)
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_djo5v"]
|
||||
radius = 300.0
|
||||
|
||||
[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")
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=804584423]
|
||||
shape = SubResource("CircleShape2D_djo5v")
|
||||
|
||||
28
EnergyProducer.cs
Normal file
28
EnergyProducer.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Godot;
|
||||
using NodeWar.scripts;
|
||||
using NodeWar.Scripts;
|
||||
|
||||
namespace NodeWar;
|
||||
|
||||
public partial class EnergyProducer : Node
|
||||
{
|
||||
[Export]
|
||||
public float EnergyCapacity = 100;
|
||||
|
||||
[Export]
|
||||
public float EnergyStored = 0;
|
||||
|
||||
[Export]
|
||||
public float EnergyGenerationRate = 1.0f;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
EnergyManager.Instance.EnergyProducers.Add(this);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
EnergyStored += EnergyGenerationRate * delta.ToFloat();
|
||||
EnergyStored = Mathf.Clamp(EnergyStored, 0, EnergyCapacity);
|
||||
}
|
||||
}
|
||||
1
EnergyProducer.cs.uid
Normal file
1
EnergyProducer.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://labc6hameh2q
|
||||
6
EnergyProducer.tscn
Normal file
6
EnergyProducer.tscn
Normal file
@@ -0,0 +1,6 @@
|
||||
[gd_scene format=3 uid="uid://b0vhqymerr5mx"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://labc6hameh2q" path="res://EnergyProducer.cs" id="1_vpkbi"]
|
||||
|
||||
[node name="EnergyProducer" type="Node" unique_id=30257502]
|
||||
script = ExtResource("1_vpkbi")
|
||||
95
Nodule.cs
95
Nodule.cs
@@ -16,13 +16,15 @@ public partial class Nodule : Area2D
|
||||
{
|
||||
private const float GrowthFactor = 0.01f;
|
||||
|
||||
private bool _growAreaInside;
|
||||
private bool _holdInsideGrowArea;
|
||||
private Vector2 _lastPosition;
|
||||
private bool _freezeMove;
|
||||
|
||||
[Export]
|
||||
private NoduleType _noduleType;
|
||||
|
||||
private bool _growAreaInside;
|
||||
|
||||
[Export]
|
||||
public float MaxRadius { get; set; } = 360;
|
||||
private float _maxRadius;
|
||||
|
||||
[Export]
|
||||
public bool BuildMode { get; set; } = false;
|
||||
@@ -35,6 +37,10 @@ public partial class Nodule : Area2D
|
||||
|
||||
public List<Nodule> ConnectedNodules { get; } = [];
|
||||
|
||||
public Nodule ParentNoduleWhileBuilding;
|
||||
|
||||
public Nodule ChildNoduleWhileBuilding;
|
||||
|
||||
[Signal]
|
||||
public delegate void NoduleBuiltEventHandler();
|
||||
|
||||
@@ -43,6 +49,7 @@ public partial class Nodule : Area2D
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_maxRadius = _.ConnectionArea.GetRadius();
|
||||
_.ConnectionArea.NoduleEntered += HandleNoduleEntered;
|
||||
_.ConnectionArea.NoduleExited += HandleNoduleExited;
|
||||
|
||||
@@ -52,7 +59,6 @@ public partial class Nodule : Area2D
|
||||
Modulate = Colors.Red;
|
||||
|
||||
_.Gfx.Get().Scale = new Vector2(2, 2);
|
||||
_.BaseCollision.Disabled = false;
|
||||
|
||||
BuildNode();
|
||||
break;
|
||||
@@ -67,11 +73,24 @@ public partial class Nodule : Area2D
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
// TODO: StateMachine?
|
||||
if (BuildMode)
|
||||
{
|
||||
GlobalPosition = GetGlobalMousePosition();
|
||||
var distanceToParent =
|
||||
GetGlobalMousePosition() - ParentNoduleWhileBuilding.GlobalPosition;
|
||||
|
||||
if (Input.IsActionJustPressed(Scripts.Globals.MyInput.MouseLeft))
|
||||
if (distanceToParent.Length() > _maxRadius)
|
||||
{
|
||||
GlobalPosition =
|
||||
ParentNoduleWhileBuilding.GlobalPosition
|
||||
+ distanceToParent.Normalized() * _maxRadius;
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalPosition = GetGlobalMousePosition();
|
||||
}
|
||||
|
||||
if (Input.IsActionJustReleased(Scripts.Globals.MyInput.MouseLeft))
|
||||
{
|
||||
BuildNode();
|
||||
}
|
||||
@@ -80,21 +99,21 @@ public partial class Nodule : Area2D
|
||||
{
|
||||
EmitSignalBuildingCanceled();
|
||||
BuildMode = false;
|
||||
BuildManager.Instance.IsBuilding = false;
|
||||
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (
|
||||
Input.IsActionJustPressed(Scripts.Globals.MyInput.MouseLeft)
|
||||
&& _growAreaInside
|
||||
&& !BuildMode
|
||||
)
|
||||
if (Input.IsActionJustPressed(Scripts.Globals.MyInput.MouseLeft) && _growAreaInside)
|
||||
{
|
||||
var newNodule = Instantiator.Instantiate<Nodule>();
|
||||
newNodule.BuildMode = true;
|
||||
GetParent().AddChild(newNodule);
|
||||
newNodule.GlobalPosition = GlobalPosition;
|
||||
_holdInsideGrowArea = true;
|
||||
}
|
||||
|
||||
if (Input.IsActionJustReleased(Scripts.Globals.MyInput.MouseLeft) && _growAreaInside)
|
||||
{
|
||||
_holdInsideGrowArea = false;
|
||||
}
|
||||
|
||||
Age += GrowthFactor * delta.ToFloat();
|
||||
@@ -105,13 +124,15 @@ public partial class Nodule : Area2D
|
||||
|
||||
// TODO: replace with line2d?
|
||||
QueueRedraw();
|
||||
|
||||
_lastPosition = GlobalPosition;
|
||||
}
|
||||
|
||||
public override void _Draw()
|
||||
{
|
||||
foreach (var nodule in ConnectedNodules)
|
||||
{
|
||||
if (BuildMode)
|
||||
{
|
||||
foreach (var nodule in ConnectedNodules)
|
||||
{
|
||||
DrawDashedLine(
|
||||
Vector2.Zero,
|
||||
@@ -123,16 +144,6 @@ public partial class Nodule : Area2D
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
var value = 200 * Age;
|
||||
|
||||
if (!BuildMode)
|
||||
{
|
||||
DrawRect(
|
||||
new Rect2(-value / 2, -value / 2, value, value),
|
||||
new Color(Colors.WhiteSmoke, 0.4f)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleNoduleEntered(Nodule nodule)
|
||||
@@ -148,7 +159,12 @@ public partial class Nodule : Area2D
|
||||
private void BuildNode()
|
||||
{
|
||||
EmitSignalNoduleBuilt();
|
||||
|
||||
BuildMode = false;
|
||||
ChildNoduleWhileBuilding = null;
|
||||
|
||||
// Weird architecture...
|
||||
BuildManager.Instance.IsBuilding = false;
|
||||
|
||||
// try connecting built nodule to connected ones
|
||||
foreach (var connectedNodule in ConnectedNodules)
|
||||
@@ -163,7 +179,30 @@ public partial class Nodule : Area2D
|
||||
|
||||
_.GrowArea.Get().MouseExited += () =>
|
||||
{
|
||||
if (_holdInsideGrowArea)
|
||||
{
|
||||
StartBuilding();
|
||||
_holdInsideGrowArea = false;
|
||||
}
|
||||
|
||||
_growAreaInside = false;
|
||||
};
|
||||
}
|
||||
|
||||
private void StartBuilding()
|
||||
{
|
||||
GD.Print("START BUILDING");
|
||||
if (BuildManager.Instance.IsBuilding)
|
||||
return;
|
||||
|
||||
BuildManager.Instance.IsBuilding = true;
|
||||
var newNodule = Instantiator.Instantiate<Nodule>();
|
||||
|
||||
GetParent().AddChild(newNodule);
|
||||
|
||||
newNodule.BuildMode = true;
|
||||
newNodule.GlobalPosition = GlobalPosition;
|
||||
newNodule.ParentNoduleWhileBuilding = this;
|
||||
ChildNoduleWhileBuilding = newNodule;
|
||||
}
|
||||
}
|
||||
|
||||
29
Scripts/EnergyManager.cs
Normal file
29
Scripts/EnergyManager.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
|
||||
namespace NodeWar.Scripts;
|
||||
|
||||
[Singleton]
|
||||
public partial class EnergyManager
|
||||
{
|
||||
public List<EnergyProducer> EnergyProducers { get; set; } = []; // TODO: privat set
|
||||
|
||||
public float GetTotalEnergyCapacity()
|
||||
{
|
||||
// TODO: cache + dirty flag
|
||||
return EnergyProducers.Sum(energyProducer => energyProducer.EnergyCapacity);
|
||||
}
|
||||
|
||||
public float GetEnergyGeneration()
|
||||
{
|
||||
// TODO: cache + dirty flag
|
||||
return EnergyProducers.Sum(energyProducer => energyProducer.EnergyGenerationRate);
|
||||
}
|
||||
|
||||
public float GetEnergyStored()
|
||||
{
|
||||
// TODO: cache + dirty flag
|
||||
return EnergyProducers.Sum(energyProducer => energyProducer.EnergyStored);
|
||||
}
|
||||
}
|
||||
1
Scripts/EnergyManager.cs.uid
Normal file
1
Scripts/EnergyManager.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://lmswu03qs5f3
|
||||
@@ -1,39 +1,34 @@
|
||||
using System.Globalization;
|
||||
using Godot;
|
||||
using GodotUtilities;
|
||||
using NodeWar.Scripts.Globals;
|
||||
|
||||
namespace NodeWar.Scripts.Gui;
|
||||
|
||||
[Scene]
|
||||
[SceneTree]
|
||||
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()
|
||||
{
|
||||
_energyGeneration.Text = 1f.ToString(CultureInfo.InvariantCulture);
|
||||
_energyConsumption.Text = 0f.ToString(CultureInfo.InvariantCulture);
|
||||
_energyStorage.Text = 100f.ToString(CultureInfo.InvariantCulture);
|
||||
base._Ready();
|
||||
|
||||
Autoloads.EventBus.SelectionChanged += text => _infoLabel.Text = text;
|
||||
EnergyGeneration.Text = 1f.ToString(CultureInfo.InvariantCulture);
|
||||
EnergyConsumption.Text = 0f.ToString(CultureInfo.InvariantCulture);
|
||||
EnergyCapacity.Text = 100f.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
Autoloads.EventBus.SelectionChanged += text => _InfoLabel.Text = text;
|
||||
}
|
||||
|
||||
public override void _Notification(int what)
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (what == NotificationSceneInstantiated)
|
||||
{
|
||||
WireNodes();
|
||||
}
|
||||
EnergyGeneration.Text = EnergyManager
|
||||
.Instance.GetEnergyGeneration()
|
||||
.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
EnergyCapacity.Text = EnergyManager
|
||||
.Instance.GetTotalEnergyCapacity()
|
||||
.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
EnergyProgressBar.MaxValue = EnergyManager.Instance.GetTotalEnergyCapacity();
|
||||
EnergyProgressBar.Value = EnergyManager.Instance.GetEnergyStored();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,10 +64,9 @@ layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
size_flags_vertical = 4
|
||||
theme_override_styles/fill = SubResource("StyleBoxFlat_80edf")
|
||||
value = 37.15
|
||||
show_percentage = false
|
||||
value = 36.59
|
||||
|
||||
[node name="EnergyStorage" type="Label" parent="Top/MarginContainer/HBoxContainer" unique_id=1992827443]
|
||||
[node name="EnergyCapacity" type="Label" parent="Top/MarginContainer/HBoxContainer" unique_id=1992827443]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(80, 0)
|
||||
layout_mode = 2
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
[ext_resource type="Script" uid="uid://dl1t88tthmwts" path="res://addons/curved_lines_2d/scalable_arc_list.gd" id="6_c6i3y"]
|
||||
[ext_resource type="Script" uid="uid://rirna2vebukw" path="res://addons/strategy_cam/strategy_camera.gd" id="7_c2ibq"]
|
||||
[ext_resource type="PackedScene" uid="uid://cb07jvtr8x7i1" path="res://Nodule.tscn" id="8_j4qnp"]
|
||||
[ext_resource type="PackedScene" uid="uid://b0vhqymerr5mx" path="res://EnergyProducer.tscn" id="9_03owx"]
|
||||
|
||||
[sub_resource type="Curve2D" id="Curve2D_5vw27"]
|
||||
resource_local_to_scene = true
|
||||
@@ -56,10 +57,13 @@ polygon = PackedVector2Array(-13.812602, -13.812602, 13.812602, -13.812602, 13.8
|
||||
metadata/_edit_lock_ = true
|
||||
|
||||
[node name="StrategyCamera" type="Camera2D" parent="." unique_id=706618209]
|
||||
zoom = Vector2(0.205, 0.205)
|
||||
limit_enabled = false
|
||||
script = ExtResource("7_c2ibq")
|
||||
translation_speed = 300.0
|
||||
metadata/_custom_type_script = "uid://rirna2vebukw"
|
||||
|
||||
[node name="Nodule" parent="." unique_id=698496795 instance=ExtResource("8_j4qnp")]
|
||||
[node name="Base" parent="." unique_id=698496795 instance=ExtResource("8_j4qnp")]
|
||||
_noduleType = 0
|
||||
|
||||
[node name="EnergyProducer" parent="Base" unique_id=30257502 instance=ExtResource("9_03owx")]
|
||||
|
||||
11
nodule.tscn
11
nodule.tscn
@@ -7,8 +7,8 @@
|
||||
[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_k6tuy"]
|
||||
radius = 140.0
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_0cigu"]
|
||||
radius = 12.0
|
||||
|
||||
[sub_resource type="Curve2D" id="Curve2D_272bh"]
|
||||
resource_local_to_scene = true
|
||||
@@ -24,15 +24,14 @@ script = ExtResource("4_qiyup")
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_8l2gl"]
|
||||
radius = 18.027756
|
||||
|
||||
[node name="Nodule" type="Area2D" unique_id=698496795]
|
||||
[node name="Nodule" type="Area2D" unique_id=540162896]
|
||||
script = ExtResource("1_beybw")
|
||||
_noduleType = 1
|
||||
Age = 0.5
|
||||
RootScene = ExtResource("2_k6tuy")
|
||||
|
||||
[node name="BaseCollision" type="CollisionShape2D" parent="." unique_id=1607878878]
|
||||
shape = SubResource("CircleShape2D_k6tuy")
|
||||
disabled = true
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1698228409]
|
||||
shape = SubResource("CircleShape2D_0cigu")
|
||||
|
||||
[node name="ConnectionArea" parent="." unique_id=943082837 instance=ExtResource("3_0cigu")]
|
||||
|
||||
|
||||
@@ -32,6 +32,10 @@ EventBus="*uid://bibq3e4nkwiwv"
|
||||
|
||||
project/assembly_name="Node War"
|
||||
|
||||
[editor]
|
||||
|
||||
naming/scene_name_casing=1
|
||||
|
||||
[editor_plugins]
|
||||
|
||||
enabled=PackedStringArray("res://addons/curved_lines_2d/plugin.cfg", "res://addons/strategy_cam/plugin.cfg")
|
||||
|
||||
Reference in New Issue
Block a user