mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Move chunk enumerators to engine (#4901)
* Move chunk enumerators to engine * notes * Cleanup
This commit is contained in:
@@ -35,11 +35,11 @@ END TEMPLATE-->
|
||||
|
||||
### Breaking changes
|
||||
|
||||
*None yet*
|
||||
* Moved ChunkIndicesEnumerator to engine and to a re-useable namespace at Robust.Shared/Maps.
|
||||
|
||||
### New features
|
||||
|
||||
*None yet*
|
||||
* Added an Enlarged method for Box2Rotated.
|
||||
|
||||
### Bugfixes
|
||||
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Numerics;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
namespace Robust.Server.GameStates;
|
||||
|
||||
public struct ChunkIndicesEnumerator
|
||||
{
|
||||
private Vector2i _bottomLeft;
|
||||
private Vector2i _topRight;
|
||||
|
||||
private int _x;
|
||||
private int _y;
|
||||
|
||||
public ChunkIndicesEnumerator(Vector2 viewPos, float range, float chunkSize)
|
||||
{
|
||||
var rangeVec = new Vector2(range, range);
|
||||
|
||||
_bottomLeft = ((viewPos - rangeVec) / chunkSize).Floored();
|
||||
// Also floor this as we get the whole chunk anyway.
|
||||
_topRight = ((viewPos + rangeVec) / chunkSize).Floored();
|
||||
|
||||
_x = _bottomLeft.X;
|
||||
_y = _bottomLeft.Y;
|
||||
}
|
||||
|
||||
public bool MoveNext([NotNullWhen(true)] out Vector2i? chunkIndices)
|
||||
{
|
||||
if (_y > _topRight.Y)
|
||||
{
|
||||
_x++;
|
||||
_y = _bottomLeft.Y;
|
||||
}
|
||||
|
||||
if (_x > _topRight.X)
|
||||
{
|
||||
chunkIndices = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
chunkIndices = new Vector2i(_x, _y);
|
||||
|
||||
_y++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using Prometheus;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Map.Enumerators;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Robust.Shared.Maths
|
||||
@@ -57,7 +58,17 @@ namespace Robust.Shared.Maths
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// calculates the smallest AABB that will encompass the rotated box. The AABB is in local space.
|
||||
/// Enlarges the box by the specified value.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public readonly Box2Rotated Enlarged(float value)
|
||||
{
|
||||
var box = Box.Enlarged(value);
|
||||
return new Box2Rotated(box, Rotation, Origin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the smallest AABB that will encompass the rotated box. The AABB is in local space.
|
||||
/// </summary>
|
||||
public readonly Box2 CalcBoundingBox()
|
||||
{
|
||||
|
||||
@@ -9,7 +9,10 @@ namespace Robust.Shared.Map.Enumerators;
|
||||
|
||||
internal struct ChunkEnumerator
|
||||
{
|
||||
public static ChunkEnumerator Empty => new(new Dictionary<Vector2i, MapChunk>(), Box2.Empty, 16);
|
||||
/// <summary>
|
||||
/// An empty enumerator that will return nothing.
|
||||
/// </summary>
|
||||
public static ChunkEnumerator Empty => new(new Dictionary<Vector2i, MapChunk>(), Box2.Empty, 1);
|
||||
|
||||
private Dictionary<Vector2i, MapChunk> _chunks;
|
||||
private Vector2i _chunkLB;
|
||||
|
||||
59
Robust.Shared/Map/Enumerators/ChunkIndicesEnumerator.cs
Normal file
59
Robust.Shared/Map/Enumerators/ChunkIndicesEnumerator.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Numerics;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
namespace Robust.Shared.Map.Enumerators;
|
||||
|
||||
/// <summary>
|
||||
/// Generic iterator for chunk indices for the specified bounds with the specified chunk size.
|
||||
/// </summary>
|
||||
public struct ChunkIndicesEnumerator
|
||||
{
|
||||
private readonly Vector2i _chunkLB;
|
||||
private readonly Vector2i _chunkRT;
|
||||
|
||||
private int _xIndex;
|
||||
private int _yIndex;
|
||||
|
||||
public ChunkIndicesEnumerator(Vector2 viewPos, float range, float chunkSize)
|
||||
{
|
||||
var rangeVec = new Vector2(range, range);
|
||||
|
||||
_chunkLB = ((viewPos - rangeVec) / chunkSize).Floored();
|
||||
// Also floor this as we get the whole chunk anyway.
|
||||
_chunkRT = ((viewPos + rangeVec) / chunkSize).Floored();
|
||||
|
||||
_xIndex = _chunkLB.X;
|
||||
_yIndex = _chunkLB.Y;
|
||||
}
|
||||
|
||||
public ChunkIndicesEnumerator(Box2 localAABB, int chunkSize)
|
||||
{
|
||||
_chunkLB = (localAABB.BottomLeft / chunkSize).Floored();
|
||||
_chunkRT = (localAABB.TopRight / chunkSize).Floored();
|
||||
|
||||
_xIndex = _chunkLB.X;
|
||||
_yIndex = _chunkLB.Y;
|
||||
}
|
||||
|
||||
public bool MoveNext([NotNullWhen(true)] out Vector2i? indices)
|
||||
{
|
||||
if (_yIndex > _chunkRT.Y)
|
||||
{
|
||||
_yIndex = _chunkLB.Y;
|
||||
_xIndex++;
|
||||
}
|
||||
|
||||
if (_xIndex > _chunkRT.X)
|
||||
{
|
||||
indices = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
indices = new Vector2i(_xIndex, _yIndex);
|
||||
_yIndex++;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user