From 2b1bb586c9fd64e784cd1138749f7186831814ed Mon Sep 17 00:00:00 2001 From: Lucas Habersaat Date: Mon, 20 Oct 2025 19:17:23 +0200 Subject: [PATCH] Frontend toggleRead/toggleRole actions and join (#13) --- src/frontend/src/Connection.ts | 39 ++++++++-- .../src/GameElements/PlayerNameInput.ts | 5 ++ src/frontend/src/GameState.ts | 58 ++++++++++++++- src/frontend/src/GameStateRaw.ts | 22 ++++++ src/frontend/src/Scenes/LobbyScene.ts | 73 ++++++++++++++----- src/frontend/src/ScreenElements/Button.ts | 71 +++++++++++------- src/frontend/src/ScreenElements/Checkbox.ts | 50 +++++++++++++ ...nElement.ts => ClientListScreenElement.ts} | 27 +++---- .../ScreenElements/PlayerListScreenElement.ts | 33 ++++++--- src/frontend/src/main.ts | 26 ++++++- src/frontend/src/utils/debugUtils.ts | 2 +- src/frontend/src/utils/styledRectangles.ts | 21 ++++++ 12 files changed, 347 insertions(+), 80 deletions(-) create mode 100644 src/frontend/src/GameStateRaw.ts create mode 100644 src/frontend/src/ScreenElements/Checkbox.ts rename src/frontend/src/ScreenElements/{TitledListScreenElement.ts => ClientListScreenElement.ts} (71%) create mode 100644 src/frontend/src/utils/styledRectangles.ts diff --git a/src/frontend/src/Connection.ts b/src/frontend/src/Connection.ts index b06b221..8d863d6 100644 --- a/src/frontend/src/Connection.ts +++ b/src/frontend/src/Connection.ts @@ -1,4 +1,5 @@ import { GameState } from './GameState'; +import { Client, GameStateRaw } from './GameStateRaw'; import { log } from './utils/debugUtils'; import { Event, Evented } from './utils/Evented'; @@ -29,6 +30,7 @@ export class GameMessageEvent extends Event { export class ConnectEvent extends Event { } export class DisconnectEvent extends Event { } +export class JoinedEvent extends Event { } export class ConnectOrDisconnectEvent extends Event { constructor(public connected: boolean) { super(); @@ -37,6 +39,7 @@ export class ConnectOrDisconnectEvent extends Event { export const ConnectionEvents = { "game-message": GameMessageEvent, + "joined": JoinedEvent, "connect": ConnectEvent, "disconnect": DisconnectEvent, "connect-or-disconnect": ConnectOrDisconnectEvent, @@ -44,6 +47,10 @@ export const ConnectionEvents = { export class Connection extends Evented { private _socket: WebSocket | null = null; + private _connected = false; + + private _waitForJoin = false; + private _client : Client | null = null; get connected(): boolean { const { _socket } = this; @@ -63,23 +70,42 @@ export class Connection extends Evented { log("Connection", "received", event) const messageObject = JSON.parse(event.data); - const gameState = messageObject.gamestate; - const gameMessage = new GameMessageEvent(gameState) - this.emit("game-message", gameMessage); + + if (this._waitForJoin) { + this._client = messageObject.player as Client; + this._waitForJoin = false; + this.emit("joined", new JoinedEvent()); + return; + } + + + // We only emit game message events when we have joined. + if (this._client) { + const gameState: GameStateRaw = messageObject.gamestate; + const gameMessage = new GameMessageEvent(new GameState(gameState, this._client)); + this.emit("game-message", gameMessage); + } } private _onConnect(): void { log("Connection", "connected"); - this.emit("connect", new DisconnectEvent()); + this._connected = true; + + this.emit("connect", new ConnectEvent()); this.emit("connect-or-disconnect", new ConnectOrDisconnectEvent(true)); } private _onDisconnect(): void { log("Connection", "disconnected"); - this.emit("disconnect", new DisconnectEvent()); - this.emit("connect-or-disconnect", new ConnectOrDisconnectEvent(false)); + // The socket close event is also invoked when a connection attempt fails. + // Only emit the disconnect event when we actually had a connection. + if (this._connected) { + this._connected = false; + this.emit("disconnect", new DisconnectEvent()); + this.emit("connect-or-disconnect", new ConnectOrDisconnectEvent(false)); + } // Try again this.connect(); @@ -104,6 +130,7 @@ export class Connection extends Evented { name, } as JoinRequest; + this._waitForJoin = true; this._send(joinRequest); } diff --git a/src/frontend/src/GameElements/PlayerNameInput.ts b/src/frontend/src/GameElements/PlayerNameInput.ts index 0c247ff..85821f1 100644 --- a/src/frontend/src/GameElements/PlayerNameInput.ts +++ b/src/frontend/src/GameElements/PlayerNameInput.ts @@ -63,6 +63,11 @@ export class PlayerNameInput extends ex.ScreenElement { return this._height; } + clear(): void { + this._nameInput.text = ""; + this._updateJoinButtonDisabled(); + } + private _onConnect(): void { this._updateJoinButtonDisabled(); } diff --git a/src/frontend/src/GameState.ts b/src/frontend/src/GameState.ts index d36eaf4..646901f 100644 --- a/src/frontend/src/GameState.ts +++ b/src/frontend/src/GameState.ts @@ -1,5 +1,57 @@ +import { Client, GameStateRawReadonly, Id, Player, Spectator } from "./GameStateRaw"; +import { maxPlayers } from "./main"; +import { log } from "./utils/debugUtils"; -export interface GameState { - players: string[]; - spectators: string[]; +export class GameState { + _errors: string[] = []; + + constructor(public readonly gameStateRaw: GameStateRawReadonly, private readonly client: Client) { + this._validate(); + } + + private _validate(): void { + if (!this.isPlayer && !this.isSpectator) { + this._errors.push("You must be either a player or spectator.") + } + + if (this.isPlayer && this.isSpectator) { + this._errors.push("You cannot be both player and spectator.") + } + + if (this._errors.length > 0) { + log("GameState", this._errors); + } + } + + get players(): Player[] { + return this.gameStateRaw.players; + } + + get spectators(): Spectator[] { + return this.gameStateRaw.spectators; + } + + get isFull(): boolean { + return this.players.length >= maxPlayers; + } + + get isPlayer(): boolean { + return this.players.map(p => p.player.id).includes(this.client.id); + } + + get isReady(): boolean { + return this.getPlayerById(this.client.id)!.ready; + } + + get isSpectator(): boolean { + return this.spectators.map(p => p.player.id).includes(this.client.id); + } + + getPlayerById(id: Id): Player | undefined { + return this.players.find(p => p.player.id === id); + } + + getSpectatorById(id: Id): Spectator | undefined { + return this.spectators.find(p => p.player.id === id); + } } \ No newline at end of file diff --git a/src/frontend/src/GameStateRaw.ts b/src/frontend/src/GameStateRaw.ts new file mode 100644 index 0000000..95cf281 --- /dev/null +++ b/src/frontend/src/GameStateRaw.ts @@ -0,0 +1,22 @@ +export type Id = number; + +export interface GameStateRaw { + players: Player[]; + spectators: Spectator[]; +} + +export type GameStateRawReadonly = Readonly + +export interface Player { + player: Client; + ready: boolean; +} + +export interface Spectator { + player: Client; +} + +export interface Client { + id: Id; + name: string; +} diff --git a/src/frontend/src/Scenes/LobbyScene.ts b/src/frontend/src/Scenes/LobbyScene.ts index 774f4a4..2fe18ca 100644 --- a/src/frontend/src/Scenes/LobbyScene.ts +++ b/src/frontend/src/Scenes/LobbyScene.ts @@ -1,18 +1,18 @@ import * as ex from 'excalibur'; import { spacingL, spacingM } from '../utils/formattingUtils'; import { Cups } from '../Cups'; -import { GameState } from '../GameState'; import { GameStateScene } from './GameStateScene'; import { PlayerListScreenElement } from '../ScreenElements/PlayerListScreenElement'; -import { TitledListScreenElement } from '../ScreenElements/TitledListScreenElement'; +import { ClientListScreenElement } from '../ScreenElements/ClientListScreenElement'; import { Button } from '../ScreenElements/Button'; import { PlayerNameInput } from '../GameElements/PlayerNameInput'; import { Connection, GameMessageEvent } from '../Connection'; import { Title } from '../GameElements/Title'; import { ErrorList } from '../GameElements/ErrorList'; +import { GameState } from '../GameState'; +import { maxPlayers } from '../main'; const tableWidth = 400; -const maxPlayers = 2; export type Screens = "join" | "lobby"; @@ -27,11 +27,14 @@ export class LobbyScene extends GameStateScene { private _bar3: ex.ScreenElement = null!; private _playerList: PlayerListScreenElement = null!; - private _spectatorsList: TitledListScreenElement = null!; + private _spectatorsList: ClientListScreenElement = null!; private _joinSpectatorButton: Button = null!; private _joinPlayerButton: Button = null!; + private _readyButton: Button = null!; + private _unreadyButton: Button = null!; + private _playerNameInput: PlayerNameInput = null!; private _errorList: ErrorList = null!; @@ -41,7 +44,13 @@ export class LobbyScene extends GameStateScene { constructor(private _connection: Connection) { super(); - this._connection.on("game-message", (e: GameMessageEvent) => this.updateFromGameState(e.gameState)) + + this._connection.on("joined", () => this.setScreen("lobby")); + this._connection.on("disconnect", () => { + this._playerNameInput.clear(); + this.setScreen("join"); + }); + this._connection.on("game-message", (e: GameMessageEvent) => this.updateFromGameState(e.gameState)); } onInitialize(engine: ex.Engine): void { @@ -84,7 +93,6 @@ export class LobbyScene extends GameStateScene { this.add(this._title); this._playerList = new PlayerListScreenElement("Players", maxPlayers); - this._playerList.labels = ["Dubbel"]; this._playerList.anchor.setTo(0.5, 0); this._joinPlayerButton = new Button({ @@ -93,8 +101,7 @@ export class LobbyScene extends GameStateScene { onClick: () => this._joinAsPlayer(), }); - this._spectatorsList = new TitledListScreenElement("Spectators"); - this._spectatorsList.labels = ["Dubbel", "Cocksucker"]; + this._spectatorsList = new ClientListScreenElement("Spectators"); this._spectatorsList.anchor.setTo(0.5, 0); this._joinSpectatorButton = new Button({ @@ -103,6 +110,19 @@ export class LobbyScene extends GameStateScene { onClick: () => this._joinAsSpectator(), }); + this._readyButton = new Button({ + text: "I'm Ready", + anchor: ex.vec(0.5, 0), + color: ex.Color.Green, + onClick: () => this._ready(), + }); + this._unreadyButton = new Button({ + text: "Not Ready", + anchor: ex.vec(0.5, 0), + color: ex.Color.Red, + onClick: () => this._unready(), + }); + // Name input this._playerNameInput = new PlayerNameInput(this._connection, (_name: string) => this._join()); this._playerNameInput.anchor = ex.vec(0.5, 0); @@ -143,7 +163,7 @@ export class LobbyScene extends GameStateScene { return bar; } - private _updateVisibility(): void { + private _updateVisibility(gameState?: GameState): void { const isJoinScreen = this._screen === "join"; const setVisible = (g: ex.Actor, visible: boolean): void => { if (visible) { @@ -153,12 +173,22 @@ export class LobbyScene extends GameStateScene { } } + const isPlayer = gameState?.isPlayer ?? false; + const isSpectator = gameState?.isSpectator ?? false; + const isReadyVisible = isPlayer && (gameState?.isReady ?? false); + const isNotReadyVisible = isPlayer && ((gameState != null && !gameState.isReady) ?? false); + const isLobbyFull = gameState?.isFull ?? false; + setVisible(this._playerNameInput, isJoinScreen); setVisible(this._playerList, !isJoinScreen); - setVisible(this._joinPlayerButton, !isJoinScreen); + setVisible(this._joinPlayerButton, !isJoinScreen && isSpectator && !isLobbyFull); setVisible(this._spectatorsList, !isJoinScreen); setVisible(this._bar3, !isJoinScreen); - setVisible(this._joinSpectatorButton, !isJoinScreen); + setVisible(this._joinSpectatorButton, !isJoinScreen && isPlayer); + + setVisible(this._readyButton, !isJoinScreen && isNotReadyVisible); + setVisible(this._unreadyButton, !isJoinScreen && isReadyVisible); + } private _updatePositions(): void { @@ -189,6 +219,8 @@ export class LobbyScene extends GameStateScene { y += this._playerList.childrenHeight + spacingM; this._joinPlayerButton.pos = ex.vec(halfWidth, y); + this._readyButton.pos = ex.vec(halfWidth, y); + this._unreadyButton.pos = ex.vec(halfWidth, y); y += this._joinPlayerButton.height + spacingL; this._bar3.pos = ex.vec(halfWidth, y); @@ -203,15 +235,14 @@ export class LobbyScene extends GameStateScene { } public updateFromGameState(gameState: GameState): void { - this.setScreen("lobby"); - this._playerList.labels = gameState.players; - this._spectatorsList.labels = gameState.spectators; + this._playerList.clients = gameState.gameStateRaw.players; + this._spectatorsList.clients = gameState.gameStateRaw.spectators; this._updatePositions(); + this._updateVisibility(gameState); } public setScreen(screen: Screens): void { this._screen = screen; - this._updateVisibility(); this._updatePositions(); } @@ -220,10 +251,18 @@ export class LobbyScene extends GameStateScene { } private _joinAsPlayer(): void { - // Send request + this._connection.toggleRole(); } private _joinAsSpectator(): void { - // Send request + this._connection.toggleRole(); + } + + private _ready(): void { + this._connection.toggleReady(); + } + + private _unready(): void { + this._connection.toggleReady(); } } \ No newline at end of file diff --git a/src/frontend/src/ScreenElements/Button.ts b/src/frontend/src/ScreenElements/Button.ts index 426b982..d6906b0 100644 --- a/src/frontend/src/ScreenElements/Button.ts +++ b/src/frontend/src/ScreenElements/Button.ts @@ -2,10 +2,12 @@ import * as ex from 'excalibur'; import { Text } from './Text'; import { paragraphFont } from '../utils/fontUtil'; import { spacingS } from '../utils/formattingUtils'; +import { lowerBarHeightDown, makeStyledRectangle } from '../utils/styledRectangles'; interface ButtonProperties { text?: string; disabled?: boolean; + color?: ex.Color, onClick?: (event: ex.PointerEvent) => void; } @@ -14,21 +16,24 @@ type ConstructProperties = ex.ActorArgs & ButtonProperties; export class Button extends ex.ScreenElement { private _text?: string; private _textElement: Text = null!; + private _color?: ex.Color; private _disabled: boolean; private _onClick?: (event: ex.PointerEvent) => void; + private _down: boolean = false; + constructor(config?: ConstructProperties) { super(config); this._text = config?.text; this._disabled = config?.disabled ?? false; + this._color = config?.color; this._onClick = config?.onClick; } onInitialize(_engine: ex.Engine): void { - const width = paragraphFont.measureTextWithoutCache(this.text).width + this.padding; - const height = this.height; + const { height, width } = this; this._textElement = new Text({ text: this._text, @@ -38,26 +43,18 @@ export class Button extends ex.ScreenElement { this._textElement.pos = ex.vec(0, height / 2); this.addChild(this._textElement); - this.graphics.add('idle', new ex.Rectangle({ - width, - height, - color: ex.Color.fromHex("4788ff"), - })); + const color = this._color ?? ex.Color.fromHex("4788ff"); + const hoverColor = color.lighten(0.5); - this.graphics.add('hover', new ex.Rectangle({ - width, - height, - color: ex.Color.fromHex("bad2ff") - })); + this.graphics.add('idle', makeStyledRectangle(width, height, color, false)); + this.graphics.add('hover', makeStyledRectangle(width, height, hoverColor, false)); + this.graphics.add('disabled', makeStyledRectangle(width, height, ex.Color.Gray, false)); // this._createButtonGraphic(ex.Color.Gray)); + this.graphics.add("clicked", makeStyledRectangle(width, height, hoverColor, true)); - this.graphics.add('disabled', new ex.Rectangle({ - width, - height, - color: ex.Color.Gray, - })); - this._updateGraphic(); + this._updateGraphic("idle"); this.on('pointerup', (e) => this.onPointerUp(e)); + this.on('pointerdown', (e) => this.onPointerDown(e)); this.on('pointerenter', (e) => this.onPointerEnter(e)); this.on('pointerleave', (e) => this.onPointerLeave(e)); } @@ -75,6 +72,10 @@ export class Button extends ex.ScreenElement { this._updateGraphic(); } + get width(): number { + return paragraphFont.measureTextWithoutCache(this.text).width + this.padding; + } + get height(): number { return paragraphFont.size + this.padding; } @@ -82,24 +83,37 @@ export class Button extends ex.ScreenElement { get padding(): number { return 2 * spacingS; } - - onPointerUp(event: ex.PointerEvent): void { + + onPointerDown(_event: ex.PointerEvent): void { if (this._disabled) { return; } + this._down = true; + this._updateGraphic("clicked") + } + + onPointerUp(event: ex.PointerEvent): void { + if (this._disabled || !this._down) { + return; + } + + this._down = false; + const { _onClick } = this; if (_onClick != null) { _onClick(event); } + + this._updateGraphic("hover") } onPointerEnter(_event: ex.PointerEvent): void { - if (this._disabled) { + if (this._disabled || this._down) { return; } - this.graphics.use('hover'); + this._updateGraphic("hover") } onPointerLeave(_event: ex.PointerEvent): void { @@ -107,11 +121,16 @@ export class Button extends ex.ScreenElement { return; } - this.graphics.use('idle'); + this._updateGraphic("idle") } - private _updateGraphic(): void { - this.graphics.use(this.disabled ? 'disabled' : 'idle') - } + private _updateGraphic(state: string = "idle"): void { + if (this.disabled) { + this.graphics.use("disabled"); + return; + } + this.graphics.use(state); + this._textElement.pos = state === "clicked" ? ex.vec(0, this.height / 2 + lowerBarHeightDown) : ex.vec(0, this.height / 2) ; + } } \ No newline at end of file diff --git a/src/frontend/src/ScreenElements/Checkbox.ts b/src/frontend/src/ScreenElements/Checkbox.ts new file mode 100644 index 0000000..674fac3 --- /dev/null +++ b/src/frontend/src/ScreenElements/Checkbox.ts @@ -0,0 +1,50 @@ +import * as ex from 'excalibur'; +import { paragraphFont } from '../utils/fontUtil'; +import { spacingS } from '../utils/formattingUtils'; + +interface CheckboxProperties { + checked: boolean; +}; + +type ConstructProperties = ex.ActorArgs & CheckboxProperties; + +export class Checkbox extends ex.ScreenElement { + private _checked: boolean; + + constructor(config: ConstructProperties) { + super(config); + this._checked = config.checked; + } + + onInitialize(): void { + const size = paragraphFont.size - spacingS; + const color = ex.Color.Green; + + const checkedGraphics = new ex.GraphicsGroup({ + members: [ + { graphic: new ex.Circle({ radius: size / 2, color }), offset: ex.vec(0, 0) }, + + ], + }); + + const uncheckedGraphics = new ex.GraphicsGroup({ members: [ ], }) + + this.graphics.add('checked', checkedGraphics); + this.graphics.add('unchecked', uncheckedGraphics); + + this._updateGraphic(); + } + + get checked(): boolean { + return this._checked; + } + + set checked(checked: boolean) { + this._checked = checked; + this._updateGraphic(); + } + + private _updateGraphic(): void { + this.graphics.use(this.checked ? "checked" : "unchecked"); + } +} \ No newline at end of file diff --git a/src/frontend/src/ScreenElements/TitledListScreenElement.ts b/src/frontend/src/ScreenElements/ClientListScreenElement.ts similarity index 71% rename from src/frontend/src/ScreenElements/TitledListScreenElement.ts rename to src/frontend/src/ScreenElements/ClientListScreenElement.ts index 5e2f1ca..67a6fc4 100644 --- a/src/frontend/src/ScreenElements/TitledListScreenElement.ts +++ b/src/frontend/src/ScreenElements/ClientListScreenElement.ts @@ -3,11 +3,12 @@ import * as ex from 'excalibur'; import { spacingS } from '../utils/formattingUtils'; import { headingFont, paragraphFont } from '../utils/fontUtil'; import { Text } from './Text'; +import { Player, Spectator } from '../GameStateRaw'; -export class TitledListScreenElement extends ex.ScreenElement { - protected _labels: string[] = []; +export class ClientListScreenElement extends ex.ScreenElement { + protected _clients: Player[] | Spectator[] = []; protected heading: Text = null!; - protected labelsEntities: Text[] = []; + protected entities: ex.Actor[] = []; constructor(private _headingText: string) { super(); @@ -25,11 +26,11 @@ export class TitledListScreenElement extends ex.ScreenElement { } get childrenHeight(): number { - return this.computeChildrenHeight(this._labels.length); + return this.computeChildrenHeight(this._clients.length); } - set labels(labels: string[]) { - this._labels = labels; + set clients(labels: Player[] | Spectator[]) { + this._clients = labels; if (this.isInitialized) { this.updateList(); } @@ -40,19 +41,19 @@ export class TitledListScreenElement extends ex.ScreenElement { let y = headingFont.size + spacingS; - for(const labelText of this._labels) { + for(const client of this._clients) { const label = new Text({ - text: labelText, + text: client.player.name, font: paragraphFont, }) label.pos.setTo(0, y); label.anchor.setTo(0.5, 0); y += label.font.size + spacingS; this.addChild(label); - this.labelsEntities.push(label); + this.entities.push(label); } - if (this._labels.length === 0) { + if (this._clients.length === 0) { const label = new Text({ text: "No one cares.", font: paragraphFont, @@ -60,7 +61,7 @@ export class TitledListScreenElement extends ex.ScreenElement { label.pos = ex.vec(0, y); label.anchor = ex.vec(0.5, 0); this.addChild(label); - this.labelsEntities.push(label); + this.entities.push(label); } } @@ -70,10 +71,10 @@ export class TitledListScreenElement extends ex.ScreenElement { } protected clearLabelEntities(): void { - for(const labelEntity of this.labelsEntities) { + for(const labelEntity of this.entities) { this.removeChild(labelEntity); } - this.labelsEntities = []; + this.entities = []; } diff --git a/src/frontend/src/ScreenElements/PlayerListScreenElement.ts b/src/frontend/src/ScreenElements/PlayerListScreenElement.ts index 8533548..a46327f 100644 --- a/src/frontend/src/ScreenElements/PlayerListScreenElement.ts +++ b/src/frontend/src/ScreenElements/PlayerListScreenElement.ts @@ -1,11 +1,13 @@ import * as ex from 'excalibur'; - -import { TitledListScreenElement } from "./TitledListScreenElement"; import { spacingS } from '../utils/formattingUtils'; import { disabledFont, paragraphFont } from '../utils/fontUtil'; import { Text } from './Text'; +import { Checkbox } from './Checkbox'; +import { Player } from '../GameStateRaw'; +import { ClientListScreenElement } from './ClientListScreenElement'; -export class PlayerListScreenElement extends TitledListScreenElement { +export class PlayerListScreenElement extends ClientListScreenElement { + protected override _clients: Player[] = []; constructor(headingText: string, private _maxPlayers: number) { super(headingText); } @@ -15,21 +17,30 @@ export class PlayerListScreenElement extends TitledListScreenElement { let y = this.heading.font.size + spacingS; - const labels = this._labels.slice(0, this._maxPlayers); + const clients = this._clients.slice(0, this._maxPlayers); - for(const labelText of labels) { + for(const client of clients) { + const { name } = client.player; const label = new Text({ - text: labelText, + text: name, font: paragraphFont, - }) + }); label.pos.setTo(0, y); label.anchor.setTo(0.5, 0); - y += label.font.size + spacingS; this.addChild(label); - this.labelsEntities.push(label); + + const ready = new Checkbox({ checked: client.ready}); + const textWidth = label.font.measureTextWithoutCache(name ?? "").width + ready.pos = ex.vec( textWidth / 2 + spacingS, y); + this.addChild(ready); + + this.entities.push(label); + this.entities.push(ready); + + y += label.font.size + spacingS; } - for(let i = labels.length; i < this._maxPlayers; i++) { + for(let i = clients.length; i < this._maxPlayers; i++) { const label = new Text({ text: "Empty", font: disabledFont, @@ -38,7 +49,7 @@ export class PlayerListScreenElement extends TitledListScreenElement { label.anchor.setTo(0.5, 0); y += label.font.size + spacingS; this.addChild(label); - this.labelsEntities.push(label); + this.entities.push(label); } } diff --git a/src/frontend/src/main.ts b/src/frontend/src/main.ts index ab4ca60..43fe8fd 100644 --- a/src/frontend/src/main.ts +++ b/src/frontend/src/main.ts @@ -1,6 +1,11 @@ import * as ex from "excalibur"; import { LobbyScene } from "./Scenes/LobbyScene"; import { Connection } from "./Connection"; +import { GameStateRaw } from "./GameStateRaw"; +import { GameState } from "./GameState"; + +const skipJoinScreen = false; +export const maxPlayers = 2; (async () => { const connection = new Connection(); @@ -17,7 +22,22 @@ import { Connection } from "./Connection"; }); game.toggleDebug(); - game.start().then(async () => { - await game.goToScene("lobby"); - }); + await game.start(); + await game.goToScene("lobby"); + + if (skipJoinScreen) { + lobbyScene.setScreen("lobby"); + lobbyScene.updateFromGameState(dummyGameState()); + } })(); + +function dummyGameState(): GameState { + const you = { name: "Dubbel", id: 0}; + + const dummyState: GameStateRaw = { + players: [{player: { name: "Dubbel", id: 0}, ready: true }, { player: { name: "Yeah", id: 1 }, ready: false }], + spectators: [ { player: { name: "Dubbel", id: 2} }, { player: { name: "Cocksucker", id: 3 } }], + } + + return new GameState(dummyState, you); +} \ No newline at end of file diff --git a/src/frontend/src/utils/debugUtils.ts b/src/frontend/src/utils/debugUtils.ts index cdde0fc..5cb8c1f 100644 --- a/src/frontend/src/utils/debugUtils.ts +++ b/src/frontend/src/utils/debugUtils.ts @@ -1,4 +1,4 @@ -export type LogSource = "Connection"; +export type LogSource = "Connection" | "GameState"; export function log(source: LogSource, ...data: any[]): void { console.log(`${source}: `, ...data); diff --git a/src/frontend/src/utils/styledRectangles.ts b/src/frontend/src/utils/styledRectangles.ts new file mode 100644 index 0000000..51e829d --- /dev/null +++ b/src/frontend/src/utils/styledRectangles.ts @@ -0,0 +1,21 @@ +import { Color, GraphicsGroup, Rectangle, vec } from "excalibur"; +import { spacingS } from "./formattingUtils"; + +const lowerBarHeight = spacingS; +export const lowerBarHeightDown = spacingS / 2; + +/** Return a GraphicsGroup of a clicky button. */ +export function makeStyledRectangle(width: number, height: number, color: Color, down: boolean): GraphicsGroup { + const offsetYTop = !down ? 0 : lowerBarHeightDown; + + // The lower bar is the side of the rectangle on the bottom part that makes it look 3d. + const heightLowerBar = !down ? lowerBarHeight : lowerBarHeightDown; + const offsetYLowerBar = !down ? height : height + lowerBarHeightDown; + + return new GraphicsGroup({ + members: [ + { graphic: new Rectangle({ width, height, color, }), offset: vec(0, offsetYTop) }, + { graphic: new Rectangle({ width, height: heightLowerBar, color: color.darken(0.5), }), offset: vec(0, offsetYLowerBar) }, + ], + }); +}