1
0

GridPointsNearEnumerator.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Diagnostics.Contracts;
  3. namespace Content.Server.Worldgen;
  4. /// <summary>
  5. /// A struct enumerator of points on a grid within the given radius.
  6. /// </summary>
  7. public struct GridPointsNearEnumerator
  8. {
  9. private readonly int _radius;
  10. private readonly Vector2i _center;
  11. private int _x;
  12. private int _y;
  13. /// <summary>
  14. /// Initializes a new enumerator with the given center and radius.
  15. /// </summary>
  16. public GridPointsNearEnumerator(Vector2i center, int radius)
  17. {
  18. _radius = radius;
  19. _center = center;
  20. _x = -_radius;
  21. _y = -_radius;
  22. }
  23. /// <summary>
  24. /// Gets the next point in the enumeration.
  25. /// </summary>
  26. /// <param name="chunk">The computed point, if any</param>
  27. /// <returns>Success</returns>
  28. [Pure]
  29. public bool MoveNext([NotNullWhen(true)] out Vector2i? chunk)
  30. {
  31. while (!(_x * _x + _y * _y <= _radius * _radius))
  32. {
  33. if (_y > _radius)
  34. {
  35. chunk = null;
  36. return false;
  37. }
  38. if (_x > _radius)
  39. {
  40. _x = -_radius;
  41. _y++;
  42. }
  43. else
  44. {
  45. _x++;
  46. }
  47. }
  48. chunk = _center + new Vector2i(_x, _y);
  49. _x++;
  50. return true;
  51. }
  52. }