namespace SpaceGame.Api.Shared.Runtime;

public readonly record struct Vector3(float X, float Y, float Z)
{
    public static Vector3 Zero => new(0f, 0f, 0f);

    public float DistanceTo(Vector3 other)
    {
        var dx = X - other.X;
        var dy = Y - other.Y;
        var dz = Z - other.Z;
        return MathF.Sqrt((dx * dx) + (dy * dy) + (dz * dz));
    }

    public float LengthSquared() => (X * X) + (Y * Y) + (Z * Z);

    public Vector3 MoveToward(Vector3 target, float maxDistance)
    {
        var delta = target.Subtract(this);
        var distance = MathF.Sqrt(delta.LengthSquared());
        if (distance <= 0.0001f || distance <= maxDistance)
        {
            return target;
        }

        var scale = maxDistance / distance;
        return new Vector3(
            X + (delta.X * scale),
            Y + (delta.Y * scale),
            Z + (delta.Z * scale));
    }

    public Vector3 Subtract(Vector3 other) => new(X - other.X, Y - other.Y, Z - other.Z);

    public Vector3 Divide(float scalar) => MathF.Abs(scalar) <= 0.0001f ? Zero : new Vector3(X / scalar, Y / scalar, Z / scalar);
}
