using Godot;
using GodotUtilities;

namespace NodeWar;

[SceneTree]
public partial class Root : Node2D
{
	private Nodule _a;
	private Nodule _b;

	public void ConnectNodules(Nodule a, Nodule b)
	{
		_a = a;
		_b = 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;
	}
}
