diff --git a/Barco.js b/Barco.js index 1420c21..64951a5 100644 --- a/Barco.js +++ b/Barco.js @@ -12,6 +12,7 @@ class Barco { this.longitud = longitud; this.orientacion = orientacion; this.seleccionado = false; + this.superpuesto = false; } clickado(clickX, clickY) { @@ -48,7 +49,7 @@ class Barco { } draw(ctx) { - ctx.fillStyle = this.seleccionado?"green":"red"; + ctx.fillStyle = this.seleccionado ? "green" : "red"; this.orientacion === "VERTICAL" ? ctx.fillRect(this.x * 64, this.y * 64, 64, 64 * this.longitud) : ctx.fillRect(this.x * 64, this.y * 64, 64 * this.longitud, 64); diff --git a/Mapa.js b/Mapa.js index 33f25bc..af905a2 100644 --- a/Mapa.js +++ b/Mapa.js @@ -111,6 +111,7 @@ class Mapa { if (!this.colisionBorde(x, y)) this.barcoSeleccionado.setXY(x, y); } }; + posicionaBarcoSeleccionado() { this.setBarcos(this.barcoSeleccionado); this.barcoSeleccionado = null; @@ -124,6 +125,29 @@ class Mapa { : x < 0 || x + longitud > this.numColumnas || y < 0 || y > this.numFilas; } + sePuedeColocar() { + const x = this.barcoSeleccionado.x; + const y = this.barcoSeleccionado.y; + const longitud = this.barcoSeleccionado.longitud; + const orientacion = this.barcoSeleccionado.orientacion; + let suma = 0; + for (let f = -1; f <= longitud; f++) { + for (let c = -1; c < 2; c++) { + const fila = y + f; + const columna = x + c; + if ( + fila >= 0 && + fila < this.celdas.length && + columna >= 0 && + columna < this.celdas[0].length + ) { + suma += this.celdas[fila][columna]; + } + } + } + return suma; + } + draw() { this.ctx.clearRect(0, 0, this.width, this.height); this.barcos.forEach((barco) => { diff --git a/assets/barcos.png b/assets/barcos.png new file mode 100644 index 0000000..2d8a88f Binary files /dev/null and b/assets/barcos.png differ diff --git a/assets/barcos.xcf b/assets/barcos.xcf new file mode 100644 index 0000000..7e67df1 Binary files /dev/null and b/assets/barcos.xcf differ