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