Indicate the player with a separate color (#19)
This commit is contained in:
1 parent
a8777035f8
commit
f2f4a9573f
4 files changed
+40
-25
No files matched your search
@@ -5,7 +5,7 @@ import { log } from "./utils/debugUtils";
|
|||||||
export class GameState {
|
export class GameState {
|
||||||
_errors: string[] = [];
|
_errors: string[] = [];
|
||||||
|
|
||||||
constructor(public readonly gameStateRaw: GameStateRawReadonly, private readonly client: Client) {
|
constructor(public readonly gameStateRaw: GameStateRawReadonly, readonly client: Client) {
|
||||||
this._validate();
|
this._validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -235,8 +235,8 @@ export class LobbyScene extends GameStateScene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public updateFromGameState(gameState: GameState): void {
|
public updateFromGameState(gameState: GameState): void {
|
||||||
this._playerList.clients = gameState.gameStateRaw.players;
|
this._playerList.updateFromGameState(gameState.players, gameState.client);
|
||||||
this._spectatorsList.clients = gameState.gameStateRaw.spectators;
|
this._spectatorsList.updateFromGameState(gameState.spectators, gameState.client);
|
||||||
this._updatePositions();
|
this._updatePositions();
|
||||||
this._updateVisibility(gameState);
|
this._updateVisibility(gameState);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ import * as ex from 'excalibur';
|
|||||||
import { spacingS } from '../utils/formattingUtils';
|
import { spacingS } from '../utils/formattingUtils';
|
||||||
import { headingFont, paragraphFont } from '../utils/fontUtil';
|
import { headingFont, paragraphFont } from '../utils/fontUtil';
|
||||||
import { Text } from './Text';
|
import { Text } from './Text';
|
||||||
import { Player, Spectator } from '../GameStateRaw';
|
import { Client, Player, Spectator } from '../GameStateRaw';
|
||||||
|
|
||||||
|
const clientColor = ex.Color.fromHex("40E0D0")
|
||||||
|
|
||||||
export class ClientListScreenElement extends ex.ScreenElement {
|
export class ClientListScreenElement extends ex.ScreenElement {
|
||||||
protected _clients: Player[] | Spectator[] = [];
|
protected _clients: Player[] | Spectator[] = [];
|
||||||
|
protected _client: Client | null = null;
|
||||||
|
|
||||||
protected heading: Text = null!;
|
protected heading: Text = null!;
|
||||||
protected entities: ex.Actor[] = [];
|
protected entities: ex.Actor[] = [];
|
||||||
|
|
||||||
@@ -29,8 +33,9 @@ export class ClientListScreenElement extends ex.ScreenElement {
|
|||||||
return this.computeChildrenHeight(this._clients.length);
|
return this.computeChildrenHeight(this._clients.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
set clients(labels: Player[] | Spectator[]) {
|
public updateFromGameState(clients: Player[] | Spectator[], client: Client): void {
|
||||||
this._clients = labels;
|
this._clients = clients
|
||||||
|
this._client = client;
|
||||||
if (this.isInitialized) {
|
if (this.isInitialized) {
|
||||||
this.updateList();
|
this.updateList();
|
||||||
}
|
}
|
||||||
@@ -41,13 +46,10 @@ export class ClientListScreenElement extends ex.ScreenElement {
|
|||||||
|
|
||||||
let y = headingFont.size + spacingS;
|
let y = headingFont.size + spacingS;
|
||||||
|
|
||||||
for(const client of this._clients) {
|
for(const currentClient of this._clients) {
|
||||||
const label = new Text({
|
const isClient = currentClient.player.id === this._client?.id;
|
||||||
text: client.player.name,
|
const label = this.createLabel(currentClient, y, isClient);
|
||||||
font: paragraphFont,
|
|
||||||
})
|
|
||||||
label.pos.setTo(0, y);
|
|
||||||
label.anchor.setTo(0.5, 0);
|
|
||||||
y += label.font.size + spacingS;
|
y += label.font.size + spacingS;
|
||||||
this.addChild(label);
|
this.addChild(label);
|
||||||
this.entities.push(label);
|
this.entities.push(label);
|
||||||
@@ -66,6 +68,20 @@ export class ClientListScreenElement extends ex.ScreenElement {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected createLabel(client: Player | Spectator, y: number, isClient: boolean): Text {
|
||||||
|
const color = isClient ? clientColor : undefined;
|
||||||
|
const { name } = client.player;
|
||||||
|
const label = new Text({
|
||||||
|
text: name,
|
||||||
|
font: paragraphFont,
|
||||||
|
color,
|
||||||
|
})
|
||||||
|
label.pos.setTo(0, y);
|
||||||
|
label.anchor.setTo(0.5, 0);
|
||||||
|
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
|
||||||
protected computeChildrenHeight(numLabels: number): number {
|
protected computeChildrenHeight(numLabels: number): number {
|
||||||
return headingFont.size + spacingS + Math.max(1, numLabels) * (spacingS + paragraphFont.size);
|
return headingFont.size + spacingS + Math.max(1, numLabels) * (spacingS + paragraphFont.size);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from 'excalibur';
|
||||||
import { spacingS } from '../utils/formattingUtils';
|
import { spacingS } from '../utils/formattingUtils';
|
||||||
import { disabledFont, paragraphFont } from '../utils/fontUtil';
|
import { disabledFont } from '../utils/fontUtil';
|
||||||
import { Text } from './Text';
|
import { Text } from './Text';
|
||||||
import { Checkbox } from './Checkbox';
|
import { Checkbox } from './Checkbox';
|
||||||
import { Player } from '../GameStateRaw';
|
import { Player } from '../GameStateRaw';
|
||||||
import { ClientListScreenElement } from './ClientListScreenElement';
|
import { ClientListScreenElement } from './ClientListScreenElement';
|
||||||
|
|
||||||
|
|
||||||
export class PlayerListScreenElement extends ClientListScreenElement {
|
export class PlayerListScreenElement extends ClientListScreenElement {
|
||||||
protected override _clients: Player[] = [];
|
protected override _clients: Player[] = [];
|
||||||
constructor(headingText: string, private _maxPlayers: number) {
|
constructor(headingText: string, private _maxPlayers: number) {
|
||||||
@@ -19,22 +20,20 @@ export class PlayerListScreenElement extends ClientListScreenElement {
|
|||||||
|
|
||||||
const clients = this._clients.slice(0, this._maxPlayers);
|
const clients = this._clients.slice(0, this._maxPlayers);
|
||||||
|
|
||||||
for(const client of clients) {
|
for(const currentClient of clients) {
|
||||||
const { name } = client.player;
|
const { name, id } = currentClient.player;
|
||||||
const label = new Text({
|
const isClient = id === this._client?.id;
|
||||||
text: name,
|
const label = this.createLabel(currentClient, y, isClient);
|
||||||
font: paragraphFont,
|
|
||||||
});
|
|
||||||
label.pos.setTo(0, y);
|
|
||||||
label.anchor.setTo(0.5, 0);
|
|
||||||
this.addChild(label);
|
|
||||||
|
|
||||||
const ready = new Checkbox({ checked: client.ready});
|
this.addChild(label);
|
||||||
|
this.entities.push(label);
|
||||||
|
|
||||||
|
|
||||||
|
const ready = new Checkbox({ checked: currentClient.ready});
|
||||||
const textWidth = label.font.measureTextWithoutCache(name ?? "").width
|
const textWidth = label.font.measureTextWithoutCache(name ?? "").width
|
||||||
ready.pos = ex.vec( textWidth / 2 + spacingS, y);
|
ready.pos = ex.vec( textWidth / 2 + spacingS, y);
|
||||||
this.addChild(ready);
|
this.addChild(ready);
|
||||||
|
|
||||||
this.entities.push(label);
|
|
||||||
this.entities.push(ready);
|
this.entities.push(ready);
|
||||||
|
|
||||||
y += label.font.size + spacingS;
|
y += label.font.size + spacingS;
|
||||||
|
|||||||
Reference in new issue
Block a user