You always want to use a minimum of CPU per game cycle, right? Looping through all entites, for all entities is therefore not an option and should be done differently. I can do that, most of the time. Please consider this situation:
You have a 2D landscape, in a tower-defence something (RotT for instance) where towers shoot at nearby enemies. How does each tower know, what enemy to target? I actually tried to look in the source code in RotT to find out what the solution used there, and it seems like they’re doing what I’d do (roughly):
Loop through all targetAble enemies (quickly returning an ArrayList of the type that is targetAble - data models is not my concern), and get the distance to them, using… hypotenuse of a triangle. Then just keeping in mind which one was the closest one, and bam - we have our answer.
But, Math can be expensive - if we have enough towers and enough enemies, we’re going to run out of CPU sometime, if we want to loop through things like that.
What can I do to optimize performance, in these situations? I know I can get a copy of Riven’s (awesome) fastMath, and I should also ONLY loop through enemies that actually COULD be targeted if they’re close enough. Anything else? Practises? Any collections better than others?
Smart ways of sorting collections?
Thanks for being awesome JGO! 8)