My case is: I have several vehicles moving in formation and when they arrived they must go under trees to take cover. If there are more vehicles than trees, I must send more the one vehicle for some trees. The control how many vehicles I have already sent to each tree at each loop I tough of two options:
Have a Dictionary<Transform, int> where the Transform is the tree and the in is how many vehicles have been sent to take cover on that tree.
Have a List where MyClass would contain a Transform (the tree) and an int (the amount of vehicles sent to the tree).
What would be the best option? Why? Does anyone have a better suggestion?
If you don’t ever relist trees and you look up your vehicles by tree, then I would use the trees as a key for the Dictionary. It will be much faster than running a for-loop (aka Contains) through the List to find your tree.
for each vehicle: search best cover
add one vehicle to the cover (add one to the int in the dictionary entrance for the cover or add the int on myclass).
The best cover is the closest with the less cover currently.
In the end, I will be doing one foreach inside other (one for every vehicle, one for every cover). So I don’t know what would be better. The dictionary is simpler to implement (no extra class), but it seems strange to use dictionary like that.