Colisiones

This commit is contained in:
Marklogo 2024-03-26 13:48:53 +01:00
parent f0e9077bf0
commit 325522a07c
4 changed files with 26 additions and 1 deletions

View File

@ -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);

24
Mapa.js
View File

@ -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) => {

BIN
assets/barcos.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

BIN
assets/barcos.xcf Normal file

Binary file not shown.