Create StructureLayer

This commit is contained in:
Evan
2024-11-15 20:43:15 -08:00
parent 71be40a2cd
commit 5456988709
4 changed files with 145 additions and 51 deletions
-51
View File
@@ -70,52 +70,6 @@ export class UnitLayer implements Layer {
);
}
private handlePortEvent(event: UnitEvent) {
if (!this.anchorImageLoaded) return;
// Create a temporary canvas to process the anchor icon
const tempCanvas = document.createElement('canvas');
const tempContext = tempCanvas.getContext('2d');
tempCanvas.width = this.anchorImage.width;
tempCanvas.height = this.anchorImage.height;
// Draw the anchor icon to the temporary canvas
tempContext.drawImage(this.anchorImage, 0, 0);
const iconData = tempContext.getImageData(0, 0, tempCanvas.width, tempCanvas.height);
// Calculate position to center the icon on the port
const cell = event.unit.tile().cell();
const startX = cell.x - Math.floor(tempCanvas.width / 2);
const startY = cell.y - Math.floor(tempCanvas.height / 2);
bfs(event.unit.tile(), euclDist(event.unit.tile(), 8))
.forEach(t => this.paintCell(t.cell(), this.theme.territoryColor(event.unit.owner().info()), 255));
// Process each pixel of the icon
for (let y = 0; y < tempCanvas.height; y++) {
for (let x = 0; x < tempCanvas.width; x++) {
const iconIndex = (y * tempCanvas.width + x) * 4;
const alpha = iconData.data[iconIndex + 3];
if (alpha > 0) { // Only process non-transparent pixels
const targetX = startX + x;
const targetY = startY + y;
// Check if the target pixel is within the game bounds
if (targetX >= 0 && targetX < this.game.width() &&
targetY >= 0 && targetY < this.game.height()) {
// Color the pixel using the unit owner's colors
this.paintCell(
new Cell(targetX, targetY),
this.theme.borderColor(event.unit.owner().info()),
alpha
);
}
}
}
}
}
onUnitEvent(event: UnitEvent) {
switch (event.unit.type()) {
case UnitType.TransportShip:
@@ -124,11 +78,6 @@ export class UnitLayer implements Layer {
case UnitType.Destroyer:
this.handleDestroyerEvent(event);
break;
case UnitType.Port:
this.handlePortEvent(event);
break;
default:
throw Error(`event for unit ${event.unit.type()} not supported`);
}
}