-
Notifications
You must be signed in to change notification settings - Fork 0
Core.GridSystem.GridAnalyzerYZ
The class is a utility designed for navigating and searching through a 2D grid structure. It treats the grid as a "YX" system, where the outer list represents rows (Y) and the inner list represents columns (X). GridAnalyzerYX<T>
Here is a breakdown of its primary responsibilities:
-
Data Structure: It wraps a , providing a safer and more convenient API than raw nested list indexing.
List<List<T>> - Validation: The constructor ensures the grid is rectangular (all rows have the same length) and contains data, preventing null or empty reference errors later.
-
Bounds Checking: It includes internal logic () to prevent when accessing coordinates.
InBounds``ArgumentOutOfRangeException
Unlike a simple lookup table, this class maintains an internal "cursor" via the field. _position
-
/ :
SetPosition``GetPositionAllows you to move the analyzer's focus to a specific cell. -
methods:
NeighborThese are used to "look" or "move" relative to the current position (Up, Down, Left, Right). One overload even updates the internal position automatically, allowing you to "walk" through the grid programmatically.
The class provides several ways to find data within the grid:
-
:
SearchFinds the first occurrence of a specific value. -
:
SearchAllUses C# iterators () to find every occurrence of a value.yield return -
:
IterateAllA helper to loop through every cell in the grid, returning both the coordinate and the value.
This is the most complex method in the class. It performs a search to find a sequence of adjacent cells that all share the same value.
- It starts at a given position and looks at the cardinal neighbors (Up, Down, Left, Right).
- If a neighbor matches the target and hasn't been visited yet, it "jumps" there and continues the search.
value - This is useful for logic like finding connected "blobs" of the same tile type or tracing a path in a puzzle game.
-
Generic ():
<T>It can handle any data type (integers, strings, or custom Tile objects). -
:
PointIntIt heavily relies on a type (from your namespace) to handle 2D coordinates as a single object.PointInt``Meatcorps.Engine.Core.Data -
Equality: It uses to ensure it correctly compares the generic type .
EqualityComparer<T>.Default``T
Summary: You would use this class if you have a 2D map or board and need to perform logic like "find the nearest empty space," "check what is to the left of the player," or "find all connected water tiles."
var analyzer = new GridAnalyzerYX<char>(levelData);
var allowedDirections = new List<PointInt>();
var allDirections = new PointInt[]
{ new PointInt(-1, 0), new PointInt(1, 0), new PointInt(0, -1), new PointInt(0, 1) };
foreach (var direction in allDirections)
{
var exist = analyzer.Neighbor(position, direction, out var neighbor);
if (((!exist) || neighbor != '#') && walkable)
allowedDirections.Add(direction);
}
var analyzer = new GridAnalyzerYX<char>(levelData);
foreach (var position in analyzer.NeighborEqualSearch('2', player2Position))
playerPositions.Add(position);