Skip to content
Snippets Groups Projects
Commit c685deb0 authored by atm2g19's avatar atm2g19
Browse files

patched sunk ships, added buttons for touchscreen

parent e677874c
Branches
No related tags found
No related merge requests found
......@@ -214,6 +214,26 @@ class defend extends base {
// rotation: rotation.NORTH,
// size: 5,
// }
this.createElement("br");
this.placeButton = this.createButton("Place");
this.placeButton.mousePressed(() => this.placeShip(this.placing));
this.rotateButton = this.createButton("Rotate");
this.rotateButton.mousePressed(() => {
switch (this.placing.rotation) {
case rotation.NORTH:
this.placing.rotation = rotation.EAST;
break;
case rotation.EAST:
this.placing.rotation = rotation.SOUTH;
break;
case rotation.SOUTH:
this.placing.rotation = rotation.WEST;
break;
case rotation.WEST:
this.placing.rotation = rotation.NORTH;
break;
}
});
}
addSocketListeners() {
this.socket.on("reset", this.reset.bind(this));
......@@ -272,6 +292,14 @@ class attack extends base {
}
setup() {
this.createCanvas(500, 500);
this.createElement("br");
this.fireButton = this.createButton("Fire!");
this.fireButton.size(100, 30);
this.fireButton.mouseClicked(() => {
// this.shots.push(this.firing);
this.socket.emit("fire", this.firing);
this.firing = null;
});
}
addSocketListeners() {
this.socket.on("reset", this.reset.bind(this));
......@@ -284,6 +312,10 @@ class attack extends base {
});
this.socket.on("fired", (shot) => {
this.shots.push(...shot);
}),
this.socket.on("sunk", (ships) => {
console.log("sunk");
this.placed_ships.push(...ships);
});
}
keyPressed() {
......
......@@ -257,6 +257,10 @@ class base extends p5 {
}
class defend extends base {
placeButton;
rotateButton;
constructor(socket) {
super(socket, "defend")
this.socket.removeAllListeners();
......@@ -273,6 +277,27 @@ class defend extends base {
// rotation: rotation.NORTH,
// size: 5,
// }
this.createElement("br");
this.placeButton = this.createButton("Place");
this.placeButton.mousePressed(() => this.placeShip(this.placing));
this.rotateButton = this.createButton("Rotate");
this.rotateButton.mousePressed(() => {
switch (this.placing.rotation) {
case rotation.NORTH:
this.placing.rotation = rotation.EAST;
break;
case rotation.EAST:
this.placing.rotation = rotation.SOUTH;
break;
case rotation.SOUTH:
this.placing.rotation = rotation.WEST;
break;
case rotation.WEST:
this.placing.rotation = rotation.NORTH;
break;
}
});
}
......@@ -340,6 +365,7 @@ class attack extends base {
setup() {
this.createCanvas(500, 500);
this.createElement("br");
this.fireButton = this.createButton("Fire!");
this.fireButton.size(100, 30);
this.fireButton.mouseClicked(() => {
......@@ -362,6 +388,7 @@ class attack extends base {
this.shots.push(...shot);
}),
this.socket.on("sunk", (ships: ship[]) => {
console.log("sunk");
this.placed_ships.push(...ships);
})
}
......
......@@ -287,6 +287,12 @@ const battleships_v2: GameConstructor = class battleships_v2 implements Game {
if (shot.hit && shot.x == X && shot.y == Y) hits--;
}
}
console.log({
hits,
shot,
hit,
ship
});
if (hits == 0 && hit == ship) {
console.log("SUNK");
player.socket.emit("sunk", [ship]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment