It's usually a Bad Idea™ in game programming when you find yourself taking the solution of "every time I do this, I'll iterate over all objects and check if they're active..." I say this as someone who once upon a time made this mistake on a 100000x100000 grid. The best solution to problems of this type is usually inverting the question. Instead of asking each waypoint whether it's active, each waypoint should ask whether it belongs to the active or inactive set. I'd recommend having at least two lists: activeWaypoints
and inactiveWaypoints
. If you need it (and sometimes you do), masterWaypoints
can be used as a universal set.
Now it's simple: each waypoint knows whether it's active or inactive, when it activates it removes itself from the inactive list and adds itself to the active list and vice versa. When it's created, you have to choose whether it's active or inactive by default (or provide an option to create it with either default), but that's a minor design issue at best. When it's destroyed, just remove it from whichever it currently is and masterWaypoints
if it exists.
The benefit to this is that you only have to update things when they change state, rather than check them every frame (or worse: for every NPC agent every frame).
From there, well, now all you have to do with each customer is check activeWaypoints
. If you need further filtering for each customer, that gets a little more difficult. In that case it may be acceptable to iterate over all activeWaypoints
and just ignore all waypoints that don't meet the criteria. Though depending on how much filtering needs to be done, I'd probably consider writing some sort of WaypointManager
with its own logic for filtering based on active/inactive/red/blue/made-of-marmalade/insanity-inducing/etc that's accessed via getWaypointsMeetingCriteria(list of filters)
. This can use a hybrid of maps, lists, and filtering via iteration depending on what you decide for each tag.
There is a snag here in that the waypoints are, presumably, ordered, which means that when waypoints move from inactive or active they need to have their order maintained, but there are plenty of methods for quickly inserting into and/or constructing a sorted list.