Tracking all targets within a radius

So I’ve got a turret and multiple enemies and I’d like to keep track of all enemies within the firing range. I’ve considered a couple ways to do this, one would be to just calculate the distance from each turret to each enemy at every step. Put that in an array for each turret I guess, then sort it. Seems a little expensive though, and maybe over complicated.

What I thought I could do instead was just attach a sensor object to each turret and then listen for a collision with the sensor. That’s fine, but I’m running into a problem: collisions only happen twice, once when the enemy enters the collision zone and once when it leaves. There doesn’t seem to be any way to track continuous collisions (no collisions occur inside the zone) and the collision on entering is the same as the collision on leaving (postCollision doesn’t fire at all in this case). So I could do something like add an enemy to the target array on the first collision (when they enter the zone) and remove them on the second collision (when they exit). Then calculate distances to each enemy in the target array, then sort them, etc.

The problem that I see with this method is basically just a matter of unpredictability - suppose an enemy spawned inside the zone, or suppose the enemy was killed and removed by another turret before they exited. There are a lot of potential situations like that which might result in odd behavior and the only resolution I see is be very careful and cross my fingers and hope that I don’t miss anything when I’m implementing my fixes for these problems.

Is there a better way to do this? What I’d really like is a sensor object that fires every tick for every object within range.

Side question: is there a good way to deal with line-of-sight in this scenario? It seems like a related problem. [import]uid: 106115 topic_id: 28401 reply_id: 328401[/import]

I’ve been there - though it was awhile ago now. I believe I handled it by putting the enemy into a table tied to the sensor when it entered the area and removing it when it left OR was killed. Naturally you need to take into account that there can be more than one sensor at a time and perform more than one check.

As to spawning inside the zone, I believe that should trigger a began phase, in which case that is no different to an enemy wandering into a sensor. (Correct me if I am mistaken, it has been months since I’ve played with my own defense style stuff - I always found it to be one of the more challenging things a person can play with.)

Peach :slight_smile: [import]uid: 52491 topic_id: 28401 reply_id: 114659[/import]