}
if (this.getCoolDownNow() > 0) {
this.setCoolDownNow(this.getCoolDownNow() - 1);
} else {
Creep c = this.getStrategy().findCreep();
if (c != null) {
Projectile p = new Projectile();
p.setTarget(c);
p.setX(this.getGrid().getLocation()[0] + Grid.SIZE / 2);
p.setY(this.getGrid().getLocation()[1] + Grid.SIZE / 2);
p.setSpeed(this.projectileSpeed);
this.projectiles.add(p);
this.setCoolDownNow(this.getCoolDown());
// play shoot-sound
if (sound != null) {
sound.towerShootsSound(this.getType());
}
}
}
for (int ip = this.projectiles.size() - 1; ip >= 0; ip--) {
Projectile p = this.projectiles.get(ip);
if (!p.getTarget().isValidTarget()) {
Creep cNew = this.getStrategy().findCreep();
if (cNew != null) {
p.setTarget(cNew);
} else {
p.setTarget(null);
this.projectiles.remove(ip);
}
} else {
float dx, dy, r, factor;
if (p.getStatus() == 0) {
dx = p.getTarget().getX() + Grid.SIZE / 2 - p.getX();
dy = p.getTarget().getY() + Grid.SIZE / 2 - p.getY();
r = (float) Math.sqrt(dx * dx + dy * dy);
factor = p.getSpeed() / (r / 1.5f);
if (r > 2) {
dx *= factor;
dy *= factor;
p.setSpeed(p.getSpeed() + 0.02f);
p.setX(p.getX() + dx);
p.setY(p.getY() + dy);
} else {
// HIT!
p.setX(p.getTarget().getX() + Grid.SIZE / 2);
p.setY(p.getTarget().getY() + Grid.SIZE / 2);
p.setStatus(1);
}
} else if (p.getStatus() == 1) {
float x = p.getX();
float y = p.getY();
Creep next;
for (int i = 0; i < getContext().getCreeps().size(); i++) {
if (getContext().getCreeps().get(i).isValidTarget()) {
next = getContext().getCreeps().get(i);
float dX = (next.getX() + Grid.SIZE / 2) - x;
float dY = (next.getY() + Grid.SIZE / 2) - y;
// squared distance
float dist = dX * dX + dY * dY;
if (dist < getSplashRadius() * getSplashRadius()) {
p.getSplashTargets().add(next);