Skip to content

Commit

Permalink
NextTurnButton: Indicate we are waiting for other players
Browse files Browse the repository at this point in the history
Hotfix: Correct invalid import in Unit.ts
  • Loading branch information
RyanGrieb committed Nov 17, 2023
1 parent 189b665 commit 0ddd476
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
1 change: 0 additions & 1 deletion client/src/Unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { NetworkEvents } from "./network/Client";
import { AbstractPlayer } from "./player/AbstractPlayer";
import { Actor } from "./scene/Actor";
import { ActorGroup } from "./scene/ActorGroup";
import { InGameScene } from "./scene/type/InGameScene";
import { UnitDisplayInfo } from "./ui/UnitDisplayInfo";

export class UnitActionManager {
Expand Down
6 changes: 3 additions & 3 deletions client/src/player/AbstractPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export class AbstractPlayer {
private name: string;
private civData: JSON;

constructor(name: string, civData: JSON) {
this.name = name;
this.civData = civData;
constructor(playerJSON: JSON) {
this.civData = playerJSON["civData"];
this.name = playerJSON["name"];
}

public static getPlayerByName(name: string) {
Expand Down
14 changes: 12 additions & 2 deletions client/src/player/ClientPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export class ClientPlayer extends AbstractPlayer {
private hoveredTile: HoveredTile;
private movementLines: Line[];
private rightMouseDrag: boolean;
private requestedNextTurn: boolean;

constructor(name: string, civData: JSON) {
super(name, civData);
constructor(playerJSON: JSON) {
super(playerJSON);

this.movementLines = [];
this.requestedNextTurn = playerJSON["requestedNextTurn"];

Game.getCurrentScene().on("mapLoaded", () => {
this.hoveredTile = new HoveredTile(9999, 9999);
Expand Down Expand Up @@ -180,6 +182,14 @@ export class ClientPlayer extends AbstractPlayer {
});
}

public setRequestedNextTurn(value: boolean) {
this.requestedNextTurn = value;
}

public hasRequestedNextTurn() {
return this.requestedNextTurn;
}

private onMouseRightClick() {
this.rightMouseDrag = true;

Expand Down
4 changes: 2 additions & 2 deletions client/src/player/ExternalPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AbstractPlayer } from "./AbstractPlayer";

export class ExternalPlayer extends AbstractPlayer {
constructor(name: string, civData: JSON) {
super(name, civData);
constructor(playerJSON: JSON) {
super(playerJSON);
}
}
35 changes: 30 additions & 5 deletions client/src/scene/type/InGameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ export class InGameScene extends Scene {
callback: (data) => {
for (let i = 0; i < data["players"].length; i++) {
const playerJSON = data["players"][i];
const civData = playerJSON["civData"];
if (playerJSON["name"] === data["requestingName"]) {
this.clientPlayer = new ClientPlayer(playerJSON["name"], civData);
this.clientPlayer = new ClientPlayer(playerJSON);
this.players.push(this.clientPlayer);
} else {
this.players.push(new ExternalPlayer(playerJSON["name"], civData));
this.players.push(new ExternalPlayer(playerJSON));
}
}
},
Expand Down Expand Up @@ -78,15 +77,32 @@ export class InGameScene extends Scene {
this.addActor(this.statusBar);

this.nextTurnButton = new Button({
text: "Next Turn",
text: this.clientPlayer.hasRequestedNextTurn()
? "Waiting..."
: "Next Turn",
x: Game.getWidth() / 2 - 150 / 2,
y: Game.getHeight() - 44,
z: 6,
width: 150,
height: 42,
fontColor: "white",
onClicked: () => {
WebsocketClient.sendMessage({ event: "nextTurnRequest" });
// Undo next turn request.
if (this.clientPlayer.hasRequestedNextTurn()) {
this.nextTurnButton.setText("Next Turn");
WebsocketClient.sendMessage({
event: "nextTurnRequest",
value: false,
});
this.clientPlayer.setRequestedNextTurn(false);
} else {
WebsocketClient.sendMessage({
event: "nextTurnRequest",
value: true,
});
this.nextTurnButton.setText("Waiting...");
this.clientPlayer.setRequestedNextTurn(true);
}
},
});
this.addActor(this.nextTurnButton);
Expand Down Expand Up @@ -152,6 +168,15 @@ export class InGameScene extends Scene {
if (this.firstLoad) {
WebsocketClient.sendMessage({ event: "loadedIn" });
}

NetworkEvents.on({
eventName: "newTurn",
parentObject: this,
callback: (data) => {
this.nextTurnButton.setText("Next Turn");
this.clientPlayer.setRequestedNextTurn(false);
},
});
});
}

Expand Down
4 changes: 4 additions & 0 deletions client/src/ui/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,8 @@ export class Button extends ActorGroup {
Game.setCursor("default");
}
}

public setText(text: string) {
this.text = text;
}
}
1 change: 1 addition & 0 deletions server/src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export class Player {
return {
name: this.name,
civData: this.civilizationData,
requestedNextTurn: this.requestedNextTurn,
};
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/state/type/InGameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class InGameState extends State {
parentObject: this,
callback: (data, websocket) => {
const player = Game.getPlayerFromWebsocket(websocket);
player.setRequestedNextTurn(true);
player.setRequestedNextTurn(data["value"]);

const allRequested = Array.from(Game.getPlayers().values()).every(
(player) => player.hasRequestedNextTurn()
Expand Down

0 comments on commit 0ddd476

Please sign in to comment.