Colisiones
This commit is contained in:
parent
f0e9077bf0
commit
325522a07c
3
Barco.js
3
Barco.js
|
|
@ -12,6 +12,7 @@ class Barco {
|
||||||
this.longitud = longitud;
|
this.longitud = longitud;
|
||||||
this.orientacion = orientacion;
|
this.orientacion = orientacion;
|
||||||
this.seleccionado = false;
|
this.seleccionado = false;
|
||||||
|
this.superpuesto = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
clickado(clickX, clickY) {
|
clickado(clickX, clickY) {
|
||||||
|
|
@ -48,7 +49,7 @@ class Barco {
|
||||||
}
|
}
|
||||||
|
|
||||||
draw(ctx) {
|
draw(ctx) {
|
||||||
ctx.fillStyle = this.seleccionado?"green":"red";
|
ctx.fillStyle = this.seleccionado ? "green" : "red";
|
||||||
this.orientacion === "VERTICAL"
|
this.orientacion === "VERTICAL"
|
||||||
? ctx.fillRect(this.x * 64, this.y * 64, 64, 64 * this.longitud)
|
? ctx.fillRect(this.x * 64, this.y * 64, 64, 64 * this.longitud)
|
||||||
: ctx.fillRect(this.x * 64, this.y * 64, 64 * this.longitud, 64);
|
: ctx.fillRect(this.x * 64, this.y * 64, 64 * this.longitud, 64);
|
||||||
|
|
|
||||||
24
Mapa.js
24
Mapa.js
|
|
@ -111,6 +111,7 @@ class Mapa {
|
||||||
if (!this.colisionBorde(x, y)) this.barcoSeleccionado.setXY(x, y);
|
if (!this.colisionBorde(x, y)) this.barcoSeleccionado.setXY(x, y);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
posicionaBarcoSeleccionado() {
|
posicionaBarcoSeleccionado() {
|
||||||
this.setBarcos(this.barcoSeleccionado);
|
this.setBarcos(this.barcoSeleccionado);
|
||||||
this.barcoSeleccionado = null;
|
this.barcoSeleccionado = null;
|
||||||
|
|
@ -124,6 +125,29 @@ class Mapa {
|
||||||
: x < 0 || x + longitud > this.numColumnas || y < 0 || y > this.numFilas;
|
: 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() {
|
draw() {
|
||||||
this.ctx.clearRect(0, 0, this.width, this.height);
|
this.ctx.clearRect(0, 0, this.width, this.height);
|
||||||
this.barcos.forEach((barco) => {
|
this.barcos.forEach((barco) => {
|
||||||
|
|
|
||||||
BIN
assets/barcos.png
Normal file
BIN
assets/barcos.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 190 KiB |
BIN
assets/barcos.xcf
Normal file
BIN
assets/barcos.xcf
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user