mirror of
https://github.com/MaciejkaG/statki.git
synced 2024-11-30 05:22:55 +01:00
bb17dc47ba
- Fully working ship placement system - Validating ship positions works too - Client side ship data display works as well - Changes to field colors to dark grey instead of light. This improves comfort of use and ensures proper contrast on lower quality displays and ease of use.
232 lines
6.0 KiB
JavaScript
232 lines
6.0 KiB
JavaScript
const mapSize = 10;
|
|
|
|
const bsc = new Battleships(mapSize);
|
|
|
|
let board = bsc.generateDOMBoard(mapSize);
|
|
$("#board").html(board);
|
|
$("#secondaryBoard").html(board);
|
|
|
|
var previousRow = $(":not(*)");
|
|
var previousColumn = $(":not(*)");
|
|
var selectedShip = 0;
|
|
var shipRotation = 0;
|
|
var shipsLeft = [4, 3, 2, 1];
|
|
var changedFields = [];
|
|
|
|
var hoveredField = null;
|
|
|
|
refreshBoardView();
|
|
|
|
$("#board .field").hover(function () {
|
|
hoveredField = this;
|
|
// Pokaż "miarki"
|
|
let posY = parseInt($(this).data("pos-x"));
|
|
let posX = parseInt($(this).data("pos-y"));
|
|
let row = bsc.getRow(posY);
|
|
let column = bsc.getColumn(posX);
|
|
|
|
changedFields.push(row, column, $(this));
|
|
|
|
row.css("background", "var(--mark-line)");
|
|
column.css("background", "var(--mark-line)");
|
|
|
|
$(this).css("background", "var(--mark-spot)");
|
|
|
|
previousRow = row;
|
|
previousColumn = column;
|
|
|
|
// Pokaż podgląd statku
|
|
var fields = [];
|
|
switch (shipRotation) {
|
|
case 0:
|
|
for (let i = 0; i < 4; i++) {
|
|
fields.push([posX + i, posY]);
|
|
}
|
|
break;
|
|
case 1:
|
|
for (let i = 0; i < 4; i++) {
|
|
fields.push([posX, posY + i]);
|
|
}
|
|
break;
|
|
case 2:
|
|
for (let i = 0; i < 4; i++) {
|
|
fields.push([posX - i, posY]);
|
|
}
|
|
break;
|
|
case 3:
|
|
for (let i = 0; i < 4; i++) {
|
|
fields.push([posX, posY - i]);
|
|
}
|
|
break;
|
|
}
|
|
|
|
var fieldElem;
|
|
let failed = false;
|
|
for (let i = 0; i <= selectedShip; i++) {
|
|
const field = fields[i];
|
|
|
|
try {
|
|
fieldElem = bsc.getField(field[0], field[1]);
|
|
} catch {
|
|
if (!failed) {
|
|
failed = true;
|
|
i = -1;
|
|
}
|
|
}
|
|
|
|
if (failed) {
|
|
fieldElem.css("background", "var(--mark-ship-invalid)");
|
|
} else {
|
|
fieldElem.css("background", "var(--mark-ship-valid)");
|
|
}
|
|
changedFields.push(fieldElem);
|
|
}
|
|
}, function () {
|
|
hoveredField = null;
|
|
// Wyłącz "miarki" po wyjściu kursora z pola (aby się nie duplikowały w przyszłości)
|
|
changedFields.forEach(field => {
|
|
field.css("background", "var(--field)");
|
|
});
|
|
changedFields.length = 0;
|
|
});
|
|
|
|
$("#board .field").on("click", function() {
|
|
|
|
});
|
|
|
|
var ownBoardIsActive = true;
|
|
|
|
function switchBoards() {
|
|
if (ownBoardIsActive) { // Aktywna jest plansza użytkownika
|
|
$("#secondaryBoard").removeClass("secondary");
|
|
$("#board").addClass("secondary");
|
|
$(".ownBoardInfo").css("opacity", 0);
|
|
$(".controlsOwnBoard").css("opacity", 0.3);
|
|
} else { // Aktywna jest plansza przeciwnika
|
|
$("#board").removeClass("secondary");
|
|
$("#secondaryBoard").addClass("secondary");
|
|
$(".ownBoardInfo").css("opacity", 1);
|
|
$(".controlsOwnBoard").css("opacity", 1);
|
|
}
|
|
|
|
ownBoardIsActive = !ownBoardIsActive;
|
|
}
|
|
|
|
function switchShips() {
|
|
if (selectedShip===3) {
|
|
selectedShip = 0;
|
|
} else {
|
|
selectedShip++;
|
|
}
|
|
|
|
refreshBoardView();
|
|
|
|
$("#selectedShip").addClass("changing");
|
|
|
|
setTimeout(() => {
|
|
switch (selectedShip) {
|
|
case 0:
|
|
$("#selectedShip").html("Jednomasztowiec");
|
|
break;
|
|
case 1:
|
|
$("#selectedShip").html("Dwumasztowiec");
|
|
break;
|
|
case 2:
|
|
$("#selectedShip").html("Trójmasztowiec");
|
|
break;
|
|
case 3:
|
|
$("#selectedShip").html("Czteromasztowiec");
|
|
break;
|
|
}
|
|
|
|
$("#selectedShip").removeClass("changing");
|
|
}, 200);
|
|
}
|
|
|
|
function switchRotation() {
|
|
if (shipRotation === 3) {
|
|
shipRotation = 0;
|
|
} else {
|
|
shipRotation++;
|
|
}
|
|
|
|
refreshBoardView();
|
|
}
|
|
|
|
function refreshBoardView() {
|
|
let shipsOfType = shipsLeft[selectedShip];
|
|
$("#shipsLeft").html(shipsOfType);
|
|
if (!shipsOfType) {
|
|
$("#shipsLeft").addClass("danger");
|
|
} else {
|
|
$("#shipsLeft").removeClass("danger");
|
|
}
|
|
|
|
if (hoveredField) {
|
|
changedFields.forEach(field => {
|
|
field.css("background", "var(--field)");
|
|
});
|
|
changedFields.length = 0;
|
|
|
|
let posY = parseInt($(hoveredField).data("pos-x"));
|
|
let posX = parseInt($(hoveredField).data("pos-y"));
|
|
let row = bsc.getRow(posY);
|
|
let column = bsc.getColumn(posX);
|
|
|
|
changedFields.push(row, column, $(hoveredField));
|
|
|
|
row.css("background", "var(--mark-line)");
|
|
column.css("background", "var(--mark-line)");
|
|
|
|
$(hoveredField).css("background", "var(--mark-field)");
|
|
|
|
previousRow = row;
|
|
previousColumn = column;
|
|
|
|
var fields = [];
|
|
switch (shipRotation) {
|
|
case 0:
|
|
for (let i = 0; i < 4; i++) {
|
|
fields.push([posX + i, posY]);
|
|
}
|
|
break;
|
|
case 1:
|
|
for (let i = 0; i < 4; i++) {
|
|
fields.push([posX, posY + i]);
|
|
}
|
|
break;
|
|
case 2:
|
|
for (let i = 0; i < 4; i++) {
|
|
fields.push([posX - i, posY]);
|
|
}
|
|
break;
|
|
case 3:
|
|
for (let i = 0; i < 4; i++) {
|
|
fields.push([posX, posY - i]);
|
|
}
|
|
break;
|
|
}
|
|
|
|
var fieldElem;
|
|
let failed = false;
|
|
for (let i = 0; i <= selectedShip; i++) {
|
|
const field = fields[i];
|
|
|
|
try {
|
|
fieldElem = bsc.getField(field[0], field[1]);
|
|
} catch {
|
|
if (!failed) {
|
|
failed = true;
|
|
i = -1;
|
|
}
|
|
}
|
|
|
|
if (failed) {
|
|
fieldElem.css("background", "var(--mark-ship-invalid)");
|
|
} else {
|
|
fieldElem.css("background", "var(--mark-ship-valid)");
|
|
}
|
|
changedFields.push(fieldElem);
|
|
}
|
|
}
|
|
} |