class Hatrack {
constructor(context, scale, width) {
this.hats = [];
this.context = context;
this.width = width;
this.scale = scale;
this.Hex = hc.defineHex({
dimensions: 1,
orientation: hc.Orientation.FLAT
});
this.grid = new hc.Grid(
this.Hex,
hc.rectangle({ width: 35, height: 35, start: [-6, -6] })
);
}
add_hat(coords, rotation, flip) {
this.hats.push(new Hat(coords, rotation, flip));
}
*add_and_add(radius = 6) {
const spiralTraverser = hc.spiral({ start: [0, 0], radius });
for (let f of this.grid.traverse(spiralTraverser)) {
yield new hc.Hex(f);
}
}
rect() {
const { width, scale } = this;
this.context.strokeRect(
-width / (2 * scale),
-width / (2 * scale),
width / scale,
width / scale
);
}
draw(context = null) {
const { scale, width } = this;
if (context === null) {
context = this.context;
}
context.clearRect(
-width / (2 * scale),
-width / (2 * scale),
width / scale,
width / scale
);
for (const hat of this.hats) {
context.save();
hat.draw(context);
context.restore();
}
return context.canvas;
}
collides(coords, rotation, flip) {
const { context, scale, width } = this;
context.clearRect(
-width / (2 * scale),
-width / (2 * scale),
width / scale,
width / scale
);
this.draw(context);
const existingHatsData = context.getImageData(
0,
0,
width * 2,
width * 2
).data;
const newHat = new Hat(coords, rotation, flip);
newHat.draw(context);
const updatedData = context.getImageData(0, 0, width * 2, width * 2).data;
let collisionsDetected = 0;
for (let i = 3; i < existingHatsData.length; i += 4) {
if (existingHatsData[i] > 0 && updatedData[i] > 129) {
collisionsDetected += 1;
}
}
console.log(collisionsDetected);
return collisionsDetected > 50;
}
}