I am recreating a edge-link system from the game "Rimworld".
The purple squares are individual squares that need to form "links", in the direction the arrows are drawn in, with that exact length.
Currently, I don't know how to do this better than by placing all of those squares in a dictionary where I mark a square's position and its direction (which can be UP or RIGHT), like this:
Dictionary<Vector3Int, bool> linkSquares;
I want to sort this dictionary so that the KV pairs that come first are the ones where the Vector3Int.x is lowest and then where the Vector3Int.y is lowest.
That way, I could always start at those squares (marked with numbers 1-4 on the picture) and go in the appropriate arrow direction, removing those squares as I go, which would only leave other starting squares.
I've read that dictionaries can't be sorted, but I don't know how I would use a SortedDictionary for this case. This whole system seems overly dirty but I don't know how else I would achieve my goal here, so any design change suggestions are highly appreciated.
Try to define a custom class which includes the vector3 and bool together and then make list of objects from that class. Then you can sort the class in any you want.
If you only need the bool for filtering then you can just sort the dictionary into a list. An example of sorting dictionary into list is as below:
List<Vector3Int> productList = myDictionary.OrderBy(kp => kp.key.x).ThenBy(kp => kp.key.y)
.ToList();
you could use OrderBy for the first key and ThenBy for the second key (Linq) and put the result in new dictionary (sorted):
var dictsorted= linkSquares.OrderBy(k => k.Key.x).ThenBy(k => k.Key.y).ToDictionary(k => k.Key, k=> k.Value);