From 77dcad10153506072323869ae47ea0f22d51442d Mon Sep 17 00:00:00 2001 From: Lucas Habersaat Date: Sun, 7 Sep 2025 19:30:18 +0200 Subject: [PATCH] Add a basic lobby (#3) - Switch to ExcaliburJS - Add lobby with background table, title, player list and spectator list - Add buttons that do nothing --- README.md | 6 +- src/frontend/index.html | 6 +- src/frontend/package-lock.json | 100 +--------- src/frontend/package.json | 2 +- src/frontend/public/favicon.png | Bin 830 -> 0 bytes src/frontend/public/style.css | 9 - src/frontend/src/Cups.ts | 27 +++ src/frontend/src/GameState.ts | 5 + src/frontend/src/Scenes/GameStateScene.ts | 6 + src/frontend/src/Scenes/LobbyScene.ts | 181 ++++++++++++++++++ src/frontend/src/ScreenElements/Button.ts | 106 ++++++++++ .../ScreenElements/PlayerListScreenElement.ts | 49 +++++ src/frontend/src/ScreenElements/Text.ts | 9 + .../ScreenElements/TitledListScreenElement.ts | 80 ++++++++ src/frontend/src/main.ts | 49 ++--- src/frontend/src/utils/fontUtil.ts | 43 +++++ src/frontend/src/utils/formattingUtils.ts | 3 + 17 files changed, 543 insertions(+), 138 deletions(-) delete mode 100644 src/frontend/public/favicon.png create mode 100644 src/frontend/src/Cups.ts create mode 100644 src/frontend/src/GameState.ts create mode 100644 src/frontend/src/Scenes/GameStateScene.ts create mode 100644 src/frontend/src/Scenes/LobbyScene.ts create mode 100644 src/frontend/src/ScreenElements/Button.ts create mode 100644 src/frontend/src/ScreenElements/PlayerListScreenElement.ts create mode 100644 src/frontend/src/ScreenElements/Text.ts create mode 100644 src/frontend/src/ScreenElements/TitledListScreenElement.ts create mode 100644 src/frontend/src/utils/fontUtil.ts create mode 100644 src/frontend/src/utils/formattingUtils.ts diff --git a/README.md b/README.md index deec950..cd80ba9 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -A beer pong auto battler with stupid people. \ No newline at end of file +A beer pong auto battler with stupid people. + +## Frontend + +To run the front-end, go into `src/frontend` and run: `npm install && npm run dev`. \ No newline at end of file diff --git a/src/frontend/index.html b/src/frontend/index.html index c618e96..520f0b7 100644 --- a/src/frontend/index.html +++ b/src/frontend/index.html @@ -2,16 +2,12 @@ - - PixiJS - Template + Auto Beer Pong -
-
-
diff --git a/src/frontend/package-lock.json b/src/frontend/package-lock.json index 15e852d..53fe3fd 100644 --- a/src/frontend/package-lock.json +++ b/src/frontend/package-lock.json @@ -8,7 +8,7 @@ "name": "frontend", "version": "0.0.0", "dependencies": { - "pixi.js": "^8.8.1" + "excalibur": "^0.30.3" }, "devDependencies": { "@eslint/js": "^9.21.0", @@ -708,12 +708,6 @@ "node": ">= 8" } }, - "node_modules/@pixi/colord": { - "version": "2.9.6", - "resolved": "https://registry.npmjs.org/@pixi/colord/-/colord-2.9.6.tgz", - "integrity": "sha512-nezytU2pw587fQstUu1AsJZDVEynjskwOL+kibwcdxsMBFqPsFFNA7xl0ii/gXuDi6M0xj3mfRJj8pBSc2jCfA==", - "license": "MIT" - }, "node_modules/@pkgr/core": { "version": "0.2.9", "resolved": "https://registry.npmjs.org/@pkgr/core/-/core-0.2.9.tgz", @@ -1007,18 +1001,6 @@ "win32" ] }, - "node_modules/@types/css-font-loading-module": { - "version": "0.0.12", - "resolved": "https://registry.npmjs.org/@types/css-font-loading-module/-/css-font-loading-module-0.0.12.tgz", - "integrity": "sha512-x2tZZYkSxXqWvTDgveSynfjq/T2HyiZHXb00j/+gy19yp70PHCizM48XFdjBCWH7eHBD0R5i/pw9yMBP/BH5uA==", - "license": "MIT" - }, - "node_modules/@types/earcut": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@types/earcut/-/earcut-3.0.0.tgz", - "integrity": "sha512-k/9fOUGO39yd2sCjrbAJvGDEQvRwRnQIZlBz43roGwUZo5SHAmyVvSFyaVVZkicRVCaDXPKlbxrUcBuJoSWunQ==", - "license": "MIT" - }, "node_modules/@types/estree": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz", @@ -1291,21 +1273,6 @@ "url": "https://opencollective.com/typescript-eslint" } }, - "node_modules/@webgpu/types": { - "version": "0.1.64", - "resolved": "https://registry.npmjs.org/@webgpu/types/-/types-0.1.64.tgz", - "integrity": "sha512-84kRIAGV46LJTlJZWxShiOrNL30A+9KokD7RB3dRCIqODFjodS5tCD5yyiZ8kIReGVZSDfA3XkkwyyOIF6K62A==", - "license": "BSD-3-Clause" - }, - "node_modules/@xmldom/xmldom": { - "version": "0.8.11", - "resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.8.11.tgz", - "integrity": "sha512-cQzWCtO6C8TQiYl1ruKNn2U6Ao4o4WBBcbL61yJl84x+j5sOWWFU9X7DpND8XZG3daDppSsigMdfAIl2upQBRw==", - "license": "MIT", - "engines": { - "node": ">=10.0.0" - } - }, "node_modules/acorn": { "version": "8.15.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz", @@ -1494,12 +1461,6 @@ "dev": true, "license": "MIT" }, - "node_modules/earcut": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/earcut/-/earcut-3.0.2.tgz", - "integrity": "sha512-X7hshQbLyMJ/3RPhyObLARM2sNxxmRALLKx1+NVFFnQ9gKzmCrxm9+uLIAdBcvc8FNLpctqlQ2V6AE92Ol9UDQ==", - "license": "ISC" - }, "node_modules/esbuild": { "version": "0.25.9", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.9.tgz", @@ -1757,11 +1718,11 @@ "node": ">=0.10.0" } }, - "node_modules/eventemitter3": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz", - "integrity": "sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==", - "license": "MIT" + "node_modules/excalibur": { + "version": "0.30.3", + "resolved": "https://registry.npmjs.org/excalibur/-/excalibur-0.30.3.tgz", + "integrity": "sha512-RUP3qQ6tFe9BJxvqh6p/I0B9rKMu+KGlHZruJyVOohiYUtCP0LsRgkVHxezvWtWsvCElVBL4PeCooE4KPlIldA==", + "license": "BSD-2-Clause" }, "node_modules/fast-deep-equal": { "version": "3.1.3", @@ -1910,15 +1871,6 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, - "node_modules/gifuct-js": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/gifuct-js/-/gifuct-js-2.1.2.tgz", - "integrity": "sha512-rI2asw77u0mGgwhV3qA+OEgYqaDn5UNqgs+Bx0FGwSpuqfYn+Ir6RQY5ENNQ8SbIiG/m5gVa7CD5RriO4f4Lsg==", - "license": "MIT", - "dependencies": { - "js-binary-schema-parser": "^2.0.3" - } - }, "node_modules/glob-parent": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", @@ -2039,18 +1991,6 @@ "dev": true, "license": "ISC" }, - "node_modules/ismobilejs": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/ismobilejs/-/ismobilejs-1.1.1.tgz", - "integrity": "sha512-VaFW53yt8QO61k2WJui0dHf4SlL8lxBofUuUmwBo0ljPk0Drz2TiuDW4jo3wDcv41qy/SxrJ+VAzJ/qYqsmzRw==", - "license": "MIT" - }, - "node_modules/js-binary-schema-parser": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/js-binary-schema-parser/-/js-binary-schema-parser-2.0.3.tgz", - "integrity": "sha512-xezGJmOb4lk/M1ZZLTR/jaBHQ4gG/lqQnJqdIv4721DMggsa1bDVlHXNeHYogaIEHD9vCRv0fcL4hMA+Coarkg==", - "license": "MIT" - }, "node_modules/js-yaml": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", @@ -2265,12 +2205,6 @@ "node": ">=6" } }, - "node_modules/parse-svg-path": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/parse-svg-path/-/parse-svg-path-0.1.2.tgz", - "integrity": "sha512-JyPSBnkTJ0AI8GGJLfMXvKq42cj5c006fnLz6fXy6zfoVjJizi8BNTpu8on8ziI1cKy9d9DGNuY17Ce7wuejpQ==", - "license": "MIT" - }, "node_modules/path-exists": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", @@ -2311,28 +2245,6 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, - "node_modules/pixi.js": { - "version": "8.12.0", - "resolved": "https://registry.npmjs.org/pixi.js/-/pixi.js-8.12.0.tgz", - "integrity": "sha512-or7vrH7WajLevu/JnGMdD80JaSpTlXfjwCLtzhg2BL60LWPf1pF0w08Qleiqr1Saj012gevguM//+6HzzVlnfA==", - "license": "MIT", - "dependencies": { - "@pixi/colord": "^2.9.6", - "@types/css-font-loading-module": "^0.0.12", - "@types/earcut": "^3.0.0", - "@webgpu/types": "^0.1.40", - "@xmldom/xmldom": "^0.8.10", - "earcut": "^3.0.2", - "eventemitter3": "^5.0.1", - "gifuct-js": "^2.1.2", - "ismobilejs": "^1.1.1", - "parse-svg-path": "^0.1.2" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/pixijs" - } - }, "node_modules/postcss": { "version": "8.5.6", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.6.tgz", diff --git a/src/frontend/package.json b/src/frontend/package.json index 18d9d55..3f471f0 100644 --- a/src/frontend/package.json +++ b/src/frontend/package.json @@ -10,7 +10,7 @@ "dev": "vite" }, "dependencies": { - "pixi.js": "^8.8.1" + "excalibur": "^0.30.3" }, "devDependencies": { "@eslint/js": "^9.21.0", diff --git a/src/frontend/public/favicon.png b/src/frontend/public/favicon.png deleted file mode 100644 index fdc13bcfc3634814b8d0520b62a3e5ecc1725948..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 830 zcmV-E1Ht@>P)<{*UCbGcAx zC?WQVU{5=n4c$3~PEgCLSP*87qgQm|S+br~^w1>ovk7V#3&JipH&+LG2RT}^YZWN@3K@V&Hf}e!ssns6|w)M#t)Hj+E7+OniZr8P|{q6)_h!QM61Ep^6zb( zAwpp(VhbBuGwbiWWt7N`>Qr-Uqi>vE-nb{^&CEEqdv1v4fWRD*$Q9m($pZvUhk)lu zMyZHmpTM{zOin(*TR#AjJR7O-tiCZA5+ukS&@GX*`rh1mrKut=o9op~jPgbn~HZCip);k~b9m{Zk_Q-@Dz= z0s5A|v~Y>|3qbb-DvA`bK>z>%07*qo IM6N<$f*Sa90ssI2 diff --git a/src/frontend/public/style.css b/src/frontend/public/style.css index f4524fd..aeff35c 100644 --- a/src/frontend/public/style.css +++ b/src/frontend/public/style.css @@ -4,12 +4,3 @@ body { color: rgba(255, 255, 255, 0.87); background-color: #000000; } - -#app { - width: 100%; - height: 100vh; - overflow: hidden; - display: flex; - justify-content: center; - align-items: center; -} diff --git a/src/frontend/src/Cups.ts b/src/frontend/src/Cups.ts new file mode 100644 index 0000000..103b97f --- /dev/null +++ b/src/frontend/src/Cups.ts @@ -0,0 +1,27 @@ +import * as ex from 'excalibur'; + +const radius = 20; +const margin = 2; + +export class Cups extends ex.Graphic { + protected _drawImage(context: ex.ExcaliburGraphicsContext, x: number, y: number): void { + y += radius; + for (let r = 4; r > 0; r--) { + x = -r * radius + radius; + for (let c = 0; c < r; c++) { + context.drawCircle(ex.vec(x, y), radius, ex.Color.Red, ex.Color.White, 2) + x += radius * 2 + margin; + } + + y += radius * 2 + margin; + } + } + + clone(): ex.Graphic { + return new Cups(); + } + + get computeHeight(): number { + return 4 * 2 * radius + 3 * margin; + } +} \ No newline at end of file diff --git a/src/frontend/src/GameState.ts b/src/frontend/src/GameState.ts new file mode 100644 index 0000000..d36eaf4 --- /dev/null +++ b/src/frontend/src/GameState.ts @@ -0,0 +1,5 @@ + +export interface GameState { + players: string[]; + spectators: string[]; +} \ No newline at end of file diff --git a/src/frontend/src/Scenes/GameStateScene.ts b/src/frontend/src/Scenes/GameStateScene.ts new file mode 100644 index 0000000..0a65975 --- /dev/null +++ b/src/frontend/src/Scenes/GameStateScene.ts @@ -0,0 +1,6 @@ +import * as ex from 'excalibur'; +import { GameState } from '../GameState'; + +export class GameStateScene extends ex.Scene { + public updateFromGameState(_engine: ex.Engine, _gameState: GameState): void { } +} \ No newline at end of file diff --git a/src/frontend/src/Scenes/LobbyScene.ts b/src/frontend/src/Scenes/LobbyScene.ts new file mode 100644 index 0000000..2cf7ee9 --- /dev/null +++ b/src/frontend/src/Scenes/LobbyScene.ts @@ -0,0 +1,181 @@ +import * as ex from 'excalibur'; +import { spacingL, spacingM, spacingS } from '../utils/formattingUtils'; +import { subTitleFont, titleFont } from '../utils/fontUtil'; +import { Cups } from '../Cups'; +import { GameState } from '../GameState'; +import { GameStateScene } from './GameStateScene'; +import { PlayerListScreenElement } from '../ScreenElements/PlayerListScreenElement'; +import { TitledListScreenElement } from '../ScreenElements/TitledListScreenElement'; +import { Text } from '../ScreenElements/Text'; +import { Button } from '../ScreenElements/Button'; + +const tableWidth = 400; +const maxPlayers = 2; + +export class LobbyScene extends GameStateScene { + private _table: ex.ScreenElement = null!; + private _cups: ex.ScreenElement = null!; + + private _bar1: ex.ScreenElement = null!; + private _bar2: ex.ScreenElement = null!; + private _bar3: ex.ScreenElement = null!; + + private _title: Text = null!; + private _subTitle: Text = null!; + + private _playerList: PlayerListScreenElement = null!; + private _spectatorsList: TitledListScreenElement = null!; + + private _joinSpectatorButton: Button = null!; + private _joinPlayerButton: Button = null!; + + onInitialize(engine: ex.Engine): void { + // Table background + this._table = new ex.ScreenElement({ + anchor: ex.vec(0.5, 0) + }); + this.add(this._table); + + const s = 10; + const points = [ + ex.vec(0, s), + ex.vec(s, 0), + ex.vec(tableWidth - s, 0), + ex.vec(tableWidth, s), + ex.vec(tableWidth, engine.screen.height), + ex.vec(0, engine.screen.height), + ]; + const tableBackground = new ex.Polygon({ + points, + color: ex.Color.fromHex("1971c2"), + lineWidth: 16, + strokeColor: ex.Color.White, + }); + this._table.graphics.add(tableBackground); + + this._bar1 = this._createBar(); + this._bar2 = this._createBar(); + this._bar3 = this._createBar(); + + // Cups + this._cups = new ex.ScreenElement({}) + this._cups.anchor.setTo(0.5, 0); + this._cups.graphics.add(new Cups()); + this.add(this._cups); + + // Title + this._title = new Text({ + text: "Auto Beer Pong", + font: titleFont, + }); + this._title.anchor.setTo(0.5, 0); + this.add(this._title); + + + // Sub title + this._subTitle = new Text({ + text: "An auto battler with stupid people.", + font: subTitleFont, + }); + + this._subTitle.anchor.setTo(0.5, 0); + this.add(this._subTitle); + + + this._playerList = new PlayerListScreenElement("Players", maxPlayers); + this._playerList.labels = ["Dubbel"]; + this._playerList.anchor.setTo(0.5, 0); + this.add(this._playerList); + + this._joinPlayerButton = new Button({ + text: "Join as player", + anchor: ex.vec(0.5, 0), + onClick: () => this._joinAsPlayer(), + }); + this.add(this._joinPlayerButton); + + + this._spectatorsList = new TitledListScreenElement("Spectators"); + this._spectatorsList.labels = ["Dubbel", "Cocksucker"]; + this._spectatorsList.anchor.setTo(0.5, 0); + this.add(this._spectatorsList); + + + this._joinSpectatorButton = new Button({ + text: "Join as spectator", + anchor: ex.vec(0.5, 0), + onClick: () => this._joinAsSpectator(), + }); + this.add(this._joinSpectatorButton); + + this._updatePositions(engine.screen); + } + + private _createBar(): ex.ScreenElement { + const bar = new ex.ScreenElement({ + anchor: ex.vec(0.5, 0), + }) + bar.graphics.add(new ex.Rectangle({ + width: tableWidth - 2 * spacingM, + height: 5, + color: ex.Color.White, + })); + this.add(bar); + return bar; + } + + private _updatePositions(screen: ex.Screen): void { + let y = spacingM; + const halfWidth = screen.width / 2; + + this._table.pos = ex.vec(halfWidth, y); + y += spacingM; + + this._cups.pos = ex.vec(halfWidth, y); + const cups = this._cups.graphics.getGraphic('default') as Cups; + y += cups.computeHeight! + spacingM; + + this._bar1.pos = ex.vec(halfWidth, y); + y += this._bar1.height + spacingM; + + this._title.pos = ex.vec(halfWidth, y); + y += this._title.font.size + spacingM; + + this._subTitle.pos = ex.vec(halfWidth, y); + y += this._subTitle.font.size + spacingM; + + this._bar2.pos = ex.vec(halfWidth, y); + y += this._bar2.height + spacingL; + + this._playerList.pos = ex.vec(halfWidth, y); + y += this._playerList.childrenHeight + spacingL; + + this._joinPlayerButton.pos = ex.vec(halfWidth, y); + y += this._joinPlayerButton.height + spacingL; + + this._bar3.pos = ex.vec(halfWidth, y); + y += this._bar3.height + spacingL; + + + this._spectatorsList.pos = ex.vec(halfWidth, y); + y += this._spectatorsList.childrenHeight + spacingL; + + this._joinSpectatorButton.pos = ex.vec(halfWidth, y); + y += this._spectatorsList.childrenHeight + spacingL; + + } + + public updateFromGameState(engine: ex.Engine, gameState: GameState): void { + this._playerList.labels = gameState.players; + this._spectatorsList.labels = gameState.spectators; + this._updatePositions(engine.screen); + } + + private _joinAsPlayer(): void { + // Send request + } + + private _joinAsSpectator(): void { + // Send request + } +} \ No newline at end of file diff --git a/src/frontend/src/ScreenElements/Button.ts b/src/frontend/src/ScreenElements/Button.ts new file mode 100644 index 0000000..5aa4be0 --- /dev/null +++ b/src/frontend/src/ScreenElements/Button.ts @@ -0,0 +1,106 @@ +import * as ex from 'excalibur'; +import { Text } from './Text'; +import { paragraphFont } from '../utils/fontUtil'; +import { spacingS } from '../utils/formattingUtils'; + +interface ButtonProperties { + text?: string; + disabled?: boolean; + onClick?: (event: ex.PointerEvent) => void; +} +type ConstructProperties = ex.ActorArgs & ButtonProperties; + +export class Button extends ex.ScreenElement { + private _text?: string; + private _textElement: Text = null!; + + private _disabled: boolean; + + private _onClick?: (event: ex.PointerEvent) => void; + + constructor(config?: ConstructProperties) { + super(config); + this._text = config?.text; + this._disabled = config?.disabled ?? false; + this._onClick = config?.onClick; + } + + onInitialize(_engine: ex.Engine): void { + const margin = spacingS * 2; + const width = paragraphFont.measureTextWithoutCache(this.text).width + margin; + const height = paragraphFont.size + margin; + + this._textElement = new Text({ + text: this._text, + font: paragraphFont, + }) + this._textElement.anchor = ex.vec(0.5, 0.5); + 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"), + })); + + this.graphics.add('hover', new ex.Rectangle({ + width, + height, + color: ex.Color.fromHex("bad2ff") + })); + this.graphics.use('idle'); + + this.graphics.add('disabled', new ex.Rectangle({ + width, + height, + color: ex.Color.Gray, + })); + this.graphics.use('idle'); + + this.on('pointerup', (e) => this.onPointerUp(e)); + this.on('pointerenter', (e) => this.onPointerEnter(e)); + this.on('pointerleave', (e) => this.onPointerLeave(e)); + } + + get text(): string { + return this._text ?? ""; + } + + get disabled(): boolean { + return this._disabled; + } + + set disabled(value: boolean) { + this._disabled = value; + this.graphics.use(value ? 'disabled' : 'idle') + } + + onPointerUp(event: ex.PointerEvent): void { + if (this._disabled) { + return; + } + + const { _onClick } = this; + if (_onClick != null) { + _onClick(event); + } + } + + onPointerEnter(_event: ex.PointerEvent): void { + if (this._disabled) { + return; + } + + this.graphics.use('hover'); + } + + onPointerLeave(_event: ex.PointerEvent): void { + if (this._disabled) { + return; + } + + this.graphics.use('idle'); + } + +} \ No newline at end of file diff --git a/src/frontend/src/ScreenElements/PlayerListScreenElement.ts b/src/frontend/src/ScreenElements/PlayerListScreenElement.ts new file mode 100644 index 0000000..8533548 --- /dev/null +++ b/src/frontend/src/ScreenElements/PlayerListScreenElement.ts @@ -0,0 +1,49 @@ +import * as ex from 'excalibur'; + +import { TitledListScreenElement } from "./TitledListScreenElement"; +import { spacingS } from '../utils/formattingUtils'; +import { disabledFont, paragraphFont } from '../utils/fontUtil'; +import { Text } from './Text'; + +export class PlayerListScreenElement extends TitledListScreenElement { + constructor(headingText: string, private _maxPlayers: number) { + super(headingText); + } + + protected override updateList(): void { + this.clearLabelEntities(); + + let y = this.heading.font.size + spacingS; + + const labels = this._labels.slice(0, this._maxPlayers); + + for(const labelText of labels) { + const label = new Text({ + text: labelText, + 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); + } + + for(let i = labels.length; i < this._maxPlayers; i++) { + const label = new Text({ + text: "Empty", + font: disabledFont, + }) + label.pos.setTo(0, y); + label.anchor.setTo(0.5, 0); + y += label.font.size + spacingS; + this.addChild(label); + this.labelsEntities.push(label); + + } + } + + override get childrenHeight(): number { + return this.computeChildrenHeight(this._maxPlayers); + } +} \ No newline at end of file diff --git a/src/frontend/src/ScreenElements/Text.ts b/src/frontend/src/ScreenElements/Text.ts new file mode 100644 index 0000000..62df2a0 --- /dev/null +++ b/src/frontend/src/ScreenElements/Text.ts @@ -0,0 +1,9 @@ +import * as ex from 'excalibur'; + +type ConstructProperties = ex.LabelOptions & ex.ActorArgs; + +export class Text extends ex.Label { + constructor(options?: ConstructProperties) { + super(options); + } +} \ No newline at end of file diff --git a/src/frontend/src/ScreenElements/TitledListScreenElement.ts b/src/frontend/src/ScreenElements/TitledListScreenElement.ts new file mode 100644 index 0000000..0e58bc7 --- /dev/null +++ b/src/frontend/src/ScreenElements/TitledListScreenElement.ts @@ -0,0 +1,80 @@ +import * as ex from 'excalibur'; + +import { spacingS } from '../utils/formattingUtils'; +import { headingFont, paragraphFont } from '../utils/fontUtil'; +import { Text } from './Text'; + +export class TitledListScreenElement extends ex.ScreenElement { + protected _labels: string[] = []; + protected heading: Text = null!; + protected labelsEntities: Text[] = []; + + constructor(private _headingText: string) { + super(); + } + + override onInitialize(_engine: ex.Engine): void { + this.heading = new Text({ + text: this._headingText, + font: headingFont + }); + this.heading.anchor.setTo(0.5, 0); + this.addChild(this.heading); + + this.updateList(); + } + + get childrenHeight(): number { + return this.computeChildrenHeight(this._labels.length); + } + + set labels(labels: string[]) { + this._labels = labels; + if (this.isInitialized) { + this.updateList(); + } + } + + protected updateList(): void { + this.clearLabelEntities(); + + let y = headingFont.size + spacingS; + + for(const labelText of this._labels) { + const label = new Text({ + text: labelText, + 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); + } + + if (this._labels.length === 0) { + const label = new Text({ + text: "No one cares.", + font: paragraphFont, + }); + label.pos = ex.vec(0, y); + label.anchor = ex.vec(0.5, 0); + this.addChild(label); + this.labelsEntities.push(label); + } + + } + + protected computeChildrenHeight(numLabels: number): number { + return headingFont.size + spacingS + numLabels * (spacingS + paragraphFont.size); + } + + protected clearLabelEntities(): void { + for(const labelEntity of this.labelsEntities) { + this.removeChild(labelEntity); + } + this.labelsEntities = []; + } + + +} \ No newline at end of file diff --git a/src/frontend/src/main.ts b/src/frontend/src/main.ts index 083ac19..011319b 100644 --- a/src/frontend/src/main.ts +++ b/src/frontend/src/main.ts @@ -1,35 +1,28 @@ -import { Application, Assets, Sprite } from "pixi.js"; +import * as ex from 'excalibur'; +import { LobbyScene } from "./Scenes/LobbyScene"; (async () => { - // Create a new application - const app = new Application(); + const game = new ex.Engine({ + width: 0, + height: 0, + displayMode: ex.DisplayMode.FillScreen, + scenes: { lobby: LobbyScene}, + backgroundColor: ex.Color.fromHex("23213b"), + }); + game.toggleDebug(); - // Initialize the application - await app.init({ background: "#1099bb", resizeTo: window }); + game.start().then(async () => { + await game.goToScene('lobby'); - // Append the application canvas to the document body - document.getElementById("pixi-container")!.appendChild(app.canvas); + // Test update + const gameState = { + players: ["LoL"], + spectators: [], + } - // Load the bunny texture - const texture = await Assets.load("/assets/bunny.png"); - - // Create a bunny Sprite - const bunny = new Sprite(texture); - - // Center the sprite's anchor point - bunny.anchor.set(0.5); - - // Move the sprite to the center of the screen - bunny.position.set(app.screen.width / 2, app.screen.height / 2); - - // Add the bunny to the stage - app.stage.addChild(bunny); - - // Listen for animate update - app.ticker.add((time) => { - // Just for fun, let's rotate mr rabbit a little. - // * Delta is 1 if running at 100% performance * - // * Creates frame-independent transformation * - bunny.rotation += 0.1 * time.deltaTime; + const lobbyScene = game.currentScene as LobbyScene; + setTimeout(() => { + lobbyScene.updateFromGameState(game, gameState); + }, 2000); }); })(); diff --git a/src/frontend/src/utils/fontUtil.ts b/src/frontend/src/utils/fontUtil.ts new file mode 100644 index 0000000..74a0154 --- /dev/null +++ b/src/frontend/src/utils/fontUtil.ts @@ -0,0 +1,43 @@ +import * as ex from 'excalibur'; + +const fontFamily = "impact"; +const shadow = { + color: ex.Color.Black, + offset: ex.vec(4, 4), +} + +export const paragraphFont = new ex.Font({ + family: fontFamily, + color: ex.Color.White, + size: 20, + shadow, +}); + +export const headingFont = new ex.Font({ + family: fontFamily, + color: ex.Color.White, + size: 24, + shadow, +}); + +export const subTitleFont = new ex.Font({ + family: fontFamily, + color: ex.Color.White, + size: 20, + shadow, +}); + +export const titleFont = new ex.Font({ + family: fontFamily, + color: ex.Color.White, + size: 48, + shadow, +}); + +export const disabledFont = new ex.Font({ + family: fontFamily, + color: ex.Color.Gray, + size: 20, + shadow, + +}) \ No newline at end of file diff --git a/src/frontend/src/utils/formattingUtils.ts b/src/frontend/src/utils/formattingUtils.ts new file mode 100644 index 0000000..442b683 --- /dev/null +++ b/src/frontend/src/utils/formattingUtils.ts @@ -0,0 +1,3 @@ +export const spacingL = 40; +export const spacingM = 20; +export const spacingS = 5; \ No newline at end of file