How I would do this is have a boolean lookingForTarget which is set to false when the turret is shooting at some thing already. So something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class Turret {
private float radius = 15; private boolean lookingForTarget = true; private Guy target = null;
... public update(ArrayList<Guy> guys) {
if(lookingForTarget) { for(Guy g: guys) { if(distanceFromMeToAGuy(g) < raduis) { target = g; lookingForTarget = false; break; } }
if(!lookingForTarget) { attack(target); if(target.isDead()) { target = null; lookingForTarget = true; } } }
... }
} |
Okay so I see that while I was typing all this out others have replied, but I figured I'd post it anyway because whatever!
