Refactorizaciones

This commit is contained in:
2024-04-09 09:20:44 +02:00
parent 68177c2ffa
commit f3248202bb
10 changed files with 81 additions and 39 deletions

View File

@@ -6,7 +6,8 @@ export const BarcoTipo = Object.freeze({
});
class Barco {
constructor(x, y, longitud, orientacion, barcoImg, tipoNave) {
constructor(id, x, y, longitud, orientacion, barcoImg, tipoNave) {
this.id=id;
this.barcoImg = barcoImg;
this.tipoNave = tipoNave;
this.xIni = this.x = x;
@@ -59,5 +60,19 @@ class Barco {
: ctx.fillRect(this.x * 64, this.y * 64, 64 * this.longitud, 64);
}
}
toJSON() {
return {
id:this.id,
x:this.x,
y:this.y,
longitud:this.longitud,
orientacion:this.orientacion,
tipoNave: this.tipoNave
};
}
}
export default Barco;

View File

@@ -1,4 +1,4 @@
import { TableroEditor, TableroVisor } from './tablero.js';
import { TableroEditor, TableroAtaque ,TableroVisor } from './tablero.js';
const barcosImg = new Image();
barcosImg.src = '/assets/barcos.png';
@@ -105,12 +105,13 @@ function iniciaJuego() {
let tablero = null;
const OnChangeState = (snapshotJugador) => {
console.log(snapshotJugador);
switch (snapshotJugador.estado) {
case 'ENPREPARACION':
ENPREPARACION(snapshotJugador);
break;
case 'ENCURSO':
ENCURSO(snapshotJugador);
case 'ENESPERA':
ENESPERA(snapshotJugador);
break;
}
};
@@ -132,8 +133,7 @@ function iniciaJuego() {
const ENPREPARACION = (snapshotJugador) => {
muestraBoton('Fijar Flota',()=>{
console.log(JSON.stringify(tablero.mapaFlota.barcos));
socket.emit('sendFlota', tablero.mapaFlota.barcos,handlers.OnChangeState);
socket.emit('setFlota', tablero.mapaFlota.barcos,handlers.OnChangeState);
});
addMsgChat('Situa tu flota en el mapa y pulsa el boton para continuar');
tablero = new TableroEditor(canvasMapa, canvasMiniMapa, sprites, [
@@ -142,12 +142,14 @@ function iniciaJuego() {
]);
};
const ENESPERA = (snapshotJugador) => {
muestraAviso('Espera')
addMsgChat('Espera mientras el rival acaba de colocar sus tropas');
tablero = new TableroVisor(canvasMapa, canvasMiniMapa, sprites, [
snapshotJugador.mapaFlota,
snapshotJugador.mapaDeAtaques,
]);
};
function addMsgChat(msg) {
const divChat = document.querySelector('#logchat');

View File

@@ -43,6 +43,7 @@ class MapaVisor extends MapaBase {
for (const barco of barcosArray) {
this.barcos.push(
new Barco(
barco.id,
barco.x,
barco.y,
barco.longitud,
@@ -130,7 +131,7 @@ class MapaEditor extends MapaVisor {
for (let i = 0; i < barco.longitud; i++) {
const fila = barco.y + i * incFila;
const columna = barco.x + i * incColumna;
this.celdas[fila][columna] = barco.longitud;
this.celdas[fila][columna] = barco.id;
}
this.barcos.push(barco);
}

View File

@@ -50,4 +50,4 @@ class TableroAtaque extends TableroBase{
export {TableroBase,TableroEditor,TableroVisor}
export {TableroBase,TableroEditor,TableroVisor,TableroAtaque}