From c685deb0862da659571da1d8a35836eaa53b3629 Mon Sep 17 00:00:00 2001
From: Alex <atm2g19@soton.ac.uk>
Date: Sun, 5 Apr 2020 23:43:54 +0100
Subject: [PATCH] patched sunk ships, added buttons for touchscreen

---
 public/games/battleships_v2/game.js | 34 ++++++++++++++++++++++++++++-
 public/games/battleships_v2/game.ts | 27 +++++++++++++++++++++++
 src/games/battleships_v2.ts         |  6 +++++
 3 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/public/games/battleships_v2/game.js b/public/games/battleships_v2/game.js
index 6729088..c104806 100644
--- a/public/games/battleships_v2/game.js
+++ b/public/games/battleships_v2/game.js
@@ -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,7 +312,11 @@ 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() {
         if (this.keyCode == 0x20 && this.firing) {
diff --git a/public/games/battleships_v2/game.ts b/public/games/battleships_v2/game.ts
index 0797c1d..93cf751 100644
--- a/public/games/battleships_v2/game.ts
+++ b/public/games/battleships_v2/game.ts
@@ -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);
 			})
 	}
diff --git a/src/games/battleships_v2.ts b/src/games/battleships_v2.ts
index f6f68b5..7ad2e0c 100644
--- a/src/games/battleships_v2.ts
+++ b/src/games/battleships_v2.ts
@@ -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]);
-- 
GitLab