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 { GameState } from "./GameState";
|
||||||
import { Client, GameStateRaw, Id } from './GameStateRaw';
|
import { Client, GameStateRaw, Id } from "./GameStateRaw";
|
||||||
import { log } from './utils/debugUtils';
|
import { log } from "./utils/debugUtils";
|
||||||
import { Event, Evented } from './utils/Evented';
|
import { Event, Evented } from "./utils/Evented";
|
||||||
|
|
||||||
type Action = "join" | "rejoin" | "toggleRole" | "toggleReady";
|
type Action = "join" | "rejoin" | "toggleRole" | "toggleReady";
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
action: Action,
|
action: Action;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JoinRequest {
|
interface JoinRequest {
|
||||||
action: "join",
|
action: "join";
|
||||||
name: string,
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RejoinRequest {
|
interface RejoinRequest {
|
||||||
action: "rejoin",
|
action: "rejoin";
|
||||||
id: Id,
|
id: Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ToggleRoleRequest {
|
interface ToggleRoleRequest {
|
||||||
action: "toggleRole"
|
action: "toggleRole";
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ToggleReadyRequest {
|
interface ToggleReadyRequest {
|
||||||
action: "toggleReady"
|
action: "toggleReady";
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GameMessageEvent extends Event {
|
export class GameMessageEvent extends Event {
|
||||||
constructor(public gameState: GameState) {
|
constructor(public gameState: GameState) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ConnectEvent extends Event { }
|
export class ConnectEvent extends Event {}
|
||||||
export class DisconnectEvent extends Event { }
|
export class DisconnectEvent extends Event {}
|
||||||
export class JoinedEvent extends Event { }
|
export class JoinedEvent extends Event {}
|
||||||
export class ConnectOrDisconnectEvent extends Event {
|
export class ConnectOrDisconnectEvent extends Event {
|
||||||
constructor(public connected: boolean) {
|
constructor(public connected: boolean) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ConnectionEvents = {
|
export const ConnectionEvents = {
|
||||||
"game-message": GameMessageEvent,
|
"game-message": GameMessageEvent,
|
||||||
"joined": JoinedEvent,
|
joined: JoinedEvent,
|
||||||
"connect": ConnectEvent,
|
connect: ConnectEvent,
|
||||||
"disconnect": DisconnectEvent,
|
disconnect: DisconnectEvent,
|
||||||
"connect-or-disconnect": ConnectOrDisconnectEvent,
|
"connect-or-disconnect": ConnectOrDisconnectEvent,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export class Connection extends Evented<typeof ConnectionEvents> {
|
export class Connection extends Evented<typeof ConnectionEvents> {
|
||||||
private _socket: WebSocket | null = null;
|
private _socket: WebSocket | null = null;
|
||||||
private _connected = false;
|
private _connected = false;
|
||||||
|
|
||||||
private _waitForJoin = false;
|
private _waitForJoin = false;
|
||||||
private _client : Client | null = null;
|
private _client: Client | null = null;
|
||||||
|
|
||||||
get connected(): boolean {
|
get connected(): boolean {
|
||||||
const { _socket } = this;
|
const { _socket } = this;
|
||||||
return _socket !== null && _socket.readyState === _socket.OPEN;
|
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 {
|
// We only emit game message events when we have joined.
|
||||||
this._socket = new WebSocket("ws://127.0.0.1:8888");
|
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();
|
private _onConnect(): void {
|
||||||
this._socket.onmessage = (event) => this._onMessage(event);
|
log("Connection", "connected");
|
||||||
this._socket.onerror = (event) => this._onError(event);
|
|
||||||
this._socket.onclose = () => this._onDisconnect();
|
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 {
|
// Try again
|
||||||
log("Connection", "received", event)
|
this.connect();
|
||||||
|
}
|
||||||
|
|
||||||
const messageObject = JSON.parse(event.data);
|
private _onError(event: globalThis.Event): void {
|
||||||
|
log("Connection", "error", event);
|
||||||
|
}
|
||||||
|
|
||||||
if (this._waitForJoin) {
|
private _send(request: Request): void {
|
||||||
this._client = messageObject.player as Client;
|
if (!this.connected) {
|
||||||
storeClient(this._client);
|
return;
|
||||||
this._waitForJoin = false;
|
|
||||||
this.emit("joined", new JoinedEvent());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// We only emit game message events when we have joined.
|
|
||||||
if (this._client) {
|
|
||||||
const gameState: GameStateRaw = messageObject.gamestate;
|
|
||||||
const gameMessage = new GameMessageEvent(new GameState(gameState, this._client));
|
|
||||||
this.emit("game-message", gameMessage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _onConnect(): void {
|
const message = JSON.stringify(request);
|
||||||
log("Connection", "connected");
|
this._socket!.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
this._connected = true;
|
join(name: string): void {
|
||||||
|
const joinRequest = {
|
||||||
|
action: "join",
|
||||||
|
name,
|
||||||
|
} as JoinRequest;
|
||||||
|
|
||||||
this.emit("connect", new ConnectEvent());
|
this._waitForJoin = true;
|
||||||
this.emit("connect-or-disconnect", new ConnectOrDisconnectEvent(true));
|
this._send(joinRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _onDisconnect(): void {
|
rejoin(id: Id): void {
|
||||||
log("Connection", "disconnected");
|
const rejoinRequest = {
|
||||||
|
action: "rejoin",
|
||||||
|
id,
|
||||||
|
} as RejoinRequest;
|
||||||
|
|
||||||
// The socket close event is also invoked when a connection attempt fails.
|
this._waitForJoin = true;
|
||||||
// Only emit the disconnect event when we actually had a connection.
|
this._send(rejoinRequest);
|
||||||
if (this._connected) {
|
}
|
||||||
this._connected = false;
|
|
||||||
this.emit("disconnect", new DisconnectEvent());
|
|
||||||
this.emit("connect-or-disconnect", new ConnectOrDisconnectEvent(false));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try again
|
toggleRole(): void {
|
||||||
this.connect();
|
const toggleRoleRequest = {
|
||||||
}
|
action: "toggleRole",
|
||||||
|
} as ToggleRoleRequest;
|
||||||
|
|
||||||
private _onError(event: globalThis.Event): void {
|
this._send(toggleRoleRequest);
|
||||||
log("Connection", "error", event);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private _send(request: Request): void {
|
toggleReady(): void {
|
||||||
if (!this.connected) {
|
const toggleReadyRequest = {
|
||||||
return;
|
action: "toggleReady",
|
||||||
}
|
} as ToggleReadyRequest;
|
||||||
|
|
||||||
const message = JSON.stringify(request);
|
this._send(toggleReadyRequest);
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function storedClient(): Client | null {
|
export function storedClient(): Client | null {
|
||||||
const clientRaw = localStorage.getItem("client");
|
const clientRaw = localStorage.getItem("client");
|
||||||
if (clientRaw == null) {
|
if (clientRaw == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO: Do schema check.
|
// TODO: Do schema check.
|
||||||
return JSON.parse(clientRaw) as Client;
|
return JSON.parse(clientRaw) as Client;
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeClient(client: Client) {
|
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 radius = 20;
|
||||||
const margin = 2;
|
const margin = 2;
|
||||||
|
|
||||||
export class Cups extends ex.Graphic {
|
export class Cups extends ex.Graphic {
|
||||||
protected _drawImage(context: ex.ExcaliburGraphicsContext, x: number, y: number): void {
|
protected _drawImage(
|
||||||
y += radius;
|
context: ex.ExcaliburGraphicsContext,
|
||||||
for (let r = 4; r > 0; r--) {
|
x: number,
|
||||||
x = -r * radius + radius;
|
y: number,
|
||||||
for (let c = 0; c < r; c++) {
|
): void {
|
||||||
context.drawCircle(ex.vec(x, y), radius, ex.Color.Red, ex.Color.White, 2)
|
y += radius;
|
||||||
x += radius * 2 + margin;
|
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 {
|
clone(): ex.Graphic {
|
||||||
return new Cups();
|
return new Cups();
|
||||||
}
|
}
|
||||||
|
|
||||||
get computeHeight(): number {
|
get computeHeight(): number {
|
||||||
return 4 * 2 * radius + 3 * margin;
|
return 4 * 2 * radius + 3 * margin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,44 +2,45 @@ import { ScreenElement, vec } from "excalibur";
|
|||||||
import { ErrorScreenElement } from "../ScreenElements/Error";
|
import { ErrorScreenElement } from "../ScreenElements/Error";
|
||||||
import { spacingS } from "../utils/formattingUtils";
|
import { spacingS } from "../utils/formattingUtils";
|
||||||
|
|
||||||
|
|
||||||
interface ErrorListProperties {
|
interface ErrorListProperties {
|
||||||
width: number;
|
width: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConstructProperties = Partial<ex.ActorArgs> & ErrorListProperties;
|
type ConstructProperties = Partial<ex.ActorArgs> & ErrorListProperties;
|
||||||
|
|
||||||
export class ErrorList extends ScreenElement {
|
export class ErrorList extends ScreenElement {
|
||||||
private _errors: ErrorScreenElement[] = [];
|
private _errors: ErrorScreenElement[] = [];
|
||||||
private _width: number;
|
private _width: number;
|
||||||
|
|
||||||
constructor(config: ConstructProperties){
|
constructor(config: ConstructProperties) {
|
||||||
super(config);
|
super(config);
|
||||||
this._width = config.width;
|
this._width = config.width;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addError(text: string): () => void {
|
public addError(text: string): () => void {
|
||||||
const index = this._errors.length;
|
const index = this._errors.length;
|
||||||
|
|
||||||
const errorScreenElement = new ErrorScreenElement({
|
const errorScreenElement = new ErrorScreenElement({
|
||||||
text,
|
text,
|
||||||
width: this._width,
|
width: this._width,
|
||||||
})
|
});
|
||||||
this.addChild(errorScreenElement);
|
this.addChild(errorScreenElement);
|
||||||
this._errors.push(errorScreenElement);
|
this._errors.push(errorScreenElement);
|
||||||
|
|
||||||
const y = this._errors.reduce((acc, cur) => acc + cur.height, 0) + index * this._padding;
|
const y =
|
||||||
errorScreenElement.pos = vec(0, -y);
|
this._errors.reduce((acc, cur) => acc + cur.height, 0) +
|
||||||
|
index * this._padding;
|
||||||
|
errorScreenElement.pos = vec(0, -y);
|
||||||
|
|
||||||
const removeHandle = () => {
|
const removeHandle = () => {
|
||||||
this.removeChild(errorScreenElement);
|
this.removeChild(errorScreenElement);
|
||||||
this._errors.splice(index, 1);
|
this._errors.splice(index, 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
return removeHandle;
|
return removeHandle;
|
||||||
}
|
}
|
||||||
|
|
||||||
private get _padding(): number {
|
private get _padding(): number {
|
||||||
return spacingS;
|
return spacingS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,101 +1,108 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from "excalibur";
|
||||||
import { TextInput, TextInputEvents } from '../ScreenElements/TextInput';
|
import { TextInput, TextInputEvents } from "../ScreenElements/TextInput";
|
||||||
import { Button } from '../ScreenElements/Button';
|
import { Button } from "../ScreenElements/Button";
|
||||||
import { paragraphFont } from '../utils/fontUtil';
|
import { paragraphFont } from "../utils/fontUtil";
|
||||||
import { Text } from '../ScreenElements/Text';
|
import { Text } from "../ScreenElements/Text";
|
||||||
import { spacingM } from '../utils/formattingUtils';
|
import { spacingM } from "../utils/formattingUtils";
|
||||||
import { Connection, storedClient } from '../Connection';
|
import { Connection, storedClient } from "../Connection";
|
||||||
import { Id } from '../GameStateRaw';
|
import { Id } from "../GameStateRaw";
|
||||||
|
|
||||||
export class PlayerNameInput extends ex.ScreenElement {
|
export class PlayerNameInput extends ex.ScreenElement {
|
||||||
private _nameLabel: Text = null!;
|
private _nameLabel: Text = null!;
|
||||||
private _nameInput: TextInput = null!;
|
private _nameInput: TextInput = null!;
|
||||||
private _joinButton: Button = null!;
|
private _joinButton: Button = null!;
|
||||||
private _rejoinButton: Button | null = 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) {
|
constructor(
|
||||||
super();
|
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._nameInput.on(TextInputEvents.Textchanged, () =>
|
||||||
this._nameLabel = new Text({
|
this._updateJoinButtonDisabled(),
|
||||||
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 });
|
let y = 0;
|
||||||
this._nameInput.anchor = ex.vec(0.5, 0);
|
this._nameLabel.pos = ex.vec(0, y);
|
||||||
this.addChild(this._nameInput);
|
y += this._nameLabel.font.size + spacingM;
|
||||||
|
|
||||||
this._joinButton = new Button({
|
this._nameInput.pos = ex.vec(0, y);
|
||||||
text: "Join",
|
y += this._nameInput.height + spacingM;
|
||||||
anchor: ex.vec(0.5, 0),
|
|
||||||
onClick: () => this._joinCallback(this._nameInput.text),
|
|
||||||
disabled: true,
|
|
||||||
});
|
|
||||||
this.addChild(this._joinButton);
|
|
||||||
|
|
||||||
const rejoinClient = storedClient();
|
this._joinButton.pos = ex.vec(0, y);
|
||||||
if (rejoinClient != null) {
|
y += this._joinButton.height + spacingM;
|
||||||
this._rejoinButton = new Button({
|
|
||||||
text: "Rejoin as " + rejoinClient.name,
|
|
||||||
anchor: ex.vec(0.5, 0),
|
|
||||||
onClick: () => this._rejoinCallback(rejoinClient.id),
|
|
||||||
});
|
|
||||||
|
|
||||||
this.addChild(this._rejoinButton);
|
if (this._rejoinButton != null) {
|
||||||
}
|
this._rejoinButton.pos = ex.vec(0, y);
|
||||||
|
y += this._rejoinButton.height + spacingM;
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get playerName(): string {
|
this._height = y;
|
||||||
return this._nameInput.text;
|
|
||||||
}
|
|
||||||
|
|
||||||
override get height(): number {
|
this._connection.on("connect", () => this._onConnect());
|
||||||
return this._height;
|
this._connection.on("disconnect", () => this._onDisconnect());
|
||||||
}
|
}
|
||||||
|
|
||||||
clear(): void {
|
get playerName(): string {
|
||||||
this._nameInput.text = "";
|
return this._nameInput.text;
|
||||||
this._updateJoinButtonDisabled();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private _onConnect(): void {
|
override get height(): number {
|
||||||
this._updateJoinButtonDisabled();
|
return this._height;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _onDisconnect(): void {
|
clear(): void {
|
||||||
this._updateJoinButtonDisabled();
|
this._nameInput.text = "";
|
||||||
}
|
this._updateJoinButtonDisabled();
|
||||||
|
}
|
||||||
|
|
||||||
private _updateJoinButtonDisabled(): void {
|
private _onConnect(): void {
|
||||||
const disabled = !this._connection.connected || this._nameInput.text === "";
|
this._updateJoinButtonDisabled();
|
||||||
this._joinButton.disabled = disabled;
|
}
|
||||||
}
|
|
||||||
|
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";
|
import { spacingM } from "../utils/formattingUtils";
|
||||||
|
|
||||||
export class Title extends ScreenElement {
|
export class Title extends ScreenElement {
|
||||||
private _title: Text = null!;
|
private _title: Text = null!;
|
||||||
private _subTitle: Text = null!;
|
private _subTitle: Text = null!;
|
||||||
|
|
||||||
onInitialize(_engine: Engine): void {
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
// Title
|
onInitialize(_engine: Engine): void {
|
||||||
this._title = new Text({
|
// Title
|
||||||
text: "Auto Beer Pong",
|
this._title = new Text({
|
||||||
font: titleFont,
|
text: "Auto Beer Pong",
|
||||||
});
|
font: titleFont,
|
||||||
this._title.anchor = vec(0.5, 0);
|
});
|
||||||
this.addChild(this._title);
|
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
|
get height(): number {
|
||||||
this._subTitle = new Text({
|
return this._titleHeight + subTitleFont.size + spacingM;
|
||||||
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 {
|
private get _titleHeight(): number {
|
||||||
return this._titleHeight + subTitleFont.size + spacingM;
|
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 { maxPlayers } from "./main";
|
||||||
import { log } from "./utils/debugUtils";
|
import { log } from "./utils/debugUtils";
|
||||||
|
|
||||||
export class GameState {
|
export class GameState {
|
||||||
_errors: string[] = [];
|
_errors: string[] = [];
|
||||||
|
|
||||||
constructor(public readonly gameStateRaw: GameStateRawReadonly, readonly client: Client) {
|
constructor(
|
||||||
this._validate();
|
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) {
|
||||||
if (!this.isPlayer && !this.isSpectator) {
|
this._errors.push("You cannot be both player and spectator.");
|
||||||
this._errors.push("You must be either a player or spectator.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.isPlayer && this.isSpectator) {
|
|
||||||
this._errors.push("You cannot be both player and spectator.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._errors.length > 0) {
|
|
||||||
log("GameState", this._errors);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get players(): Player[] {
|
if (this._errors.length > 0) {
|
||||||
return this.gameStateRaw.players;
|
log("GameState", this._errors);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get spectators(): Spectator[] {
|
get players(): Player[] {
|
||||||
return this.gameStateRaw.spectators;
|
return this.gameStateRaw.players;
|
||||||
}
|
}
|
||||||
|
|
||||||
get isFull(): boolean {
|
get spectators(): Spectator[] {
|
||||||
return this.players.length >= maxPlayers;
|
return this.gameStateRaw.spectators;
|
||||||
}
|
}
|
||||||
|
|
||||||
get isPlayer(): boolean {
|
get isFull(): boolean {
|
||||||
return this.players.map(p => p.player.id).includes(this.client.id);
|
return this.players.length >= maxPlayers;
|
||||||
}
|
}
|
||||||
|
|
||||||
get isReady(): boolean {
|
get isPlayer(): boolean {
|
||||||
return this.getPlayerById(this.client.id)!.ready;
|
return this.players.map((p) => p.player.id).includes(this.client.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
get isSpectator(): boolean {
|
get isReady(): boolean {
|
||||||
return this.spectators.map(p => p.player.id).includes(this.client.id);
|
return this.getPlayerById(this.client.id)!.ready;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPlayerById(id: Id): Player | undefined {
|
get isSpectator(): boolean {
|
||||||
return this.players.find(p => p.player.id === id);
|
return this.spectators.map((p) => p.player.id).includes(this.client.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
getSpectatorById(id: Id): Spectator | undefined {
|
getPlayerById(id: Id): Player | undefined {
|
||||||
return this.spectators.find(p => p.player.id === id);
|
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 type Id = number;
|
||||||
|
|
||||||
export interface GameStateRaw {
|
export interface GameStateRaw {
|
||||||
players: Player[];
|
players: Player[];
|
||||||
spectators: Spectator[];
|
spectators: Spectator[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GameStateRawReadonly = Readonly<GameStateRaw>
|
export type GameStateRawReadonly = Readonly<GameStateRaw>;
|
||||||
|
|
||||||
export interface Player {
|
export interface Player {
|
||||||
player: Client;
|
player: Client;
|
||||||
ready: boolean;
|
ready: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Spectator {
|
export interface Spectator {
|
||||||
player: Client;
|
player: Client;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Client {
|
export interface Client {
|
||||||
id: Id;
|
id: Id;
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from "excalibur";
|
||||||
import { GameState } from '../GameState';
|
import { GameState } from "../GameState";
|
||||||
|
|
||||||
export class GameStateScene extends ex.Scene {
|
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 * as ex from "excalibur";
|
||||||
import { spacingL, spacingM } from '../utils/formattingUtils';
|
import { spacingL, spacingM } from "../utils/formattingUtils";
|
||||||
import { Cups } from '../Cups';
|
import { Cups } from "../Cups";
|
||||||
import { GameStateScene } from './GameStateScene';
|
import { GameStateScene } from "./GameStateScene";
|
||||||
import { PlayerListScreenElement } from '../ScreenElements/PlayerListScreenElement';
|
import { PlayerListScreenElement } from "../ScreenElements/PlayerListScreenElement";
|
||||||
import { ClientListScreenElement } from '../ScreenElements/ClientListScreenElement';
|
import { ClientListScreenElement } from "../ScreenElements/ClientListScreenElement";
|
||||||
import { Button } from '../ScreenElements/Button';
|
import { Button } from "../ScreenElements/Button";
|
||||||
import { PlayerNameInput } from '../GameElements/PlayerNameInput';
|
import { PlayerNameInput } from "../GameElements/PlayerNameInput";
|
||||||
import { Connection, GameMessageEvent } from '../Connection';
|
import { Connection, GameMessageEvent } from "../Connection";
|
||||||
import { Title } from '../GameElements/Title';
|
import { Title } from "../GameElements/Title";
|
||||||
import { ErrorList } from '../GameElements/ErrorList';
|
import { ErrorList } from "../GameElements/ErrorList";
|
||||||
import { GameState } from '../GameState';
|
import { GameState } from "../GameState";
|
||||||
import { maxPlayers } from '../main';
|
import { maxPlayers } from "../main";
|
||||||
import { Id } from '../GameStateRaw';
|
import { Id } from "../GameStateRaw";
|
||||||
|
|
||||||
const tableWidth = 400;
|
const tableWidth = 400;
|
||||||
|
|
||||||
export type Screens = "join" | "lobby";
|
export type Screens = "join" | "lobby";
|
||||||
|
|
||||||
export class LobbyScene extends GameStateScene {
|
export class LobbyScene extends GameStateScene {
|
||||||
private _table: ex.ScreenElement = null!;
|
private _table: ex.ScreenElement = null!;
|
||||||
private _cups: ex.ScreenElement = null!;
|
private _cups: ex.ScreenElement = null!;
|
||||||
|
|
||||||
private _title: Title = null!;
|
private _title: Title = null!;
|
||||||
|
|
||||||
private _bar1: ex.ScreenElement = null!;
|
private _bar1: ex.ScreenElement = null!;
|
||||||
private _bar2: ex.ScreenElement = null!;
|
private _bar2: ex.ScreenElement = null!;
|
||||||
private _bar3: ex.ScreenElement = null!;
|
private _bar3: ex.ScreenElement = null!;
|
||||||
|
|
||||||
private _playerList: PlayerListScreenElement = null!;
|
private _playerList: PlayerListScreenElement = null!;
|
||||||
private _spectatorsList: ClientListScreenElement = null!;
|
private _spectatorsList: ClientListScreenElement = null!;
|
||||||
|
|
||||||
private _joinSpectatorButton: Button = null!;
|
private _joinSpectatorButton: Button = null!;
|
||||||
private _joinPlayerButton: Button = null!;
|
private _joinPlayerButton: Button = null!;
|
||||||
|
|
||||||
private _readyButton: Button = null!;
|
private _readyButton: Button = null!;
|
||||||
private _unreadyButton: Button = null!;
|
private _unreadyButton: Button = null!;
|
||||||
|
|
||||||
private _playerNameInput: PlayerNameInput = null!;
|
private _playerNameInput: PlayerNameInput = null!;
|
||||||
|
|
||||||
private _errorList: ErrorList = null!;
|
private _errorList: ErrorList = null!;
|
||||||
private _noConnectionErrorHandle: (() => void) | undefined;
|
private _noConnectionErrorHandle: (() => void) | undefined;
|
||||||
|
|
||||||
private _screen: "join" | "lobby" = "join";
|
private _screen: "join" | "lobby" = "join";
|
||||||
|
|
||||||
constructor(private _connection: Connection) {
|
constructor(private _connection: Connection) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this._connection.on("joined", () => this.setScreen("lobby"));
|
this._connection.on("joined", () => this.setScreen("lobby"));
|
||||||
this._connection.on("disconnect", () => {
|
this._connection.on("disconnect", () => {
|
||||||
this._playerNameInput.clear();
|
this._playerNameInput.clear();
|
||||||
this.setScreen("join");
|
this.setScreen("join");
|
||||||
});
|
});
|
||||||
this._connection.on("game-message", (e: GameMessageEvent) => this.updateFromGameState(e.gameState));
|
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 {
|
public updateFromGameState(gameState: GameState): void {
|
||||||
const tableBorder = 10;
|
this._playerList.updateFromGameState(gameState.players, gameState.client);
|
||||||
const paddedTableWidth = tableWidth - 2 * tableBorder - 2 * spacingM;
|
this._spectatorsList.updateFromGameState(
|
||||||
// Table background
|
gameState.spectators,
|
||||||
this._table = new ex.ScreenElement({
|
gameState.client,
|
||||||
anchor: ex.vec(0.5, 0)
|
);
|
||||||
});
|
this._updatePositions();
|
||||||
this.add(this._table);
|
this._updateVisibility(gameState);
|
||||||
|
}
|
||||||
|
|
||||||
const points = [
|
public setScreen(screen: Screens): void {
|
||||||
ex.vec(0, tableBorder),
|
this._screen = screen;
|
||||||
ex.vec(tableBorder, 0),
|
this._updatePositions();
|
||||||
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();
|
private _join(name: string): void {
|
||||||
this._bar2 = this._createBar();
|
this._connection.join(name);
|
||||||
this._bar3 = this._createBar();
|
}
|
||||||
|
|
||||||
// Cups
|
private _rejoin(id: Id): void {
|
||||||
this._cups = new ex.ScreenElement({})
|
this._connection.rejoin(id);
|
||||||
this._cups.anchor.setTo(0.5, 0);
|
}
|
||||||
this._cups.graphics.add(new Cups());
|
|
||||||
this.add(this._cups);
|
|
||||||
|
|
||||||
// Title
|
private _joinAsPlayer(): void {
|
||||||
this._title = new Title();
|
this._connection.toggleRole();
|
||||||
this.add(this._title);
|
}
|
||||||
|
|
||||||
this._playerList = new PlayerListScreenElement("Players", maxPlayers);
|
private _joinAsSpectator(): void {
|
||||||
this._playerList.anchor.setTo(0.5, 0);
|
this._connection.toggleRole();
|
||||||
|
}
|
||||||
|
|
||||||
this._joinPlayerButton = new Button({
|
private _ready(): void {
|
||||||
text: "Join as player",
|
this._connection.toggleReady();
|
||||||
anchor: ex.vec(0.5, 0),
|
}
|
||||||
onClick: () => this._joinAsPlayer(),
|
|
||||||
});
|
|
||||||
|
|
||||||
this._spectatorsList = new ClientListScreenElement("Spectators");
|
private _unready(): void {
|
||||||
this._spectatorsList.anchor.setTo(0.5, 0);
|
this._connection.toggleReady();
|
||||||
|
}
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,136 +1,157 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from "excalibur";
|
||||||
import { Text } from './Text';
|
import { Text } from "./Text";
|
||||||
import { paragraphFont } from '../utils/fontUtil';
|
import { paragraphFont } from "../utils/fontUtil";
|
||||||
import { spacingS } from '../utils/formattingUtils';
|
import { spacingS } from "../utils/formattingUtils";
|
||||||
import { lowerBarHeightDown, makeStyledRectangle } from '../utils/styledRectangles';
|
import {
|
||||||
|
lowerBarHeightDown,
|
||||||
|
makeStyledRectangle,
|
||||||
|
} from "../utils/styledRectangles";
|
||||||
|
|
||||||
interface ButtonProperties {
|
interface ButtonProperties {
|
||||||
text?: string;
|
text?: string;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
color?: ex.Color,
|
color?: ex.Color;
|
||||||
onClick?: (event: ex.PointerEvent) => void;
|
onClick?: (event: ex.PointerEvent) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConstructProperties = ex.ActorArgs & ButtonProperties;
|
type ConstructProperties = ex.ActorArgs & ButtonProperties;
|
||||||
|
|
||||||
export class Button extends ex.ScreenElement {
|
export class Button extends ex.ScreenElement {
|
||||||
private _text?: string;
|
private _text?: string;
|
||||||
private _textElement: Text = null!;
|
private _textElement: Text = null!;
|
||||||
private _color?: ex.Color;
|
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) {
|
constructor(config?: ConstructProperties) {
|
||||||
super(config);
|
super(config);
|
||||||
this._text = config?.text;
|
this._text = config?.text;
|
||||||
this._disabled = config?.disabled ?? false;
|
this._disabled = config?.disabled ?? false;
|
||||||
this._color = config?.color;
|
this._color = config?.color;
|
||||||
this._onClick = config?.onClick;
|
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 {
|
this._down = true;
|
||||||
const { height, width } = this;
|
this._updateGraphic("clicked");
|
||||||
|
}
|
||||||
|
|
||||||
this._textElement = new Text({
|
onPointerUp(event: ex.PointerEvent): void {
|
||||||
text: this._text,
|
if (this._disabled || !this._down) {
|
||||||
font: paragraphFont,
|
return;
|
||||||
})
|
|
||||||
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 {
|
this._down = false;
|
||||||
return this._text ?? "";
|
|
||||||
|
const { _onClick } = this;
|
||||||
|
if (_onClick != null) {
|
||||||
|
_onClick(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
get disabled(): boolean {
|
this._updateGraphic("hover");
|
||||||
return this._disabled;
|
}
|
||||||
|
|
||||||
|
// 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._updateGraphic("hover");
|
||||||
this._disabled = value;
|
}
|
||||||
this._updateGraphic();
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
onPointerLeave(_event: ex.PointerEvent): void {
|
||||||
|
if (this._disabled) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
get width(): number {
|
this._updateGraphic("idle");
|
||||||
return paragraphFont.measureTextWithoutCache(this.text).width + this.padding;
|
}
|
||||||
|
|
||||||
|
private _updateGraphic(state: string = "idle"): void {
|
||||||
|
if (this.disabled) {
|
||||||
|
this.graphics.use("disabled");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
get height(): number {
|
this.graphics.use(state);
|
||||||
return paragraphFont.size + this.padding;
|
this._textElement.pos =
|
||||||
}
|
state === "clicked"
|
||||||
|
? ex.vec(0, this.height / 2 + lowerBarHeightDown)
|
||||||
get padding(): number {
|
: ex.vec(0, this.height / 2);
|
||||||
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) ;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,50 +1,52 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from "excalibur";
|
||||||
import { paragraphFont } from '../utils/fontUtil';
|
import { paragraphFont } from "../utils/fontUtil";
|
||||||
import { spacingS } from '../utils/formattingUtils';
|
import { spacingS } from "../utils/formattingUtils";
|
||||||
|
|
||||||
interface CheckboxProperties {
|
interface CheckboxProperties {
|
||||||
checked: boolean;
|
checked: boolean;
|
||||||
};
|
}
|
||||||
|
|
||||||
type ConstructProperties = ex.ActorArgs & CheckboxProperties;
|
type ConstructProperties = ex.ActorArgs & CheckboxProperties;
|
||||||
|
|
||||||
export class Checkbox extends ex.ScreenElement {
|
export class Checkbox extends ex.ScreenElement {
|
||||||
private _checked: boolean;
|
private _checked: boolean;
|
||||||
|
|
||||||
constructor(config: ConstructProperties) {
|
constructor(config: ConstructProperties) {
|
||||||
super(config);
|
super(config);
|
||||||
this._checked = config.checked;
|
this._checked = config.checked;
|
||||||
}
|
}
|
||||||
|
|
||||||
onInitialize(): void {
|
onInitialize(): void {
|
||||||
const size = paragraphFont.size - spacingS;
|
const size = paragraphFont.size - spacingS;
|
||||||
const color = ex.Color.Green;
|
const color = ex.Color.Green;
|
||||||
|
|
||||||
const checkedGraphics = new ex.GraphicsGroup({
|
const checkedGraphics = new ex.GraphicsGroup({
|
||||||
members: [
|
members: [
|
||||||
{ graphic: new ex.Circle({ radius: size / 2, color }), offset: ex.vec(0, 0) },
|
{
|
||||||
|
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._updateGraphic();
|
||||||
this.graphics.add('unchecked', uncheckedGraphics);
|
}
|
||||||
|
|
||||||
this._updateGraphic();
|
get checked(): boolean {
|
||||||
}
|
return this._checked;
|
||||||
|
}
|
||||||
|
|
||||||
get checked(): boolean {
|
set checked(checked: boolean) {
|
||||||
return this._checked;
|
this._checked = checked;
|
||||||
}
|
this._updateGraphic();
|
||||||
|
}
|
||||||
|
|
||||||
set checked(checked: boolean) {
|
private _updateGraphic(): void {
|
||||||
this._checked = checked;
|
this.graphics.use(this.checked ? "checked" : "unchecked");
|
||||||
this._updateGraphic();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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 { spacingS } from "../utils/formattingUtils";
|
||||||
import { headingFont, paragraphFont } from '../utils/fontUtil';
|
import { headingFont, paragraphFont } from "../utils/fontUtil";
|
||||||
import { Text } from './Text';
|
import { Text } from "./Text";
|
||||||
import { Client, Player, Spectator } from '../GameStateRaw';
|
import { Client, Player, Spectator } from "../GameStateRaw";
|
||||||
|
|
||||||
const clientColor = ex.Color.fromHex("40E0D0")
|
const clientColor = ex.Color.fromHex("40E0D0");
|
||||||
|
|
||||||
export class ClientListScreenElement extends ex.ScreenElement {
|
export class ClientListScreenElement extends ex.ScreenElement {
|
||||||
protected _clients: Player[] | Spectator[] = [];
|
protected _clients: Player[] | Spectator[] = [];
|
||||||
protected _client: Client | null = null;
|
protected _client: Client | null = null;
|
||||||
|
|
||||||
protected heading: Text = null!;
|
protected heading: Text = null!;
|
||||||
protected entities: ex.Actor[] = [];
|
protected entities: ex.Actor[] = [];
|
||||||
|
|
||||||
constructor(private _headingText: string) {
|
constructor(private _headingText: string) {
|
||||||
super();
|
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 {
|
if (this._clients.length === 0) {
|
||||||
this.heading = new Text({
|
const label = new Text({
|
||||||
text: this._headingText,
|
text: "No one cares.",
|
||||||
font: headingFont
|
font: paragraphFont,
|
||||||
});
|
});
|
||||||
this.heading.anchor.setTo(0.5, 0);
|
label.pos = ex.vec(0, y);
|
||||||
this.addChild(this.heading);
|
label.anchor = ex.vec(0.5, 0);
|
||||||
|
this.addChild(label);
|
||||||
this.updateList();
|
this.entities.push(label);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get childrenHeight(): number {
|
protected createLabel(
|
||||||
return this.computeChildrenHeight(this._clients.length);
|
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 = [];
|
||||||
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 = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,53 +4,53 @@ import { paragraphFont } from "../utils/fontUtil";
|
|||||||
import { spacingS } from "../utils/formattingUtils";
|
import { spacingS } from "../utils/formattingUtils";
|
||||||
|
|
||||||
interface ErrorScreenElementProperties {
|
interface ErrorScreenElementProperties {
|
||||||
text: string;
|
text: string;
|
||||||
width: number;
|
width: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConstructProperties = Partial<ex.ActorArgs> & ErrorScreenElementProperties;
|
type ConstructProperties = Partial<ex.ActorArgs> & ErrorScreenElementProperties;
|
||||||
|
|
||||||
export class ErrorScreenElement extends ScreenElement {
|
export class ErrorScreenElement extends ScreenElement {
|
||||||
private _text: string;
|
private _text: string;
|
||||||
private _width: number;
|
private _width: number;
|
||||||
private _backgroundElement: ex.ScreenElement = null!;
|
private _backgroundElement: ex.ScreenElement = null!;
|
||||||
private _textElement: Text = null!;
|
private _textElement: Text = null!;
|
||||||
|
|
||||||
constructor(config: ConstructProperties) {
|
constructor(config: ConstructProperties) {
|
||||||
super(config);
|
super(config);
|
||||||
this._text = config.text;
|
this._text = config.text;
|
||||||
this._width = config.width;
|
this._width = config.width;
|
||||||
}
|
}
|
||||||
|
|
||||||
onInitialize(_engine: Engine): void {
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
this._textElement = new Text({
|
onInitialize(_engine: Engine): void {
|
||||||
text: this.errorText,
|
this._textElement = new Text({
|
||||||
font: paragraphFont
|
text: this.errorText,
|
||||||
});
|
font: paragraphFont,
|
||||||
this._textElement.pos = vec(this.padding, this.padding);
|
});
|
||||||
|
this._textElement.pos = vec(this.padding, this.padding);
|
||||||
|
|
||||||
this._backgroundElement = new ScreenElement();
|
this._backgroundElement = new ScreenElement();
|
||||||
const background = new Rectangle({
|
const background = new Rectangle({
|
||||||
width: this._width,
|
width: this._width,
|
||||||
height: this.height,
|
height: this.height,
|
||||||
color: Color.Red,
|
color: Color.Red,
|
||||||
});
|
});
|
||||||
this._backgroundElement.graphics.add(background);
|
this._backgroundElement.graphics.add(background);
|
||||||
|
|
||||||
|
this.addChild(this._backgroundElement);
|
||||||
|
this.addChild(this._textElement);
|
||||||
|
}
|
||||||
|
|
||||||
this.addChild(this._backgroundElement);
|
get padding(): number {
|
||||||
this.addChild(this._textElement);
|
return spacingS;
|
||||||
}
|
}
|
||||||
|
|
||||||
get padding(): number {
|
override get height(): number {
|
||||||
return spacingS;
|
return paragraphFont.size + 2 * this.padding;
|
||||||
}
|
}
|
||||||
|
|
||||||
override get height(): number {
|
private get errorText(): string {
|
||||||
return paragraphFont.size + 2 * this.padding;
|
return `Error: ${this._text}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private get errorText(): string {
|
|
||||||
return `Error: ${this._text}`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,59 +1,59 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from "excalibur";
|
||||||
import { spacingS } from '../utils/formattingUtils';
|
import { spacingS } from "../utils/formattingUtils";
|
||||||
import { disabledFont } from '../utils/fontUtil';
|
import { disabledFont } from "../utils/fontUtil";
|
||||||
import { Text } from './Text';
|
import { Text } from "./Text";
|
||||||
import { Checkbox } from './Checkbox';
|
import { Checkbox } from "./Checkbox";
|
||||||
import { Player } from '../GameStateRaw';
|
import { Player } from "../GameStateRaw";
|
||||||
import { ClientListScreenElement } from './ClientListScreenElement';
|
import { ClientListScreenElement } from "./ClientListScreenElement";
|
||||||
|
|
||||||
|
|
||||||
export class PlayerListScreenElement extends ClientListScreenElement {
|
export class PlayerListScreenElement extends ClientListScreenElement {
|
||||||
protected override _clients: Player[] = [];
|
protected override _clients: Player[] = [];
|
||||||
constructor(headingText: string, private _maxPlayers: number) {
|
constructor(
|
||||||
super(headingText);
|
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 {
|
for (let i = clients.length; i < this._maxPlayers; i++) {
|
||||||
this.clearLabelEntities();
|
const label = new Text({
|
||||||
|
text: "Empty",
|
||||||
let y = this.heading.font.size + spacingS;
|
font: disabledFont,
|
||||||
|
});
|
||||||
const clients = this._clients.slice(0, this._maxPlayers);
|
label.pos.setTo(0, y);
|
||||||
|
label.anchor.setTo(0.5, 0);
|
||||||
for(const currentClient of clients) {
|
y += label.font.size + spacingS;
|
||||||
const { name, id } = currentClient.player;
|
this.addChild(label);
|
||||||
const isClient = id === this._client?.id;
|
this.entities.push(label);
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override get childrenHeight(): number {
|
override get childrenHeight(): number {
|
||||||
return this.computeChildrenHeight(this._maxPlayers);
|
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;
|
type ConstructProperties = ex.LabelOptions & ex.ActorArgs;
|
||||||
|
|
||||||
export class Text extends ex.Label {
|
export class Text extends ex.Label {
|
||||||
constructor(options?: ConstructProperties) {
|
constructor(options?: ConstructProperties) {
|
||||||
super(options);
|
super(options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,209 +1,226 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from "excalibur";
|
||||||
import { paragraphFont } from '../utils/fontUtil';
|
import { paragraphFont } from "../utils/fontUtil";
|
||||||
import { Text } from './Text';
|
import { Text } from "./Text";
|
||||||
import { spacingS } from '../utils/formattingUtils';
|
import { spacingS } from "../utils/formattingUtils";
|
||||||
|
|
||||||
interface TextInputProperties {
|
interface TextInputProperties {
|
||||||
text?: string;
|
text?: string;
|
||||||
width: number;
|
width: number;
|
||||||
};
|
}
|
||||||
|
|
||||||
type ConstructProperties = ex.ActorArgs & TextInputProperties;
|
type ConstructProperties = ex.ActorArgs & TextInputProperties;
|
||||||
|
|
||||||
type TextInputEvents = {
|
type TextInputEvents = {
|
||||||
textChanged: TextChangedEvent;
|
textChanged: TextChangedEvent;
|
||||||
}
|
};
|
||||||
|
|
||||||
export class TextChangedEvent extends ex.GameEvent<string> {
|
export class TextChangedEvent extends ex.GameEvent<string> {
|
||||||
constructor(public text: string) {
|
constructor(public text: string) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TextInputEvents = {
|
export const TextInputEvents = {
|
||||||
Textchanged: "textchanged"
|
Textchanged: "textchanged",
|
||||||
} as const
|
} as const;
|
||||||
|
|
||||||
export class TextInput extends ex.ScreenElement {
|
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 _width: number;
|
||||||
private _text?: string;
|
private _text?: string;
|
||||||
private _textElement: Text = null!;
|
private _textElement: Text = null!;
|
||||||
|
|
||||||
private _focused = false;
|
private _focused = false;
|
||||||
|
|
||||||
private _cursor: ex.ScreenElement = null!;
|
private _cursor: ex.ScreenElement = null!;
|
||||||
private _cursorPosition = 0;
|
private _cursorPosition = 0;
|
||||||
private _cursorTimer: ex.Timer = null!
|
private _cursorTimer: ex.Timer = null!;
|
||||||
|
|
||||||
constructor(config: ConstructProperties) {
|
constructor(config: ConstructProperties) {
|
||||||
super(config);
|
super(config);
|
||||||
this._width = config?.width;
|
this._width = config?.width;
|
||||||
this._text = config?.text;
|
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 { key } = event;
|
||||||
const width = this._width;
|
|
||||||
const height = this.height;
|
|
||||||
|
|
||||||
this._textElement = new Text({
|
if (key === ex.Keys.Esc) {
|
||||||
text: this.text,
|
this._unfocus();
|
||||||
font: paragraphFont,
|
return;
|
||||||
});
|
|
||||||
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 {
|
if (key === ex.Keys.Enter) {
|
||||||
return this.textHeight + 2 * spacingS;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
get textHeight(): number {
|
if (key === ex.Keys.Space) {
|
||||||
return paragraphFont.size;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
get padding(): number {
|
const invalidKeys = [
|
||||||
return spacingS;
|
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 {
|
if (key === ex.Keys.Backspace) {
|
||||||
return this._text ?? "";
|
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) {
|
if (key === ex.Keys.Delete) {
|
||||||
this._text = value;
|
const firstPart = this.text.slice(0, this._cursorPosition);
|
||||||
this._textElement.text = value;
|
const secondPart = this.text.slice(this._cursorPosition + 1);
|
||||||
|
this.text = firstPart.concat(secondPart);
|
||||||
this._updateCursor();
|
this.emit(TextInputEvents.Textchanged, this.text);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
get cursorPosition(): number {
|
if (key === ex.Keys.ArrowLeft) {
|
||||||
return this._cursorPosition;
|
this.cursorPosition--;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
set cursorPosition(value: number) {
|
if (key === ex.Keys.ArrowRight) {
|
||||||
this._cursorPosition = Math.min(Math.max(0, value), this.text.length);
|
this.cursorPosition++;
|
||||||
this._updateCursor();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _focus(): void {
|
if (key === ex.Keys.ArrowDown) {
|
||||||
this._focused = true;
|
this.cursorPosition = 0;
|
||||||
this._cursorTimer.start();
|
return;
|
||||||
this._cursor.graphics.isVisible = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _unfocus(): void {
|
if (key === ex.Keys.ArrowUp) {
|
||||||
this._focused = false;
|
this.cursorPosition = this.text.length;
|
||||||
this._cursorTimer.stop();
|
return;
|
||||||
this._cursor.graphics.isVisible = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _updateCursor(): void {
|
this.text = this.text + event.value;
|
||||||
const textBeforeCursor = this.text.slice(0, this._cursorPosition);
|
this.cursorPosition++;
|
||||||
const textWidth = this._textElement.font.measureTextWithoutCache(textBeforeCursor).width;
|
this.emit(TextInputEvents.Textchanged, this.text);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -32,12 +32,18 @@ export const maxPlayers = 2;
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
function dummyGameState(): GameState {
|
function dummyGameState(): GameState {
|
||||||
const you = { name: "Dubbel", id: 0};
|
const you = { name: "Dubbel", id: 0 };
|
||||||
|
|
||||||
const dummyState: GameStateRaw = {
|
const dummyState: GameStateRaw = {
|
||||||
players: [{player: { name: "Dubbel", id: 0}, ready: true }, { player: { name: "Yeah", id: 1 }, ready: false }],
|
players: [
|
||||||
spectators: [ { player: { name: "Dubbel", id: 2} }, { player: { name: "Cocksucker", id: 3 } }],
|
{ 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);
|
return new GameState(dummyState, you);
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,32 @@
|
|||||||
export class Event {
|
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 EventCtor<T extends Event = Event> = abstract new (...args: any[]) => T;
|
||||||
type Events = Record<string, EventCtor>;
|
type Events = Record<string, EventCtor>;
|
||||||
|
|
||||||
type EventHandlers<E extends Events> = {
|
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> {
|
export class Evented<E extends Events> {
|
||||||
private _observers: {
|
private _observers: {
|
||||||
[K in keyof EventHandlers<E>]?: EventHandlers<E>[K][];
|
[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);
|
||||||
|
}
|
||||||
|
|
||||||
|
emit<K extends keyof EventHandlers<E>>(
|
||||||
on<K extends keyof EventHandlers<E>>(eventName: K, handler: EventHandlers<E>[K]): void {
|
eventName: K,
|
||||||
const observer = (this._observers[eventName] ??= []);
|
event: InstanceType<E[K]>,
|
||||||
observer.push(handler);
|
): 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";
|
export type LogSource = "Connection" | "GameState";
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
export function log(source: LogSource, ...data: any[]): void {
|
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 fontFamily = "impact";
|
||||||
const shadow = {
|
const shadow = {
|
||||||
color: ex.Color.Black,
|
color: ex.Color.Black,
|
||||||
offset: ex.vec(4, 4),
|
offset: ex.vec(4, 4),
|
||||||
}
|
};
|
||||||
|
|
||||||
export const paragraphFont = new ex.Font({
|
export const paragraphFont = new ex.Font({
|
||||||
family: fontFamily,
|
family: fontFamily,
|
||||||
@@ -39,5 +39,4 @@ export const disabledFont = new ex.Font({
|
|||||||
color: ex.Color.Gray,
|
color: ex.Color.Gray,
|
||||||
size: 20,
|
size: 20,
|
||||||
shadow,
|
shadow,
|
||||||
|
});
|
||||||
})
|
|
||||||
@@ -5,17 +5,32 @@ const lowerBarHeight = spacingS;
|
|||||||
export const lowerBarHeightDown = spacingS / 2;
|
export const lowerBarHeightDown = spacingS / 2;
|
||||||
|
|
||||||
/** Return a GraphicsGroup of a clicky button. */
|
/** Return a GraphicsGroup of a clicky button. */
|
||||||
export function makeStyledRectangle(width: number, height: number, color: Color, down: boolean): GraphicsGroup {
|
export function makeStyledRectangle(
|
||||||
const offsetYTop = !down ? 0 : lowerBarHeightDown;
|
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.
|
// The lower bar is the side of the rectangle on the bottom part that makes it look 3d.
|
||||||
const heightLowerBar = !down ? lowerBarHeight : lowerBarHeightDown;
|
const heightLowerBar = !down ? lowerBarHeight : lowerBarHeightDown;
|
||||||
const offsetYLowerBar = !down ? height : height + lowerBarHeightDown;
|
const offsetYLowerBar = !down ? height : height + lowerBarHeightDown;
|
||||||
|
|
||||||
return new GraphicsGroup({
|
return new GraphicsGroup({
|
||||||
members: [
|
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) },
|
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