import { _decorator, Button, Component, Label, Node } from 'cc'; import { App } from '../../app/App'; const { ccclass, property } = _decorator; /** * The lobby, per LOBBY.md: one button that starts a game, and — since the save * module is already keeping them — the player's record. * * It owns no game logic. Starting a match is reported to `MainFlow` through * `onStartRequested`, which keeps the lobby ignorant of what a match even is. */ @ccclass('LobbyScreen') export class LobbyScreen extends Component { @property(Label) playedLabel: Label | null = null; @property(Label) wonLabel: Label | null = null; @property(Label) lostLabel: Label | null = null; @property(Label) streakLabel: Label | null = null; @property(Node) statsPanel: Node | null = null; @property(Button) newGameButton: Button | null = null; /** Set by `MainFlow`. */ onStartRequested: (() => void) | null = null; onLoad(): void { // Clicks are subscribed here rather than through the scene's clickEvents // list: the wiring stays visible in the source, and a renamed handler // becomes a compile error instead of a button that silently does nothing. this.newGameButton?.node.on(Button.EventType.CLICK, this.startGame, this); } onDestroy(): void { this.newGameButton?.node.off(Button.EventType.CLICK, this.startGame, this); } onEnable(): void { this.refresh(); } /** Re-reads the saved statistics. Called whenever the lobby comes back into view. */ refresh(): void { if (!App.exists) { return; } const stats = App.instance.stats; setText(this.playedLabel, `${stats.played}`); setText(this.wonLabel, `${stats.won}`); setText(this.lostLabel, `${stats.lost}`); setText(this.streakLabel, stats.bestStreak > 0 ? `${stats.streak} (best ${stats.bestStreak})` : `${stats.streak}`); // Nothing to show before the first game — an empty table of zeroes is // worse than no table at all. if (this.statsPanel) { this.statsPanel.active = stats.played > 0; } } /** Wired to the New Game button in the scene. */ startGame(): void { this.onStartRequested?.(); } } function setText(label: Label | null, text: string): void { if (label) { label.string = text; } }