Hackswell [SIGS3GV]
Hackswell [SIGS3GV]
CC#
Created by Hackswell [SIGS3GV] on 4/25/2024 in #help
Modernizing double loops in C#?
In C#, concerning an IEnumerable, is there a better way to handle searching? The code is currently using a double for loop to search in a "radius" around the character.
for (int dx = -radius; dx <= radius; dx++)
{
for (int dy = -radius; dy <= radius; dy++)
{
Vector2 loc = currentLocation + new Vector2(dx, dy);
if (location.Objects.ContainsKey(loc) && location.Objects[loc] is T t)
{
list.Add(t);
}
}
}
for (int dx = -radius; dx <= radius; dx++)
{
for (int dy = -radius; dy <= radius; dy++)
{
Vector2 loc = currentLocation + new Vector2(dx, dy);
if (location.Objects.ContainsKey(loc) && location.Objects[loc] is T t)
{
list.Add(t);
}
}
}
I was thinking something along the lines of: location.Objects is a SerializableDictionary<Vector2, object> Is it possible to use a Where()/Intersects()/Any()/other()? Maybe by testing overlap with a Rectangle? Rectangle bubba = new Rectangle((int)location.X, (int)location.Y, radius, radius); IEnumerable<T> shortlist = location.Objects.Where(?????);
18 replies