a bit more stuff
This commit is contained in:
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user