using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
namespace Content.Server.Worldgen;
///
/// A struct enumerator of points on a grid within the given radius.
///
public struct GridPointsNearEnumerator
{
private readonly int _radius;
private readonly Vector2i _center;
private int _x;
private int _y;
///
/// Initializes a new enumerator with the given center and radius.
///
public GridPointsNearEnumerator(Vector2i center, int radius)
{
_radius = radius;
_center = center;
_x = -_radius;
_y = -_radius;
}
///
/// Gets the next point in the enumeration.
///
/// The computed point, if any
/// Success
[Pure]
public bool MoveNext([NotNullWhen(true)] out Vector2i? chunk)
{
while (!(_x * _x + _y * _y <= _radius * _radius))
{
if (_y > _radius)
{
chunk = null;
return false;
}
if (_x > _radius)
{
_x = -_radius;
_y++;
}
else
{
_x++;
}
}
chunk = _center + new Vector2i(_x, _y);
_x++;
return true;
}
}