Frontend - Lint shit and add github action (#21)
This commit is contained in:
1 parent
f938c4ebdf
commit
f2ae34e490
22 files changed
+1249
-1108
No files matched your search
@@ -0,0 +1,18 @@
|
||||
name: CI
|
||||
on: push
|
||||
jobs:
|
||||
lint_frontend:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 20
|
||||
- name: Install
|
||||
run: |
|
||||
cd src/frontend
|
||||
npm ci
|
||||
- name: Run ESLint
|
||||
run: |
|
||||
cd src/frontend
|
||||
npm run lint
|
||||
+132
-131
@@ -1,186 +1,187 @@
|
||||
import { GameState } from './GameState';
|
||||
import { Client, GameStateRaw, Id } from './GameStateRaw';
|
||||
import { log } from './utils/debugUtils';
|
||||
import { Event, Evented } from './utils/Evented';
|
||||
import { GameState } from "./GameState";
|
||||
import { Client, GameStateRaw, Id } from "./GameStateRaw";
|
||||
import { log } from "./utils/debugUtils";
|
||||
import { Event, Evented } from "./utils/Evented";
|
||||
|
||||
type Action = "join" | "rejoin" | "toggleRole" | "toggleReady";
|
||||
|
||||
interface Request {
|
||||
action: Action,
|
||||
action: Action;
|
||||
}
|
||||
|
||||
interface JoinRequest {
|
||||
action: "join",
|
||||
name: string,
|
||||
action: "join";
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface RejoinRequest {
|
||||
action: "rejoin",
|
||||
id: Id,
|
||||
action: "rejoin";
|
||||
id: Id;
|
||||
}
|
||||
|
||||
interface ToggleRoleRequest {
|
||||
action: "toggleRole"
|
||||
action: "toggleRole";
|
||||
}
|
||||
|
||||
interface ToggleReadyRequest {
|
||||
action: "toggleReady"
|
||||
action: "toggleReady";
|
||||
}
|
||||
|
||||
export class GameMessageEvent extends Event {
|
||||
constructor(public gameState: GameState) {
|
||||
super();
|
||||
}
|
||||
constructor(public gameState: GameState) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export class ConnectEvent extends Event { }
|
||||
export class DisconnectEvent extends Event { }
|
||||
export class JoinedEvent 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();
|
||||
}
|
||||
constructor(public connected: boolean) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export const ConnectionEvents = {
|
||||
"game-message": GameMessageEvent,
|
||||
"joined": JoinedEvent,
|
||||
"connect": ConnectEvent,
|
||||
"disconnect": DisconnectEvent,
|
||||
"connect-or-disconnect": ConnectOrDisconnectEvent,
|
||||
"game-message": GameMessageEvent,
|
||||
joined: JoinedEvent,
|
||||
connect: ConnectEvent,
|
||||
disconnect: DisconnectEvent,
|
||||
"connect-or-disconnect": ConnectOrDisconnectEvent,
|
||||
} as const;
|
||||
|
||||
export class Connection extends Evented<typeof ConnectionEvents> {
|
||||
private _socket: WebSocket | null = null;
|
||||
private _connected = false;
|
||||
private _socket: WebSocket | null = null;
|
||||
private _connected = false;
|
||||
|
||||
private _waitForJoin = false;
|
||||
private _client : Client | null = null;
|
||||
private _waitForJoin = false;
|
||||
private _client: Client | null = null;
|
||||
|
||||
get connected(): boolean {
|
||||
const { _socket } = this;
|
||||
return _socket !== null && _socket.readyState === _socket.OPEN;
|
||||
get connected(): boolean {
|
||||
const { _socket } = this;
|
||||
return _socket !== null && _socket.readyState === _socket.OPEN;
|
||||
}
|
||||
|
||||
connect(): void {
|
||||
this._socket = new WebSocket("ws://127.0.0.1:8888");
|
||||
|
||||
this._socket.onopen = () => this._onConnect();
|
||||
this._socket.onmessage = (event) => this._onMessage(event);
|
||||
this._socket.onerror = (event) => this._onError(event);
|
||||
this._socket.onclose = () => this._onDisconnect();
|
||||
}
|
||||
|
||||
private _onMessage(event: MessageEvent): void {
|
||||
log("Connection", "received", event);
|
||||
|
||||
const messageObject = JSON.parse(event.data);
|
||||
|
||||
if (this._waitForJoin) {
|
||||
this._client = messageObject.player as Client;
|
||||
storeClient(this._client);
|
||||
this._waitForJoin = false;
|
||||
this.emit("joined", new JoinedEvent());
|
||||
return;
|
||||
}
|
||||
|
||||
connect(): void {
|
||||
this._socket = new WebSocket("ws://127.0.0.1:8888");
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
this._socket.onopen = () => this._onConnect();
|
||||
this._socket.onmessage = (event) => this._onMessage(event);
|
||||
this._socket.onerror = (event) => this._onError(event);
|
||||
this._socket.onclose = () => this._onDisconnect();
|
||||
private _onConnect(): void {
|
||||
log("Connection", "connected");
|
||||
|
||||
this._connected = true;
|
||||
|
||||
this.emit("connect", new ConnectEvent());
|
||||
this.emit("connect-or-disconnect", new ConnectOrDisconnectEvent(true));
|
||||
}
|
||||
|
||||
private _onDisconnect(): void {
|
||||
log("Connection", "disconnected");
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
private _onMessage(event: MessageEvent): void {
|
||||
log("Connection", "received", event)
|
||||
// Try again
|
||||
this.connect();
|
||||
}
|
||||
|
||||
const messageObject = JSON.parse(event.data);
|
||||
private _onError(event: globalThis.Event): void {
|
||||
log("Connection", "error", event);
|
||||
}
|
||||
|
||||
if (this._waitForJoin) {
|
||||
this._client = messageObject.player as Client;
|
||||
storeClient(this._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 _send(request: Request): void {
|
||||
if (!this.connected) {
|
||||
return;
|
||||
}
|
||||
|
||||
private _onConnect(): void {
|
||||
log("Connection", "connected");
|
||||
const message = JSON.stringify(request);
|
||||
this._socket!.send(message);
|
||||
}
|
||||
|
||||
this._connected = true;
|
||||
join(name: string): void {
|
||||
const joinRequest = {
|
||||
action: "join",
|
||||
name,
|
||||
} as JoinRequest;
|
||||
|
||||
this.emit("connect", new ConnectEvent());
|
||||
this.emit("connect-or-disconnect", new ConnectOrDisconnectEvent(true));
|
||||
}
|
||||
this._waitForJoin = true;
|
||||
this._send(joinRequest);
|
||||
}
|
||||
|
||||
private _onDisconnect(): void {
|
||||
log("Connection", "disconnected");
|
||||
rejoin(id: Id): void {
|
||||
const rejoinRequest = {
|
||||
action: "rejoin",
|
||||
id,
|
||||
} as RejoinRequest;
|
||||
|
||||
// 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));
|
||||
}
|
||||
this._waitForJoin = true;
|
||||
this._send(rejoinRequest);
|
||||
}
|
||||
|
||||
// Try again
|
||||
this.connect();
|
||||
}
|
||||
toggleRole(): void {
|
||||
const toggleRoleRequest = {
|
||||
action: "toggleRole",
|
||||
} as ToggleRoleRequest;
|
||||
|
||||
private _onError(event: globalThis.Event): void {
|
||||
log("Connection", "error", event);
|
||||
}
|
||||
this._send(toggleRoleRequest);
|
||||
}
|
||||
|
||||
private _send(request: Request): void {
|
||||
if (!this.connected) {
|
||||
return;
|
||||
}
|
||||
toggleReady(): void {
|
||||
const toggleReadyRequest = {
|
||||
action: "toggleReady",
|
||||
} as ToggleReadyRequest;
|
||||
|
||||
const message = JSON.stringify(request);
|
||||
this._socket!.send(message);
|
||||
}
|
||||
|
||||
join(name: string): void {
|
||||
const joinRequest = {
|
||||
action: "join",
|
||||
name,
|
||||
} as JoinRequest;
|
||||
|
||||
this._waitForJoin = true;
|
||||
this._send(joinRequest);
|
||||
}
|
||||
|
||||
rejoin(id: Id): void {
|
||||
const rejoinRequest = {
|
||||
action: "rejoin",
|
||||
id,
|
||||
} as RejoinRequest;
|
||||
|
||||
this._waitForJoin = true;
|
||||
this._send(rejoinRequest);
|
||||
}
|
||||
|
||||
toggleRole(): void {
|
||||
const toggleRoleRequest = {
|
||||
action: "toggleRole"
|
||||
} as ToggleRoleRequest;
|
||||
|
||||
this._send(toggleRoleRequest)
|
||||
}
|
||||
|
||||
toggleReady(): void {
|
||||
const toggleReadyRequest = {
|
||||
action: "toggleReady"
|
||||
} as ToggleReadyRequest;
|
||||
|
||||
this._send(toggleReadyRequest)
|
||||
}
|
||||
this._send(toggleReadyRequest);
|
||||
}
|
||||
}
|
||||
|
||||
export function storedClient(): Client | null {
|
||||
const clientRaw = localStorage.getItem("client");
|
||||
if (clientRaw == null) {
|
||||
return null;
|
||||
}
|
||||
const clientRaw = localStorage.getItem("client");
|
||||
if (clientRaw == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// TODO: Do schema check.
|
||||
return JSON.parse(clientRaw) as Client;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
// TODO: Do schema check.
|
||||
return JSON.parse(clientRaw) as Client;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function storeClient(client: Client) {
|
||||
localStorage.setItem("client", JSON.stringify(client));
|
||||
localStorage.setItem("client", JSON.stringify(client));
|
||||
}
|
||||
+27
-17
@@ -1,27 +1,37 @@
|
||||
import * as ex from 'excalibur';
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
y += radius * 2 + margin;
|
||||
}
|
||||
}
|
||||
|
||||
clone(): ex.Graphic {
|
||||
return new Cups();
|
||||
}
|
||||
clone(): ex.Graphic {
|
||||
return new Cups();
|
||||
}
|
||||
|
||||
get computeHeight(): number {
|
||||
return 4 * 2 * radius + 3 * margin;
|
||||
}
|
||||
get computeHeight(): number {
|
||||
return 4 * 2 * radius + 3 * margin;
|
||||
}
|
||||
}
|
||||
@@ -2,44 +2,45 @@ import { ScreenElement, vec } from "excalibur";
|
||||
import { ErrorScreenElement } from "../ScreenElements/Error";
|
||||
import { spacingS } from "../utils/formattingUtils";
|
||||
|
||||
|
||||
interface ErrorListProperties {
|
||||
width: number;
|
||||
width: number;
|
||||
}
|
||||
|
||||
type ConstructProperties = Partial<ex.ActorArgs> & ErrorListProperties;
|
||||
|
||||
export class ErrorList extends ScreenElement {
|
||||
private _errors: ErrorScreenElement[] = [];
|
||||
private _width: number;
|
||||
private _errors: ErrorScreenElement[] = [];
|
||||
private _width: number;
|
||||
|
||||
constructor(config: ConstructProperties){
|
||||
super(config);
|
||||
this._width = config.width;
|
||||
}
|
||||
constructor(config: ConstructProperties) {
|
||||
super(config);
|
||||
this._width = config.width;
|
||||
}
|
||||
|
||||
public addError(text: string): () => void {
|
||||
const index = this._errors.length;
|
||||
public addError(text: string): () => void {
|
||||
const index = this._errors.length;
|
||||
|
||||
const errorScreenElement = new ErrorScreenElement({
|
||||
text,
|
||||
width: this._width,
|
||||
})
|
||||
this.addChild(errorScreenElement);
|
||||
this._errors.push(errorScreenElement);
|
||||
const errorScreenElement = new ErrorScreenElement({
|
||||
text,
|
||||
width: this._width,
|
||||
});
|
||||
this.addChild(errorScreenElement);
|
||||
this._errors.push(errorScreenElement);
|
||||
|
||||
const y = this._errors.reduce((acc, cur) => acc + cur.height, 0) + index * this._padding;
|
||||
errorScreenElement.pos = vec(0, -y);
|
||||
const y =
|
||||
this._errors.reduce((acc, cur) => acc + cur.height, 0) +
|
||||
index * this._padding;
|
||||
errorScreenElement.pos = vec(0, -y);
|
||||
|
||||
const removeHandle = () => {
|
||||
this.removeChild(errorScreenElement);
|
||||
this._errors.splice(index, 1);
|
||||
};
|
||||
const removeHandle = () => {
|
||||
this.removeChild(errorScreenElement);
|
||||
this._errors.splice(index, 1);
|
||||
};
|
||||
|
||||
return removeHandle;
|
||||
}
|
||||
return removeHandle;
|
||||
}
|
||||
|
||||
private get _padding(): number {
|
||||
return spacingS;
|
||||
}
|
||||
private get _padding(): number {
|
||||
return spacingS;
|
||||
}
|
||||
}
|
||||
@@ -1,101 +1,108 @@
|
||||
import * as ex from 'excalibur';
|
||||
import { TextInput, TextInputEvents } from '../ScreenElements/TextInput';
|
||||
import { Button } from '../ScreenElements/Button';
|
||||
import { paragraphFont } from '../utils/fontUtil';
|
||||
import { Text } from '../ScreenElements/Text';
|
||||
import { spacingM } from '../utils/formattingUtils';
|
||||
import { Connection, storedClient } from '../Connection';
|
||||
import { Id } from '../GameStateRaw';
|
||||
import * as ex from "excalibur";
|
||||
import { TextInput, TextInputEvents } from "../ScreenElements/TextInput";
|
||||
import { Button } from "../ScreenElements/Button";
|
||||
import { paragraphFont } from "../utils/fontUtil";
|
||||
import { Text } from "../ScreenElements/Text";
|
||||
import { spacingM } from "../utils/formattingUtils";
|
||||
import { Connection, storedClient } from "../Connection";
|
||||
import { Id } from "../GameStateRaw";
|
||||
|
||||
export class PlayerNameInput extends ex.ScreenElement {
|
||||
private _nameLabel: Text = null!;
|
||||
private _nameInput: TextInput = null!;
|
||||
private _joinButton: Button = null!;
|
||||
private _rejoinButton: Button | null = null;
|
||||
private _nameLabel: Text = null!;
|
||||
private _nameInput: TextInput = null!;
|
||||
private _joinButton: Button = null!;
|
||||
private _rejoinButton: Button | null = null;
|
||||
|
||||
private _height = 0;
|
||||
private _height = 0;
|
||||
|
||||
constructor(private _connection: Connection, private _joinCallback: (name: string) => void, private _rejoinCallback: (id: Id) => void) {
|
||||
super();
|
||||
constructor(
|
||||
private _connection: Connection,
|
||||
private _joinCallback: (name: string) => void,
|
||||
private _rejoinCallback: (id: Id) => void,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onInitialize(_engine: ex.Engine): void {
|
||||
this._nameLabel = new Text({
|
||||
text: "What are you called?",
|
||||
font: paragraphFont,
|
||||
});
|
||||
this._nameLabel.anchor = ex.vec(0.5, 0);
|
||||
this.addChild(this._nameLabel);
|
||||
|
||||
this._nameInput = new TextInput({ text: "", width: 300 });
|
||||
this._nameInput.anchor = ex.vec(0.5, 0);
|
||||
this.addChild(this._nameInput);
|
||||
|
||||
this._joinButton = new Button({
|
||||
text: "Join",
|
||||
anchor: ex.vec(0.5, 0),
|
||||
onClick: () => this._joinCallback(this._nameInput.text),
|
||||
disabled: true,
|
||||
});
|
||||
this.addChild(this._joinButton);
|
||||
|
||||
const rejoinClient = storedClient();
|
||||
if (rejoinClient != null) {
|
||||
this._rejoinButton = new Button({
|
||||
text: "Rejoin as " + rejoinClient.name,
|
||||
anchor: ex.vec(0.5, 0),
|
||||
onClick: () => this._rejoinCallback(rejoinClient.id),
|
||||
});
|
||||
|
||||
this.addChild(this._rejoinButton);
|
||||
}
|
||||
|
||||
onInitialize(_engine: ex.Engine): void {
|
||||
this._nameLabel = new Text({
|
||||
text: "What are you called?",
|
||||
font: paragraphFont,
|
||||
})
|
||||
this._nameLabel.anchor = ex.vec(0.5, 0);
|
||||
this.addChild(this._nameLabel);
|
||||
this._nameInput.on(TextInputEvents.Textchanged, () =>
|
||||
this._updateJoinButtonDisabled(),
|
||||
);
|
||||
|
||||
this._nameInput = new TextInput({ text: "", width: 300 });
|
||||
this._nameInput.anchor = ex.vec(0.5, 0);
|
||||
this.addChild(this._nameInput);
|
||||
let y = 0;
|
||||
this._nameLabel.pos = ex.vec(0, y);
|
||||
y += this._nameLabel.font.size + spacingM;
|
||||
|
||||
this._joinButton = new Button({
|
||||
text: "Join",
|
||||
anchor: ex.vec(0.5, 0),
|
||||
onClick: () => this._joinCallback(this._nameInput.text),
|
||||
disabled: true,
|
||||
});
|
||||
this.addChild(this._joinButton);
|
||||
this._nameInput.pos = ex.vec(0, y);
|
||||
y += this._nameInput.height + spacingM;
|
||||
|
||||
const rejoinClient = storedClient();
|
||||
if (rejoinClient != null) {
|
||||
this._rejoinButton = new Button({
|
||||
text: "Rejoin as " + rejoinClient.name,
|
||||
anchor: ex.vec(0.5, 0),
|
||||
onClick: () => this._rejoinCallback(rejoinClient.id),
|
||||
});
|
||||
this._joinButton.pos = ex.vec(0, y);
|
||||
y += this._joinButton.height + spacingM;
|
||||
|
||||
this.addChild(this._rejoinButton);
|
||||
}
|
||||
|
||||
this._nameInput.on(TextInputEvents.Textchanged, () => this._updateJoinButtonDisabled());
|
||||
|
||||
let y = 0;
|
||||
this._nameLabel.pos = ex.vec(0, y);
|
||||
y += this._nameLabel.font.size + spacingM;
|
||||
|
||||
this._nameInput.pos = ex.vec(0, y);
|
||||
y += this._nameInput.height + spacingM;
|
||||
|
||||
this._joinButton.pos = ex.vec(0, y);
|
||||
y += this._joinButton.height + spacingM;
|
||||
|
||||
if (this._rejoinButton != null) {
|
||||
this._rejoinButton.pos = ex.vec(0, y);
|
||||
y += this._rejoinButton.height + spacingM;
|
||||
}
|
||||
|
||||
this._height = y;
|
||||
|
||||
this._connection.on("connect",() => this._onConnect())
|
||||
this._connection.on("disconnect",() => this._onDisconnect())
|
||||
if (this._rejoinButton != null) {
|
||||
this._rejoinButton.pos = ex.vec(0, y);
|
||||
y += this._rejoinButton.height + spacingM;
|
||||
}
|
||||
|
||||
get playerName(): string {
|
||||
return this._nameInput.text;
|
||||
}
|
||||
this._height = y;
|
||||
|
||||
override get height(): number {
|
||||
return this._height;
|
||||
}
|
||||
this._connection.on("connect", () => this._onConnect());
|
||||
this._connection.on("disconnect", () => this._onDisconnect());
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this._nameInput.text = "";
|
||||
this._updateJoinButtonDisabled();
|
||||
}
|
||||
get playerName(): string {
|
||||
return this._nameInput.text;
|
||||
}
|
||||
|
||||
private _onConnect(): void {
|
||||
this._updateJoinButtonDisabled();
|
||||
}
|
||||
override get height(): number {
|
||||
return this._height;
|
||||
}
|
||||
|
||||
private _onDisconnect(): void {
|
||||
this._updateJoinButtonDisabled();
|
||||
}
|
||||
clear(): void {
|
||||
this._nameInput.text = "";
|
||||
this._updateJoinButtonDisabled();
|
||||
}
|
||||
|
||||
private _updateJoinButtonDisabled(): void {
|
||||
const disabled = !this._connection.connected || this._nameInput.text === "";
|
||||
this._joinButton.disabled = disabled;
|
||||
}
|
||||
private _onConnect(): void {
|
||||
this._updateJoinButtonDisabled();
|
||||
}
|
||||
|
||||
private _onDisconnect(): void {
|
||||
this._updateJoinButtonDisabled();
|
||||
}
|
||||
|
||||
private _updateJoinButtonDisabled(): void {
|
||||
const disabled = !this._connection.connected || this._nameInput.text === "";
|
||||
this._joinButton.disabled = disabled;
|
||||
}
|
||||
}
|
||||
@@ -4,34 +4,34 @@ import { subTitleFont, titleFont } from "../utils/fontUtil";
|
||||
import { spacingM } from "../utils/formattingUtils";
|
||||
|
||||
export class Title extends ScreenElement {
|
||||
private _title: Text = null!;
|
||||
private _subTitle: Text = null!;
|
||||
private _title: Text = null!;
|
||||
private _subTitle: Text = null!;
|
||||
|
||||
onInitialize(_engine: Engine): void {
|
||||
// Title
|
||||
this._title = new Text({
|
||||
text: "Auto Beer Pong",
|
||||
font: titleFont,
|
||||
});
|
||||
this._title.anchor = vec(0.5, 0);
|
||||
this.addChild(this._title);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onInitialize(_engine: Engine): void {
|
||||
// Title
|
||||
this._title = new Text({
|
||||
text: "Auto Beer Pong",
|
||||
font: titleFont,
|
||||
});
|
||||
this._title.anchor = vec(0.5, 0);
|
||||
this.addChild(this._title);
|
||||
|
||||
// Sub title
|
||||
this._subTitle = new Text({
|
||||
text: "An auto battler with stupid people.",
|
||||
font: subTitleFont,
|
||||
});
|
||||
this._subTitle.anchor = vec(0.5, 0);
|
||||
this._subTitle.pos = vec(0, this._titleHeight + spacingM);
|
||||
this.addChild(this._subTitle);
|
||||
}
|
||||
|
||||
// Sub title
|
||||
this._subTitle = new Text({
|
||||
text: "An auto battler with stupid people.",
|
||||
font: subTitleFont,
|
||||
});
|
||||
this._subTitle.anchor = vec(0.5, 0);
|
||||
this._subTitle.pos = vec(0, this._titleHeight + spacingM);
|
||||
this.addChild(this._subTitle);
|
||||
}
|
||||
get height(): number {
|
||||
return this._titleHeight + subTitleFont.size + spacingM;
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return this._titleHeight + subTitleFont.size + spacingM;
|
||||
}
|
||||
|
||||
private get _titleHeight(): number {
|
||||
return titleFont.size;
|
||||
}
|
||||
private get _titleHeight(): number {
|
||||
return titleFont.size;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +1,66 @@
|
||||
import { Client, GameStateRawReadonly, Id, Player, Spectator } from "./GameStateRaw";
|
||||
import {
|
||||
Client,
|
||||
GameStateRawReadonly,
|
||||
Id,
|
||||
Player,
|
||||
Spectator,
|
||||
} from "./GameStateRaw";
|
||||
import { maxPlayers } from "./main";
|
||||
import { log } from "./utils/debugUtils";
|
||||
|
||||
export class GameState {
|
||||
_errors: string[] = [];
|
||||
_errors: string[] = [];
|
||||
|
||||
constructor(public readonly gameStateRaw: GameStateRawReadonly, readonly client: Client) {
|
||||
this._validate();
|
||||
constructor(
|
||||
public readonly gameStateRaw: GameStateRawReadonly,
|
||||
readonly client: Client,
|
||||
) {
|
||||
this._validate();
|
||||
}
|
||||
|
||||
private _validate(): void {
|
||||
if (!this.isPlayer && !this.isSpectator) {
|
||||
this._errors.push("You must be either a player or spectator.");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
if (this.isPlayer && this.isSpectator) {
|
||||
this._errors.push("You cannot be both player and spectator.");
|
||||
}
|
||||
|
||||
get players(): Player[] {
|
||||
return this.gameStateRaw.players;
|
||||
if (this._errors.length > 0) {
|
||||
log("GameState", this._errors);
|
||||
}
|
||||
}
|
||||
|
||||
get spectators(): Spectator[] {
|
||||
return this.gameStateRaw.spectators;
|
||||
}
|
||||
get players(): Player[] {
|
||||
return this.gameStateRaw.players;
|
||||
}
|
||||
|
||||
get isFull(): boolean {
|
||||
return this.players.length >= maxPlayers;
|
||||
}
|
||||
get spectators(): Spectator[] {
|
||||
return this.gameStateRaw.spectators;
|
||||
}
|
||||
|
||||
get isPlayer(): boolean {
|
||||
return this.players.map(p => p.player.id).includes(this.client.id);
|
||||
}
|
||||
get isFull(): boolean {
|
||||
return this.players.length >= maxPlayers;
|
||||
}
|
||||
|
||||
get isReady(): boolean {
|
||||
return this.getPlayerById(this.client.id)!.ready;
|
||||
}
|
||||
get isPlayer(): boolean {
|
||||
return this.players.map((p) => p.player.id).includes(this.client.id);
|
||||
}
|
||||
|
||||
get isSpectator(): boolean {
|
||||
return this.spectators.map(p => p.player.id).includes(this.client.id);
|
||||
}
|
||||
get isReady(): boolean {
|
||||
return this.getPlayerById(this.client.id)!.ready;
|
||||
}
|
||||
|
||||
getPlayerById(id: Id): Player | undefined {
|
||||
return this.players.find(p => p.player.id === id);
|
||||
}
|
||||
get isSpectator(): boolean {
|
||||
return this.spectators.map((p) => p.player.id).includes(this.client.id);
|
||||
}
|
||||
|
||||
getSpectatorById(id: Id): Spectator | undefined {
|
||||
return this.spectators.find(p => p.player.id === 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);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,22 @@
|
||||
export type Id = number;
|
||||
|
||||
export interface GameStateRaw {
|
||||
players: Player[];
|
||||
spectators: Spectator[];
|
||||
players: Player[];
|
||||
spectators: Spectator[];
|
||||
}
|
||||
|
||||
export type GameStateRawReadonly = Readonly<GameStateRaw>
|
||||
export type GameStateRawReadonly = Readonly<GameStateRaw>;
|
||||
|
||||
export interface Player {
|
||||
player: Client;
|
||||
ready: boolean;
|
||||
player: Client;
|
||||
ready: boolean;
|
||||
}
|
||||
|
||||
export interface Spectator {
|
||||
player: Client;
|
||||
player: Client;
|
||||
}
|
||||
|
||||
export interface Client {
|
||||
id: Id;
|
||||
name: string;
|
||||
id: Id;
|
||||
name: string;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as ex from 'excalibur';
|
||||
import { GameState } from '../GameState';
|
||||
import * as ex from "excalibur";
|
||||
import { GameState } from "../GameState";
|
||||
|
||||
export class GameStateScene extends ex.Scene {
|
||||
public updateFromGameState(_gameState: GameState): void { }
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
public updateFromGameState(_gameState: GameState): void {}
|
||||
}
|
||||
@@ -1,273 +1,293 @@
|
||||
import * as ex from 'excalibur';
|
||||
import { spacingL, spacingM } from '../utils/formattingUtils';
|
||||
import { Cups } from '../Cups';
|
||||
import { GameStateScene } from './GameStateScene';
|
||||
import { PlayerListScreenElement } from '../ScreenElements/PlayerListScreenElement';
|
||||
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';
|
||||
import { Id } from '../GameStateRaw';
|
||||
import * as ex from "excalibur";
|
||||
import { spacingL, spacingM } from "../utils/formattingUtils";
|
||||
import { Cups } from "../Cups";
|
||||
import { GameStateScene } from "./GameStateScene";
|
||||
import { PlayerListScreenElement } from "../ScreenElements/PlayerListScreenElement";
|
||||
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";
|
||||
import { Id } from "../GameStateRaw";
|
||||
|
||||
const tableWidth = 400;
|
||||
|
||||
export type Screens = "join" | "lobby";
|
||||
|
||||
export class LobbyScene extends GameStateScene {
|
||||
private _table: ex.ScreenElement = null!;
|
||||
private _cups: ex.ScreenElement = null!;
|
||||
private _table: ex.ScreenElement = null!;
|
||||
private _cups: ex.ScreenElement = null!;
|
||||
|
||||
private _title: Title = null!;
|
||||
private _title: Title = null!;
|
||||
|
||||
private _bar1: ex.ScreenElement = null!;
|
||||
private _bar2: ex.ScreenElement = null!;
|
||||
private _bar3: ex.ScreenElement = null!;
|
||||
private _bar1: ex.ScreenElement = null!;
|
||||
private _bar2: ex.ScreenElement = null!;
|
||||
private _bar3: ex.ScreenElement = null!;
|
||||
|
||||
private _playerList: PlayerListScreenElement = null!;
|
||||
private _spectatorsList: ClientListScreenElement = null!;
|
||||
private _playerList: PlayerListScreenElement = null!;
|
||||
private _spectatorsList: ClientListScreenElement = null!;
|
||||
|
||||
private _joinSpectatorButton: Button = null!;
|
||||
private _joinPlayerButton: Button = null!;
|
||||
private _joinSpectatorButton: Button = null!;
|
||||
private _joinPlayerButton: Button = null!;
|
||||
|
||||
private _readyButton: Button = null!;
|
||||
private _unreadyButton: Button = null!;
|
||||
private _readyButton: Button = null!;
|
||||
private _unreadyButton: Button = null!;
|
||||
|
||||
private _playerNameInput: PlayerNameInput = null!;
|
||||
private _playerNameInput: PlayerNameInput = null!;
|
||||
|
||||
private _errorList: ErrorList = null!;
|
||||
private _noConnectionErrorHandle: (() => void) | undefined;
|
||||
private _errorList: ErrorList = null!;
|
||||
private _noConnectionErrorHandle: (() => void) | undefined;
|
||||
|
||||
private _screen: "join" | "lobby" = "join";
|
||||
private _screen: "join" | "lobby" = "join";
|
||||
|
||||
constructor(private _connection: Connection) {
|
||||
super();
|
||||
constructor(private _connection: Connection) {
|
||||
super();
|
||||
|
||||
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));
|
||||
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 {
|
||||
const tableBorder = 10;
|
||||
const paddedTableWidth = tableWidth - 2 * tableBorder - 2 * spacingM;
|
||||
// Table background
|
||||
this._table = new ex.ScreenElement({
|
||||
anchor: ex.vec(0.5, 0),
|
||||
});
|
||||
this.add(this._table);
|
||||
|
||||
const points = [
|
||||
ex.vec(0, tableBorder),
|
||||
ex.vec(tableBorder, 0),
|
||||
ex.vec(tableWidth - tableBorder, 0),
|
||||
ex.vec(tableWidth, tableBorder),
|
||||
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 Title();
|
||||
this.add(this._title);
|
||||
|
||||
this._playerList = new PlayerListScreenElement("Players", maxPlayers);
|
||||
this._playerList.anchor.setTo(0.5, 0);
|
||||
|
||||
this._joinPlayerButton = new Button({
|
||||
text: "Join as player",
|
||||
anchor: ex.vec(0.5, 0),
|
||||
onClick: () => this._joinAsPlayer(),
|
||||
});
|
||||
|
||||
this._spectatorsList = new ClientListScreenElement("Spectators");
|
||||
this._spectatorsList.anchor.setTo(0.5, 0);
|
||||
|
||||
this._joinSpectatorButton = new Button({
|
||||
text: "Join as spectator",
|
||||
anchor: ex.vec(0.5, 0),
|
||||
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(name),
|
||||
(id: Id) => this._rejoin(id),
|
||||
);
|
||||
this._playerNameInput.anchor = ex.vec(0.5, 0);
|
||||
|
||||
// Error box
|
||||
const w = paddedTableWidth - 2 * spacingM;
|
||||
this._errorList = new ErrorList({ width: w });
|
||||
this._errorList.pos = ex.vec(
|
||||
engine.screen.width / 2 - w / 2,
|
||||
engine.screen.height - spacingL,
|
||||
);
|
||||
|
||||
this.add(this._errorList);
|
||||
|
||||
this._connection.on("connect-or-disconnect", ({ connected }) => {
|
||||
if (connected) {
|
||||
this._noConnectionErrorHandle?.();
|
||||
} else if (this._noConnectionErrorHandle == null) {
|
||||
this._noConnectionErrorHandle = this._errorList.addError(
|
||||
"No server connection",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
engine.screen.events.on("resize", () => this._updatePositions());
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onActivate(_context: ex.SceneActivationContext<unknown>): void {
|
||||
this._updateVisibility();
|
||||
this._updatePositions();
|
||||
}
|
||||
|
||||
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 _updateVisibility(gameState?: GameState): void {
|
||||
const isJoinScreen = this._screen === "join";
|
||||
const setVisible = (g: ex.Actor, visible: boolean): void => {
|
||||
if (visible) {
|
||||
this.add(g);
|
||||
} else {
|
||||
this.remove(g);
|
||||
}
|
||||
};
|
||||
|
||||
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 && isSpectator && !isLobbyFull,
|
||||
);
|
||||
setVisible(this._spectatorsList, !isJoinScreen);
|
||||
setVisible(this._bar3, !isJoinScreen);
|
||||
setVisible(this._joinSpectatorButton, !isJoinScreen && isPlayer);
|
||||
|
||||
setVisible(this._readyButton, !isJoinScreen && isNotReadyVisible);
|
||||
setVisible(this._unreadyButton, !isJoinScreen && isReadyVisible);
|
||||
}
|
||||
|
||||
private _updatePositions(): void {
|
||||
let y = spacingM;
|
||||
const halfWidth = this.engine.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.height + spacingM;
|
||||
|
||||
this._bar2.pos = ex.vec(halfWidth, y);
|
||||
y += this._bar2.height + spacingL;
|
||||
|
||||
if (this._screen === "join") {
|
||||
this._playerNameInput.pos = ex.vec(halfWidth, y);
|
||||
y += this._playerNameInput.height;
|
||||
} else {
|
||||
this._playerList.pos = ex.vec(halfWidth, y);
|
||||
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);
|
||||
y += this._bar3.height + spacingL;
|
||||
|
||||
this._spectatorsList.pos = ex.vec(halfWidth, y);
|
||||
y += this._spectatorsList.childrenHeight + spacingM;
|
||||
|
||||
this._joinSpectatorButton.pos = ex.vec(halfWidth, y);
|
||||
y += this._spectatorsList.childrenHeight + spacingL;
|
||||
}
|
||||
}
|
||||
|
||||
onInitialize(engine: ex.Engine): void {
|
||||
const tableBorder = 10;
|
||||
const paddedTableWidth = tableWidth - 2 * tableBorder - 2 * spacingM;
|
||||
// Table background
|
||||
this._table = new ex.ScreenElement({
|
||||
anchor: ex.vec(0.5, 0)
|
||||
});
|
||||
this.add(this._table);
|
||||
public updateFromGameState(gameState: GameState): void {
|
||||
this._playerList.updateFromGameState(gameState.players, gameState.client);
|
||||
this._spectatorsList.updateFromGameState(
|
||||
gameState.spectators,
|
||||
gameState.client,
|
||||
);
|
||||
this._updatePositions();
|
||||
this._updateVisibility(gameState);
|
||||
}
|
||||
|
||||
const points = [
|
||||
ex.vec(0, tableBorder),
|
||||
ex.vec(tableBorder, 0),
|
||||
ex.vec(tableWidth - tableBorder, 0),
|
||||
ex.vec(tableWidth, tableBorder),
|
||||
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);
|
||||
public setScreen(screen: Screens): void {
|
||||
this._screen = screen;
|
||||
this._updatePositions();
|
||||
}
|
||||
|
||||
this._bar1 = this._createBar();
|
||||
this._bar2 = this._createBar();
|
||||
this._bar3 = this._createBar();
|
||||
private _join(name: string): void {
|
||||
this._connection.join(name);
|
||||
}
|
||||
|
||||
// Cups
|
||||
this._cups = new ex.ScreenElement({})
|
||||
this._cups.anchor.setTo(0.5, 0);
|
||||
this._cups.graphics.add(new Cups());
|
||||
this.add(this._cups);
|
||||
private _rejoin(id: Id): void {
|
||||
this._connection.rejoin(id);
|
||||
}
|
||||
|
||||
// Title
|
||||
this._title = new Title();
|
||||
this.add(this._title);
|
||||
private _joinAsPlayer(): void {
|
||||
this._connection.toggleRole();
|
||||
}
|
||||
|
||||
this._playerList = new PlayerListScreenElement("Players", maxPlayers);
|
||||
this._playerList.anchor.setTo(0.5, 0);
|
||||
private _joinAsSpectator(): void {
|
||||
this._connection.toggleRole();
|
||||
}
|
||||
|
||||
this._joinPlayerButton = new Button({
|
||||
text: "Join as player",
|
||||
anchor: ex.vec(0.5, 0),
|
||||
onClick: () => this._joinAsPlayer(),
|
||||
});
|
||||
private _ready(): void {
|
||||
this._connection.toggleReady();
|
||||
}
|
||||
|
||||
this._spectatorsList = new ClientListScreenElement("Spectators");
|
||||
this._spectatorsList.anchor.setTo(0.5, 0);
|
||||
|
||||
this._joinSpectatorButton = new Button({
|
||||
text: "Join as spectator",
|
||||
anchor: ex.vec(0.5, 0),
|
||||
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(name), (id: Id) => this._rejoin(id));
|
||||
this._playerNameInput.anchor = ex.vec(0.5, 0);
|
||||
|
||||
// Error box
|
||||
const w = paddedTableWidth - 2 * spacingM;
|
||||
this._errorList = new ErrorList({width: w });
|
||||
this._errorList.pos = ex.vec(engine.screen.width / 2 - w / 2, engine.screen.height - spacingL);
|
||||
|
||||
this.add(this._errorList);
|
||||
|
||||
this._connection.on("connect-or-disconnect", ({connected}) => {
|
||||
if (connected) {
|
||||
this._noConnectionErrorHandle?.();
|
||||
} else if (this._noConnectionErrorHandle == null) {
|
||||
this._noConnectionErrorHandle = this._errorList.addError("No server connection");
|
||||
}
|
||||
});
|
||||
|
||||
engine.screen.events.on("resize", () => this._updatePositions());
|
||||
}
|
||||
|
||||
onActivate(_context: ex.SceneActivationContext<unknown>): void {
|
||||
this._updateVisibility();
|
||||
this._updatePositions();
|
||||
}
|
||||
|
||||
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 _updateVisibility(gameState?: GameState): void {
|
||||
const isJoinScreen = this._screen === "join";
|
||||
const setVisible = (g: ex.Actor, visible: boolean): void => {
|
||||
if (visible) {
|
||||
this.add(g);
|
||||
} else {
|
||||
this.remove(g);
|
||||
}
|
||||
}
|
||||
|
||||
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 && isSpectator && !isLobbyFull);
|
||||
setVisible(this._spectatorsList, !isJoinScreen);
|
||||
setVisible(this._bar3, !isJoinScreen);
|
||||
setVisible(this._joinSpectatorButton, !isJoinScreen && isPlayer);
|
||||
|
||||
setVisible(this._readyButton, !isJoinScreen && isNotReadyVisible);
|
||||
setVisible(this._unreadyButton, !isJoinScreen && isReadyVisible);
|
||||
|
||||
}
|
||||
|
||||
private _updatePositions(): void {
|
||||
let y = spacingM;
|
||||
const halfWidth = this.engine.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.height + spacingM;
|
||||
|
||||
this._bar2.pos = ex.vec(halfWidth, y);
|
||||
y += this._bar2.height + spacingL;
|
||||
|
||||
if (this._screen === "join") {
|
||||
this._playerNameInput.pos = ex.vec(halfWidth, y);
|
||||
y += this._playerNameInput.height;
|
||||
} else {
|
||||
this._playerList.pos = ex.vec(halfWidth, y);
|
||||
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);
|
||||
y += this._bar3.height + spacingL;
|
||||
|
||||
this._spectatorsList.pos = ex.vec(halfWidth, y);
|
||||
y += this._spectatorsList.childrenHeight + spacingM;
|
||||
|
||||
this._joinSpectatorButton.pos = ex.vec(halfWidth, y);
|
||||
y += this._spectatorsList.childrenHeight + spacingL;
|
||||
}
|
||||
}
|
||||
|
||||
public updateFromGameState(gameState: GameState): void {
|
||||
this._playerList.updateFromGameState(gameState.players, gameState.client);
|
||||
this._spectatorsList.updateFromGameState(gameState.spectators, gameState.client);
|
||||
this._updatePositions();
|
||||
this._updateVisibility(gameState);
|
||||
}
|
||||
|
||||
public setScreen(screen: Screens): void {
|
||||
this._screen = screen;
|
||||
this._updatePositions();
|
||||
}
|
||||
|
||||
private _join(name: string): void {
|
||||
this._connection.join(name);
|
||||
}
|
||||
|
||||
private _rejoin(id: Id): void {
|
||||
this._connection.rejoin(id);
|
||||
}
|
||||
|
||||
private _joinAsPlayer(): void {
|
||||
this._connection.toggleRole();
|
||||
}
|
||||
|
||||
private _joinAsSpectator(): void {
|
||||
this._connection.toggleRole();
|
||||
}
|
||||
|
||||
private _ready(): void {
|
||||
this._connection.toggleReady();
|
||||
}
|
||||
|
||||
private _unready(): void {
|
||||
this._connection.toggleReady();
|
||||
}
|
||||
private _unready(): void {
|
||||
this._connection.toggleReady();
|
||||
}
|
||||
}
|
||||
@@ -1,136 +1,157 @@
|
||||
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';
|
||||
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;
|
||||
text?: string;
|
||||
disabled?: boolean;
|
||||
color?: ex.Color;
|
||||
onClick?: (event: ex.PointerEvent) => void;
|
||||
}
|
||||
|
||||
type ConstructProperties = ex.ActorArgs & ButtonProperties;
|
||||
|
||||
export class Button extends ex.ScreenElement {
|
||||
private _text?: string;
|
||||
private _textElement: Text = null!;
|
||||
private _color?: ex.Color;
|
||||
private _text?: string;
|
||||
private _textElement: Text = null!;
|
||||
private _color?: ex.Color;
|
||||
|
||||
private _disabled: boolean;
|
||||
private _disabled: boolean;
|
||||
|
||||
private _onClick?: (event: ex.PointerEvent) => void;
|
||||
private _onClick?: (event: ex.PointerEvent) => void;
|
||||
|
||||
private _down: boolean = false;
|
||||
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;
|
||||
constructor(config?: ConstructProperties) {
|
||||
super(config);
|
||||
this._text = config?.text;
|
||||
this._disabled = config?.disabled ?? false;
|
||||
this._color = config?.color;
|
||||
this._onClick = config?.onClick;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onInitialize(_engine: ex.Engine): void {
|
||||
const { height, width } = this;
|
||||
|
||||
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);
|
||||
|
||||
const color = this._color ?? ex.Color.fromHex("4788ff");
|
||||
const hoverColor = color.lighten(0.5);
|
||||
|
||||
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._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));
|
||||
}
|
||||
|
||||
get text(): string {
|
||||
return this._text ?? "";
|
||||
}
|
||||
|
||||
get disabled(): boolean {
|
||||
return this._disabled;
|
||||
}
|
||||
|
||||
set disabled(value: boolean) {
|
||||
this._disabled = value;
|
||||
this._updateGraphic();
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
return (
|
||||
paragraphFont.measureTextWithoutCache(this.text).width + this.padding
|
||||
);
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return paragraphFont.size + this.padding;
|
||||
}
|
||||
|
||||
get padding(): number {
|
||||
return 2 * spacingS;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onPointerDown(_event: ex.PointerEvent): void {
|
||||
if (this._disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
onInitialize(_engine: ex.Engine): void {
|
||||
const { height, width } = this;
|
||||
this._down = true;
|
||||
this._updateGraphic("clicked");
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
const color = this._color ?? ex.Color.fromHex("4788ff");
|
||||
const hoverColor = color.lighten(0.5);
|
||||
|
||||
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._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));
|
||||
onPointerUp(event: ex.PointerEvent): void {
|
||||
if (this._disabled || !this._down) {
|
||||
return;
|
||||
}
|
||||
|
||||
get text(): string {
|
||||
return this._text ?? "";
|
||||
this._down = false;
|
||||
|
||||
const { _onClick } = this;
|
||||
if (_onClick != null) {
|
||||
_onClick(event);
|
||||
}
|
||||
|
||||
get disabled(): boolean {
|
||||
return this._disabled;
|
||||
this._updateGraphic("hover");
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onPointerEnter(_event: ex.PointerEvent): void {
|
||||
if (this._disabled || this._down) {
|
||||
return;
|
||||
}
|
||||
|
||||
set disabled(value: boolean) {
|
||||
this._disabled = value;
|
||||
this._updateGraphic();
|
||||
this._updateGraphic("hover");
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onPointerLeave(_event: ex.PointerEvent): void {
|
||||
if (this._disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
return paragraphFont.measureTextWithoutCache(this.text).width + this.padding;
|
||||
this._updateGraphic("idle");
|
||||
}
|
||||
|
||||
private _updateGraphic(state: string = "idle"): void {
|
||||
if (this.disabled) {
|
||||
this.graphics.use("disabled");
|
||||
return;
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return paragraphFont.size + this.padding;
|
||||
}
|
||||
|
||||
get padding(): number {
|
||||
return 2 * spacingS;
|
||||
}
|
||||
|
||||
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 || this._down) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._updateGraphic("hover")
|
||||
}
|
||||
|
||||
onPointerLeave(_event: ex.PointerEvent): void {
|
||||
if (this._disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._updateGraphic("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) ;
|
||||
}
|
||||
this.graphics.use(state);
|
||||
this._textElement.pos =
|
||||
state === "clicked"
|
||||
? ex.vec(0, this.height / 2 + lowerBarHeightDown)
|
||||
: ex.vec(0, this.height / 2);
|
||||
}
|
||||
}
|
||||
@@ -1,50 +1,52 @@
|
||||
import * as ex from 'excalibur';
|
||||
import { paragraphFont } from '../utils/fontUtil';
|
||||
import { spacingS } from '../utils/formattingUtils';
|
||||
import * as ex from "excalibur";
|
||||
import { paragraphFont } from "../utils/fontUtil";
|
||||
import { spacingS } from "../utils/formattingUtils";
|
||||
|
||||
interface CheckboxProperties {
|
||||
checked: boolean;
|
||||
};
|
||||
checked: boolean;
|
||||
}
|
||||
|
||||
type ConstructProperties = ex.ActorArgs & CheckboxProperties;
|
||||
|
||||
export class Checkbox extends ex.ScreenElement {
|
||||
private _checked: boolean;
|
||||
private _checked: boolean;
|
||||
|
||||
constructor(config: ConstructProperties) {
|
||||
super(config);
|
||||
this._checked = config.checked;
|
||||
}
|
||||
constructor(config: ConstructProperties) {
|
||||
super(config);
|
||||
this._checked = config.checked;
|
||||
}
|
||||
|
||||
onInitialize(): void {
|
||||
const size = paragraphFont.size - spacingS;
|
||||
const color = ex.Color.Green;
|
||||
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 checkedGraphics = new ex.GraphicsGroup({
|
||||
members: [
|
||||
{
|
||||
graphic: new ex.Circle({ radius: size / 2, color }),
|
||||
offset: ex.vec(0, 0),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
],
|
||||
});
|
||||
const uncheckedGraphics = new ex.GraphicsGroup({ members: [] });
|
||||
|
||||
const uncheckedGraphics = new ex.GraphicsGroup({ members: [ ], })
|
||||
this.graphics.add("checked", checkedGraphics);
|
||||
this.graphics.add("unchecked", uncheckedGraphics);
|
||||
|
||||
this.graphics.add('checked', checkedGraphics);
|
||||
this.graphics.add('unchecked', uncheckedGraphics);
|
||||
this._updateGraphic();
|
||||
}
|
||||
|
||||
this._updateGraphic();
|
||||
}
|
||||
get checked(): boolean {
|
||||
return this._checked;
|
||||
}
|
||||
|
||||
get checked(): boolean {
|
||||
return this._checked;
|
||||
}
|
||||
set checked(checked: boolean) {
|
||||
this._checked = checked;
|
||||
this._updateGraphic();
|
||||
}
|
||||
|
||||
set checked(checked: boolean) {
|
||||
this._checked = checked;
|
||||
this._updateGraphic();
|
||||
}
|
||||
|
||||
private _updateGraphic(): void {
|
||||
this.graphics.use(this.checked ? "checked" : "unchecked");
|
||||
}
|
||||
private _updateGraphic(): void {
|
||||
this.graphics.use(this.checked ? "checked" : "unchecked");
|
||||
}
|
||||
}
|
||||
@@ -1,97 +1,106 @@
|
||||
import * as ex from 'excalibur';
|
||||
import * as ex from "excalibur";
|
||||
|
||||
import { spacingS } from '../utils/formattingUtils';
|
||||
import { headingFont, paragraphFont } from '../utils/fontUtil';
|
||||
import { Text } from './Text';
|
||||
import { Client, Player, Spectator } from '../GameStateRaw';
|
||||
import { spacingS } from "../utils/formattingUtils";
|
||||
import { headingFont, paragraphFont } from "../utils/fontUtil";
|
||||
import { Text } from "./Text";
|
||||
import { Client, Player, Spectator } from "../GameStateRaw";
|
||||
|
||||
const clientColor = ex.Color.fromHex("40E0D0")
|
||||
const clientColor = ex.Color.fromHex("40E0D0");
|
||||
|
||||
export class ClientListScreenElement extends ex.ScreenElement {
|
||||
protected _clients: Player[] | Spectator[] = [];
|
||||
protected _client: Client | null = null;
|
||||
protected _clients: Player[] | Spectator[] = [];
|
||||
protected _client: Client | null = null;
|
||||
|
||||
protected heading: Text = null!;
|
||||
protected entities: ex.Actor[] = [];
|
||||
protected heading: Text = null!;
|
||||
protected entities: ex.Actor[] = [];
|
||||
|
||||
constructor(private _headingText: string) {
|
||||
super();
|
||||
constructor(private _headingText: string) {
|
||||
super();
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
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._clients.length);
|
||||
}
|
||||
|
||||
public updateFromGameState(
|
||||
clients: Player[] | Spectator[],
|
||||
client: Client,
|
||||
): void {
|
||||
this._clients = clients;
|
||||
this._client = client;
|
||||
if (this.isInitialized) {
|
||||
this.updateList();
|
||||
}
|
||||
}
|
||||
|
||||
protected updateList(): void {
|
||||
this.clearLabelEntities();
|
||||
|
||||
let y = headingFont.size + spacingS;
|
||||
|
||||
for (const currentClient of this._clients) {
|
||||
const isClient = currentClient.player.id === this._client?.id;
|
||||
const label = this.createLabel(currentClient, y, isClient);
|
||||
|
||||
y += label.font.size + spacingS;
|
||||
this.addChild(label);
|
||||
this.entities.push(label);
|
||||
}
|
||||
|
||||
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();
|
||||
if (this._clients.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.entities.push(label);
|
||||
}
|
||||
}
|
||||
|
||||
get childrenHeight(): number {
|
||||
return this.computeChildrenHeight(this._clients.length);
|
||||
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 {
|
||||
return (
|
||||
headingFont.size +
|
||||
spacingS +
|
||||
Math.max(1, numLabels) * (spacingS + paragraphFont.size)
|
||||
);
|
||||
}
|
||||
|
||||
protected clearLabelEntities(): void {
|
||||
for (const labelEntity of this.entities) {
|
||||
this.removeChild(labelEntity);
|
||||
}
|
||||
|
||||
public updateFromGameState(clients: Player[] | Spectator[], client: Client): void {
|
||||
this._clients = clients
|
||||
this._client = client;
|
||||
if (this.isInitialized) {
|
||||
this.updateList();
|
||||
}
|
||||
}
|
||||
|
||||
protected updateList(): void {
|
||||
this.clearLabelEntities();
|
||||
|
||||
let y = headingFont.size + spacingS;
|
||||
|
||||
for(const currentClient of this._clients) {
|
||||
const isClient = currentClient.player.id === this._client?.id;
|
||||
const label = this.createLabel(currentClient, y, isClient);
|
||||
|
||||
y += label.font.size + spacingS;
|
||||
this.addChild(label);
|
||||
this.entities.push(label);
|
||||
}
|
||||
|
||||
if (this._clients.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.entities.push(label);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 {
|
||||
return headingFont.size + spacingS + Math.max(1, numLabels) * (spacingS + paragraphFont.size);
|
||||
}
|
||||
|
||||
protected clearLabelEntities(): void {
|
||||
for(const labelEntity of this.entities) {
|
||||
this.removeChild(labelEntity);
|
||||
}
|
||||
this.entities = [];
|
||||
}
|
||||
|
||||
|
||||
this.entities = [];
|
||||
}
|
||||
}
|
||||
@@ -4,53 +4,53 @@ import { paragraphFont } from "../utils/fontUtil";
|
||||
import { spacingS } from "../utils/formattingUtils";
|
||||
|
||||
interface ErrorScreenElementProperties {
|
||||
text: string;
|
||||
width: number;
|
||||
text: string;
|
||||
width: number;
|
||||
}
|
||||
|
||||
type ConstructProperties = Partial<ex.ActorArgs> & ErrorScreenElementProperties;
|
||||
|
||||
export class ErrorScreenElement extends ScreenElement {
|
||||
private _text: string;
|
||||
private _width: number;
|
||||
private _backgroundElement: ex.ScreenElement = null!;
|
||||
private _textElement: Text = null!;
|
||||
private _text: string;
|
||||
private _width: number;
|
||||
private _backgroundElement: ex.ScreenElement = null!;
|
||||
private _textElement: Text = null!;
|
||||
|
||||
constructor(config: ConstructProperties) {
|
||||
super(config);
|
||||
this._text = config.text;
|
||||
this._width = config.width;
|
||||
}
|
||||
constructor(config: ConstructProperties) {
|
||||
super(config);
|
||||
this._text = config.text;
|
||||
this._width = config.width;
|
||||
}
|
||||
|
||||
onInitialize(_engine: Engine): void {
|
||||
this._textElement = new Text({
|
||||
text: this.errorText,
|
||||
font: paragraphFont
|
||||
});
|
||||
this._textElement.pos = vec(this.padding, this.padding);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onInitialize(_engine: Engine): void {
|
||||
this._textElement = new Text({
|
||||
text: this.errorText,
|
||||
font: paragraphFont,
|
||||
});
|
||||
this._textElement.pos = vec(this.padding, this.padding);
|
||||
|
||||
this._backgroundElement = new ScreenElement();
|
||||
const background = new Rectangle({
|
||||
width: this._width,
|
||||
height: this.height,
|
||||
color: Color.Red,
|
||||
});
|
||||
this._backgroundElement.graphics.add(background);
|
||||
this._backgroundElement = new ScreenElement();
|
||||
const background = new Rectangle({
|
||||
width: this._width,
|
||||
height: this.height,
|
||||
color: Color.Red,
|
||||
});
|
||||
this._backgroundElement.graphics.add(background);
|
||||
|
||||
this.addChild(this._backgroundElement);
|
||||
this.addChild(this._textElement);
|
||||
}
|
||||
|
||||
this.addChild(this._backgroundElement);
|
||||
this.addChild(this._textElement);
|
||||
}
|
||||
get padding(): number {
|
||||
return spacingS;
|
||||
}
|
||||
|
||||
get padding(): number {
|
||||
return spacingS;
|
||||
}
|
||||
override get height(): number {
|
||||
return paragraphFont.size + 2 * this.padding;
|
||||
}
|
||||
|
||||
override get height(): number {
|
||||
return paragraphFont.size + 2 * this.padding;
|
||||
}
|
||||
|
||||
private get errorText(): string {
|
||||
return `Error: ${this._text}`;
|
||||
}
|
||||
private get errorText(): string {
|
||||
return `Error: ${this._text}`;
|
||||
}
|
||||
}
|
||||
@@ -1,59 +1,59 @@
|
||||
import * as ex from 'excalibur';
|
||||
import { spacingS } from '../utils/formattingUtils';
|
||||
import { disabledFont } from '../utils/fontUtil';
|
||||
import { Text } from './Text';
|
||||
import { Checkbox } from './Checkbox';
|
||||
import { Player } from '../GameStateRaw';
|
||||
import { ClientListScreenElement } from './ClientListScreenElement';
|
||||
|
||||
import * as ex from "excalibur";
|
||||
import { spacingS } from "../utils/formattingUtils";
|
||||
import { disabledFont } from "../utils/fontUtil";
|
||||
import { Text } from "./Text";
|
||||
import { Checkbox } from "./Checkbox";
|
||||
import { Player } from "../GameStateRaw";
|
||||
import { ClientListScreenElement } from "./ClientListScreenElement";
|
||||
|
||||
export class PlayerListScreenElement extends ClientListScreenElement {
|
||||
protected override _clients: Player[] = [];
|
||||
constructor(headingText: string, private _maxPlayers: number) {
|
||||
super(headingText);
|
||||
protected override _clients: Player[] = [];
|
||||
constructor(
|
||||
headingText: string,
|
||||
private _maxPlayers: number,
|
||||
) {
|
||||
super(headingText);
|
||||
}
|
||||
|
||||
protected override updateList(): void {
|
||||
this.clearLabelEntities();
|
||||
|
||||
let y = this.heading.font.size + spacingS;
|
||||
|
||||
const clients = this._clients.slice(0, this._maxPlayers);
|
||||
|
||||
for (const currentClient of clients) {
|
||||
const { name, id } = currentClient.player;
|
||||
const isClient = id === this._client?.id;
|
||||
const label = this.createLabel(currentClient, y, isClient);
|
||||
|
||||
this.addChild(label);
|
||||
this.entities.push(label);
|
||||
|
||||
const ready = new Checkbox({ checked: currentClient.ready });
|
||||
const textWidth = label.font.measureTextWithoutCache(name ?? "").width;
|
||||
ready.pos = ex.vec(textWidth / 2 + spacingS, y);
|
||||
this.addChild(ready);
|
||||
|
||||
this.entities.push(ready);
|
||||
|
||||
y += label.font.size + spacingS;
|
||||
}
|
||||
|
||||
protected override updateList(): void {
|
||||
this.clearLabelEntities();
|
||||
|
||||
let y = this.heading.font.size + spacingS;
|
||||
|
||||
const clients = this._clients.slice(0, this._maxPlayers);
|
||||
|
||||
for(const currentClient of clients) {
|
||||
const { name, id } = currentClient.player;
|
||||
const isClient = id === this._client?.id;
|
||||
const label = this.createLabel(currentClient, y, isClient);
|
||||
|
||||
this.addChild(label);
|
||||
this.entities.push(label);
|
||||
|
||||
|
||||
const ready = new Checkbox({ checked: currentClient.ready});
|
||||
const textWidth = label.font.measureTextWithoutCache(name ?? "").width
|
||||
ready.pos = ex.vec( textWidth / 2 + spacingS, y);
|
||||
this.addChild(ready);
|
||||
|
||||
this.entities.push(ready);
|
||||
|
||||
y += label.font.size + spacingS;
|
||||
}
|
||||
|
||||
for(let i = clients.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.entities.push(label);
|
||||
|
||||
}
|
||||
for (let i = clients.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.entities.push(label);
|
||||
}
|
||||
}
|
||||
|
||||
override get childrenHeight(): number {
|
||||
return this.computeChildrenHeight(this._maxPlayers);
|
||||
}
|
||||
override get childrenHeight(): number {
|
||||
return this.computeChildrenHeight(this._maxPlayers);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import * as ex from 'excalibur';
|
||||
import * as ex from "excalibur";
|
||||
|
||||
type ConstructProperties = ex.LabelOptions & ex.ActorArgs;
|
||||
|
||||
export class Text extends ex.Label {
|
||||
constructor(options?: ConstructProperties) {
|
||||
super(options);
|
||||
}
|
||||
constructor(options?: ConstructProperties) {
|
||||
super(options);
|
||||
}
|
||||
}
|
||||
@@ -1,209 +1,226 @@
|
||||
import * as ex from 'excalibur';
|
||||
import { paragraphFont } from '../utils/fontUtil';
|
||||
import { Text } from './Text';
|
||||
import { spacingS } from '../utils/formattingUtils';
|
||||
import * as ex from "excalibur";
|
||||
import { paragraphFont } from "../utils/fontUtil";
|
||||
import { Text } from "./Text";
|
||||
import { spacingS } from "../utils/formattingUtils";
|
||||
|
||||
interface TextInputProperties {
|
||||
text?: string;
|
||||
width: number;
|
||||
};
|
||||
text?: string;
|
||||
width: number;
|
||||
}
|
||||
|
||||
type ConstructProperties = ex.ActorArgs & TextInputProperties;
|
||||
|
||||
type TextInputEvents = {
|
||||
textChanged: TextChangedEvent;
|
||||
}
|
||||
textChanged: TextChangedEvent;
|
||||
};
|
||||
|
||||
export class TextChangedEvent extends ex.GameEvent<string> {
|
||||
constructor(public text: string) {
|
||||
super();
|
||||
}
|
||||
constructor(public text: string) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export const TextInputEvents = {
|
||||
Textchanged: "textchanged"
|
||||
} as const
|
||||
Textchanged: "textchanged",
|
||||
} as const;
|
||||
|
||||
export class TextInput extends ex.ScreenElement {
|
||||
public events = new ex.EventEmitter<ex.ActorEvents & TextInputEvents>();
|
||||
public events = new ex.EventEmitter<ex.ActorEvents & TextInputEvents>();
|
||||
|
||||
private _width: number;
|
||||
private _text?: string;
|
||||
private _textElement: Text = null!;
|
||||
private _width: number;
|
||||
private _text?: string;
|
||||
private _textElement: Text = null!;
|
||||
|
||||
private _focused = false;
|
||||
private _focused = false;
|
||||
|
||||
private _cursor: ex.ScreenElement = null!;
|
||||
private _cursorPosition = 0;
|
||||
private _cursorTimer: ex.Timer = null!
|
||||
private _cursor: ex.ScreenElement = null!;
|
||||
private _cursorPosition = 0;
|
||||
private _cursorTimer: ex.Timer = null!;
|
||||
|
||||
constructor(config: ConstructProperties) {
|
||||
super(config);
|
||||
this._width = config?.width;
|
||||
this._text = config?.text;
|
||||
constructor(config: ConstructProperties) {
|
||||
super(config);
|
||||
this._width = config?.width;
|
||||
this._text = config?.text;
|
||||
}
|
||||
|
||||
onInitialize(engine: ex.Engine): void {
|
||||
const width = this._width;
|
||||
const height = this.height;
|
||||
|
||||
this._textElement = new Text({
|
||||
text: this.text,
|
||||
font: paragraphFont,
|
||||
});
|
||||
this._textElement.anchor = ex.vec(0, 0.5);
|
||||
this._textElement.pos = ex.vec(-width / 2 + this.padding, height / 2);
|
||||
this.addChild(this._textElement);
|
||||
|
||||
this.graphics.add(
|
||||
new ex.Rectangle({
|
||||
width,
|
||||
height,
|
||||
color: ex.Color.fromHex("D3D3D3"),
|
||||
}),
|
||||
);
|
||||
|
||||
this._cursor = new ex.ScreenElement();
|
||||
this._cursor.graphics.add(
|
||||
new ex.Line({
|
||||
start: ex.vec(0, 0),
|
||||
end: ex.vec(0, this.textHeight),
|
||||
thickness: 2,
|
||||
color: ex.Color.Black,
|
||||
}),
|
||||
);
|
||||
this._cursor.graphics.isVisible = false;
|
||||
this._cursor.pos = ex.vec(-width / 2 + this.padding, this.padding);
|
||||
|
||||
this._cursorTimer = new ex.Timer({
|
||||
fcn: () => {
|
||||
this._cursor.graphics.isVisible = !this._cursor.graphics.isVisible;
|
||||
},
|
||||
repeats: true,
|
||||
interval: 500,
|
||||
});
|
||||
engine.currentScene.add(this._cursorTimer);
|
||||
this.addChild(this._cursor);
|
||||
|
||||
this.on("pointerup", () => this._focus());
|
||||
engine.input.keyboard.on("press", (e) => this._onKeyPress(e));
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return this.textHeight + 2 * spacingS;
|
||||
}
|
||||
|
||||
get textHeight(): number {
|
||||
return paragraphFont.size;
|
||||
}
|
||||
|
||||
get padding(): number {
|
||||
return spacingS;
|
||||
}
|
||||
|
||||
get text(): string {
|
||||
return this._text ?? "";
|
||||
}
|
||||
|
||||
set text(value: string) {
|
||||
this._text = value;
|
||||
this._textElement.text = value;
|
||||
|
||||
this._updateCursor();
|
||||
}
|
||||
|
||||
get cursorPosition(): number {
|
||||
return this._cursorPosition;
|
||||
}
|
||||
|
||||
set cursorPosition(value: number) {
|
||||
this._cursorPosition = Math.min(Math.max(0, value), this.text.length);
|
||||
this._updateCursor();
|
||||
}
|
||||
|
||||
private _focus(): void {
|
||||
this._focused = true;
|
||||
this._cursorTimer.start();
|
||||
this._cursor.graphics.isVisible = true;
|
||||
}
|
||||
|
||||
private _unfocus(): void {
|
||||
this._focused = false;
|
||||
this._cursorTimer.stop();
|
||||
this._cursor.graphics.isVisible = false;
|
||||
}
|
||||
|
||||
private _updateCursor(): void {
|
||||
const textBeforeCursor = this.text.slice(0, this._cursorPosition);
|
||||
const textWidth =
|
||||
this._textElement.font.measureTextWithoutCache(textBeforeCursor).width;
|
||||
const x = -this._width / 2 + this.padding + textWidth - 2;
|
||||
this._cursor.pos = ex.vec(x, this.padding);
|
||||
this._cursor.graphics.isVisible = true;
|
||||
this._cursorTimer.reset();
|
||||
}
|
||||
|
||||
private _onKeyPress(event: ex.KeyEvent): void {
|
||||
if (!this._focused) {
|
||||
return;
|
||||
}
|
||||
|
||||
onInitialize(engine: ex.Engine): void {
|
||||
const width = this._width;
|
||||
const height = this.height;
|
||||
const { key } = event;
|
||||
|
||||
this._textElement = new Text({
|
||||
text: this.text,
|
||||
font: paragraphFont,
|
||||
});
|
||||
this._textElement.anchor = ex.vec(0, 0.5);
|
||||
this._textElement.pos = ex.vec(-width / 2 + this.padding, height / 2);
|
||||
this.addChild(this._textElement);
|
||||
|
||||
this.graphics.add(new ex.Rectangle({
|
||||
width,
|
||||
height,
|
||||
color: ex.Color.fromHex("D3D3D3"),
|
||||
}));
|
||||
|
||||
this._cursor = new ex.ScreenElement();
|
||||
this._cursor.graphics.add(new ex.Line({
|
||||
start: ex.vec(0, 0),
|
||||
end: ex.vec(0, this.textHeight),
|
||||
thickness: 2,
|
||||
color: ex.Color.Black,
|
||||
}));
|
||||
this._cursor.graphics.isVisible = false;
|
||||
this._cursor.pos = ex.vec(-width / 2 + this.padding, this.padding)
|
||||
|
||||
this._cursorTimer = new ex.Timer({
|
||||
fcn: () => {
|
||||
this._cursor.graphics.isVisible = !this._cursor.graphics.isVisible;
|
||||
},
|
||||
repeats: true,
|
||||
interval: 500,
|
||||
});
|
||||
engine.currentScene.add(this._cursorTimer);
|
||||
this.addChild(this._cursor);
|
||||
|
||||
this.on("pointerup", () => this._focus());
|
||||
engine.input.keyboard.on("press", (e) => this._onKeyPress(e));
|
||||
if (key === ex.Keys.Esc) {
|
||||
this._unfocus();
|
||||
return;
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return this.textHeight + 2 * spacingS;
|
||||
if (key === ex.Keys.Enter) {
|
||||
return;
|
||||
}
|
||||
|
||||
get textHeight(): number {
|
||||
return paragraphFont.size;
|
||||
if (key === ex.Keys.Space) {
|
||||
return;
|
||||
}
|
||||
|
||||
get padding(): number {
|
||||
return spacingS;
|
||||
const invalidKeys = [
|
||||
ex.Keys.Alt,
|
||||
ex.Keys.ShiftLeft,
|
||||
ex.Keys.ShiftRight,
|
||||
ex.Keys.AltLeft,
|
||||
ex.Keys.AltRight,
|
||||
ex.Keys.ControlLeft,
|
||||
ex.Keys.ControlRight,
|
||||
ex.Keys.Home,
|
||||
ex.Keys.End,
|
||||
ex.Keys.PageDown,
|
||||
ex.Keys.PageUp,
|
||||
ex.Keys.Insert,
|
||||
ex.Keys.PrintScreen,
|
||||
];
|
||||
if (invalidKeys.includes(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
get text(): string {
|
||||
return this._text ?? "";
|
||||
if (key === ex.Keys.Backspace) {
|
||||
const firstPart = this.text.slice(0, this._cursorPosition - 1);
|
||||
const secondPart = this.text.slice(this._cursorPosition);
|
||||
this.text = firstPart.concat(secondPart);
|
||||
this.cursorPosition--;
|
||||
this.emit(TextInputEvents.Textchanged, this.text);
|
||||
return;
|
||||
}
|
||||
|
||||
set text(value: string) {
|
||||
this._text = value;
|
||||
this._textElement.text = value;
|
||||
|
||||
this._updateCursor();
|
||||
if (key === ex.Keys.Delete) {
|
||||
const firstPart = this.text.slice(0, this._cursorPosition);
|
||||
const secondPart = this.text.slice(this._cursorPosition + 1);
|
||||
this.text = firstPart.concat(secondPart);
|
||||
this.emit(TextInputEvents.Textchanged, this.text);
|
||||
return;
|
||||
}
|
||||
|
||||
get cursorPosition(): number {
|
||||
return this._cursorPosition;
|
||||
if (key === ex.Keys.ArrowLeft) {
|
||||
this.cursorPosition--;
|
||||
return;
|
||||
}
|
||||
|
||||
set cursorPosition(value: number) {
|
||||
this._cursorPosition = Math.min(Math.max(0, value), this.text.length);
|
||||
this._updateCursor();
|
||||
if (key === ex.Keys.ArrowRight) {
|
||||
this.cursorPosition++;
|
||||
return;
|
||||
}
|
||||
|
||||
private _focus(): void {
|
||||
this._focused = true;
|
||||
this._cursorTimer.start();
|
||||
this._cursor.graphics.isVisible = true;
|
||||
if (key === ex.Keys.ArrowDown) {
|
||||
this.cursorPosition = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
private _unfocus(): void {
|
||||
this._focused = false;
|
||||
this._cursorTimer.stop();
|
||||
this._cursor.graphics.isVisible = false;
|
||||
if (key === ex.Keys.ArrowUp) {
|
||||
this.cursorPosition = this.text.length;
|
||||
return;
|
||||
}
|
||||
|
||||
private _updateCursor(): void {
|
||||
const textBeforeCursor = this.text.slice(0, this._cursorPosition);
|
||||
const textWidth = this._textElement.font.measureTextWithoutCache(textBeforeCursor).width;
|
||||
const x = -this._width / 2 + this.padding + textWidth - 2;
|
||||
this._cursor.pos = ex.vec(x, this.padding);
|
||||
this._cursor.graphics.isVisible = true;
|
||||
this._cursorTimer.reset();
|
||||
}
|
||||
|
||||
private _onKeyPress(event: ex.KeyEvent): void {
|
||||
if (!this._focused) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { key } = event;
|
||||
|
||||
if (key === ex.Keys.Esc) {
|
||||
this._unfocus();
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === ex.Keys.Enter) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( key === ex.Keys.Space) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const invalidKeys = [ex.Keys.Alt, ex.Keys.ShiftLeft, ex.Keys.ShiftRight, ex.Keys.AltLeft, ex.Keys.AltRight, ex.Keys.ControlLeft, ex.Keys.ControlRight,
|
||||
ex.Keys.Home, ex.Keys.End, ex.Keys.PageDown, ex.Keys.PageUp, ex.Keys.Insert, ex.Keys.PrintScreen];
|
||||
if (invalidKeys.includes(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === ex.Keys.Backspace) {
|
||||
const firstPart = this.text.slice(0, this._cursorPosition - 1);
|
||||
const secondPart = this.text.slice(this._cursorPosition);
|
||||
this.text = firstPart.concat(secondPart);
|
||||
this.cursorPosition--;
|
||||
this.emit(TextInputEvents.Textchanged, this.text);
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === ex.Keys.Delete) {
|
||||
const firstPart = this.text.slice(0, this._cursorPosition);
|
||||
const secondPart = this.text.slice(this._cursorPosition + 1);
|
||||
this.text = firstPart.concat(secondPart);
|
||||
this.emit(TextInputEvents.Textchanged, this.text);
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === ex.Keys.ArrowLeft) {
|
||||
this.cursorPosition--;
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === ex.Keys.ArrowRight) {
|
||||
this.cursorPosition++;
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === ex.Keys.ArrowDown) {
|
||||
this.cursorPosition = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === ex.Keys.ArrowUp) {
|
||||
this.cursorPosition = this.text.length;
|
||||
return;
|
||||
}
|
||||
|
||||
this.text = this.text + event.value;
|
||||
this.cursorPosition++;
|
||||
this.emit(TextInputEvents.Textchanged, this.text);
|
||||
}
|
||||
this.text = this.text + event.value;
|
||||
this.cursorPosition++;
|
||||
this.emit(TextInputEvents.Textchanged, this.text);
|
||||
}
|
||||
}
|
||||
@@ -32,12 +32,18 @@ export const maxPlayers = 2;
|
||||
})();
|
||||
|
||||
function dummyGameState(): GameState {
|
||||
const you = { name: "Dubbel", id: 0};
|
||||
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 } }],
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -1,28 +1,32 @@
|
||||
export class Event {
|
||||
constructor() { }
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type EventCtor<T extends Event = Event> = abstract new (...args: any[]) => T;
|
||||
type Events = Record<string, EventCtor>;
|
||||
|
||||
type EventHandlers<E extends Events> = {
|
||||
[K in keyof E]: ((event: InstanceType<E[K]>) => void)
|
||||
}
|
||||
|
||||
[K in keyof E]: (event: InstanceType<E[K]>) => void;
|
||||
};
|
||||
|
||||
export class Evented<E extends Events> {
|
||||
private _observers: {
|
||||
[K in keyof EventHandlers<E>]?: EventHandlers<E>[K][];
|
||||
} = {};
|
||||
private _observers: {
|
||||
[K in keyof EventHandlers<E>]?: EventHandlers<E>[K][];
|
||||
} = {};
|
||||
|
||||
on<K extends keyof EventHandlers<E>>(
|
||||
eventName: K,
|
||||
handler: EventHandlers<E>[K],
|
||||
): void {
|
||||
const observer = (this._observers[eventName] ??= []);
|
||||
observer.push(handler);
|
||||
}
|
||||
|
||||
|
||||
on<K extends keyof EventHandlers<E>>(eventName: K, handler: EventHandlers<E>[K]): void {
|
||||
const observer = (this._observers[eventName] ??= []);
|
||||
observer.push(handler);
|
||||
}
|
||||
|
||||
emit<K extends keyof EventHandlers<E>>(eventName: K, event: InstanceType<E[K]>): void {
|
||||
this._observers[eventName]?.forEach(observer => observer(event));
|
||||
}
|
||||
emit<K extends keyof EventHandlers<E>>(
|
||||
eventName: K,
|
||||
event: InstanceType<E[K]>,
|
||||
): void {
|
||||
this._observers[eventName]?.forEach((observer) => observer(event));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export type LogSource = "Connection" | "GameState";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function log(source: LogSource, ...data: any[]): void {
|
||||
console.log(`${source}: `, ...data);
|
||||
console.log(`${source}: `, ...data);
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import * as ex from 'excalibur';
|
||||
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,
|
||||
@@ -39,5 +39,4 @@ export const disabledFont = new ex.Font({
|
||||
color: ex.Color.Gray,
|
||||
size: 20,
|
||||
shadow,
|
||||
|
||||
})
|
||||
});
|
||||
@@ -5,17 +5,32 @@ 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;
|
||||
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;
|
||||
// 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) },
|
||||
],
|
||||
});
|
||||
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),
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user