xxxxxxxxxx
60
class CirclePackCircle {
constructor(x, y) {
this.x = x;
this.y = y;
this.radius = 1;
this.growing = true;
currentAmount++;
if (this.hitsCentre()) this.stopGrowing();
}
hitsCentre() {
let distance = dist(this.x, this.y, width/2, height/2);
if (distance < towerVisionSize*1.5) return true;
}
update() {
if (!this.growing) return;
if (currentAmount == towerTargetAmount) return;
for (let i = 0; i < circles.length; i++) {
if (circles[i] == this) continue;
if (this.overlap(circles[i])) {
this.x = (this.x+circles[i].x)/2;
this.y = (this.y+circles[i].y)/2;
circles[i].stopGrowing();
break;
}
}
this.radius += growRate;
}
overlap(otherCircle) {
if (!otherCircle.growing) return;
let distance = dist(this.x, this.y, otherCircle.x, otherCircle.y);
let radii = this.radius + otherCircle.radius;
if (distance < radii) return true;
}
stopGrowing() {
this.growing = false;
currentAmount--;
}
}