I went with smth a bit different, because nested for loops make my eyes bleed
public List<GridPosition> GetValidActionGridPositionList() {
GridPosition unitGridPosition = _targetUnit.GetGridPosition();
bool IsValidPosition(GridPosition position) {
LevelGrid levelGrid = LevelGrid.Instance;
return levelGrid.IsValidGridPosition(position) &&
!levelGrid.HasAnyUnitOnGridPosition(position) &&
unitGridPosition != position;
}
int startIndex = -_maxMoveDistance;
int countPositionsToGenerate = _maxMoveDistance * 2 + 1;
IEnumerable<int> valueRange = Enumerable.Range(startIndex, countPositionsToGenerate);
return valueRange
.SelectMany(x => valueRange.Select(
y => new GridPosition(x, y) + unitGridPosition
)
).ToList()
.FindAll(IsValidPosition);
}