So my question sounds simple, and I’ve been brainstorming about this but can’t seem to find a proper solution that doesnt involve a lot of if statements.
I have two Vector2Int
that represent the input and exit point of my prefab in local space. So one is the start, which is a (0, 0)
. And the exit might be a (1,2)
. So from this data I know that I can loop on the X and Y coordinate of the vectors. But here is the problem, the exit might be (-1, 2)
.
I need to get every coordinate from my prefab as int that will also go in the negative. So I can check every coordinate and see if something else is placed there. But how would I loop through N where N is occasionally negative?
If I would use the old for loop and (int x = -1; x <= 0; x++)
then it would work ofcourse. But if my X happend to be 1
the <=
is already true.
For anyone wondering in the future. I ended up with this result, which I am not happy about because it is too chunky for my meaning. But it’ll do the trick.
public List<Vector2Int> AllLocalCoordinates()
{
int[] xArray, yArray;
if (PieceInfo.LocalExitPosition.x < 0)
{
xArray = Enumerable.Range(PieceInfo.LocalExitPosition.x, Mathf.Abs(PieceInfo.LocalExitPosition.x) + 1).ToArray();
}
else
{
xArray = Enumerable.Range(0, PieceInfo.LocalExitPosition.x).ToArray();
}
if (PieceInfo.LocalExitPosition.y < 0)
{
yArray = Enumerable.Range(PieceInfo.LocalExitPosition.y, Mathf.Abs(PieceInfo.LocalExitPosition.y) + 1).ToArray();
}
else
{
yArray = Enumerable.Range(0, PieceInfo.LocalExitPosition.y).ToArray();
}
List<Vector2Int> list = new List<Vector2Int>();
foreach (int x in xArray)
{
foreach (int y in yArray)
{
list.Add(new Vector2Int(x, y));
}
}
return list;
}