BattleShip/Barco.js
2024-03-25 13:51:56 +01:00

50 lines
1.1 KiB
JavaScript

export const BarcoTipo = Object.freeze({
FRAGATA: 1,
DESTRUCTOR: 2,
ACORAZADO: 3,
PORTAAVIONES: 4,
});
class Barco {
seleccionado = true;
constructor(x, y, longitud, orientacion) {
this.x = x;
this.y = y;
this.longitud = longitud;
this.orientacion = orientacion;
this.dragging = false;
this.offsetX = 0;
this.offsetY = 0;
}
iniciarArrastre(event) {
this.dragging = true;
this.offsetX = event.offsetX;
this.offsetY = event.offsetY;
}
moverBarco(event, canvas) {
if (this.dragging) {
const rect = canvas.getBoundingClientRect();
this.x = event.clientX - rect.left - this.offsetX;
this.y = event.clientY - rect.top - this.offsetY;
}
}
finalizarArrastre() {
this.dragging = false;
}
draw(ctx) {
ctx.fillStyle = "red";
ctx.fillRect(this.x * 32, this.y * 32, 32, 32 * this.longitud);
if (this.seleccionado) {
ctx.strokeStyle = "blue";
ctx.setLineDash([5, 5]);
ctx.lineWidth = 2;
ctx.strokeRect(this.x * 32, this.y * 32, 32, 32 * this.longitud);
ctx.setLineDash([]);
}
}
}
export default Barco;