class Crack {
constructor() {
this.findStart();
this.sp = new SandPainter();
}
static makeCrack() {
if (cracks.length < maxnum) {
cracks.push(new Crack());
}
}
findStart() {
let px = 0, py = 0;
let found = false;
let timeout = 0;
while (!found || (timeout++ > 1000)) {
px = Math.floor(Math.random() * dimx);
py = Math.floor(Math.random() * dimy);
if (cgrid[py * dimx + px] < 10000) {
found = true;
}
}
if (found) {
let a = cgrid[py * dimx + px];
if (Math.random() < 0.5) {
a -= 90 + Math.floor(Math.random() * 4.1 - 2);
} else {
a += 90 + Math.floor(Math.random() * 4.1 - 2);
}
this.startCrack(px, py, a);
}
}
startCrack(X, Y, T) {
this.x = X;
this.y = Y;
this.t = T;
this.x += 0.61 * Math.cos(this.t * Math.PI / 180);
this.y += 0.61 * Math.sin(this.t * Math.PI / 180);
}
move() {
this.x += 0.42 * Math.cos(this.t * Math.PI / 180);
this.y += 0.42 * Math.sin(this.t * Math.PI / 180);
let z = 0.33;
let cx = Math.floor(this.x + Math.random() * 2 * z - z);
let cy = Math.floor(this.y + Math.random() * 2 * z - z);
this.regionColor();
context.globalAlpha = 0.85;
context.fillStyle = "#000";
context.fillRect(
this.x + Math.random() * 2 * z - z,
this.y + Math.random() * 2 * z - z,
1, 1
);
if ((cx >= 0) && (cx < dimx) && (cy >= 0) && (cy < dimy)) {
if ((cgrid[cy * dimx + cx] > 10000) || (Math.abs(cgrid[cy * dimx + cx] - this.t) < 5)) {
cgrid[cy * dimx + cx] = Math.floor(this.t);
} else if (Math.abs(cgrid[cy * dimx + cx] - this.t) > 2) {
this.findStart();
Crack.makeCrack();
}
} else {
this.findStart();
Crack.makeCrack();
}
}
regionColor() {
let rx = this.x;
let ry = this.y;
let openspace = true;
while (openspace) {
rx += 0.81 * Math.sin(this.t * Math.PI / 180);
ry -= 0.81 * Math.cos(this.t * Math.PI / 180);
let cx = Math.floor(rx);
let cy = Math.floor(ry);
if ((cx >= 0) && (cx < dimx) && (cy >= 0) && (cy < dimy)) {
if (cgrid[cy * dimx + cx] <= 10000) {
openspace = false;
}
} else {
openspace = false;
}
}
this.sp.render(rx, ry, this.x, this.y);
}
}