PL

Samouczek 5 — Menu, opcje, pauza i języki

Każda scena dziedzicząca po MenuScene jest menu: $title i wiersze items().

protected function items(): array
{
    return [
        // [etykieta, wartość po prawej, na Enter, na lewo / prawo]
        ['PLAY', null, fn () => $this->scenes()->switchTo(PlayScene::class, ['level' => 1])],
        ['VOLUME', fn () => round($this->sound()->getSfxVolume() * 100) . '%', null,
            fn (int $dir) => $this->sound()->setSfxVolume($this->sound()->getSfxVolume() + $dir / 10)],
        ['OPTIONS', null, fn () => $this->scenes()->push(OptionsScene::class)],
    ];
}

protected function back(): void
{
    $this->scenes()->pop();    // Esc w podmenu
}

switchTo() zastępuje ekran (z przejściem), push() otwiera kolejny na wierzchu, pop() do niego wraca.

Pauza

Pauza to menu nad grą: isOverlay() zwraca true, a gra pozostaje narysowana pod spodem.

final class PauseScene extends MenuScene
{
    protected string $title = 'PAUSE';

    public function isOverlay(): bool
    {
        return true;
    }

    protected function items(): array
    {
        return [
            ['RESUME', null, fn () => $this->scenes()->pop()],
            ['TITLE SCREEN', null, fn () => $this->scenes()->switchTo(TitleScene::class)],
        ];
    }
}

// w grze:
if ($this->actions()->isPressed('pause')) {
    $this->scenes()->push(PauseScene::class);
    return;
}

Z pause_scene: App\Scene\PauseScene w game.yaml pauza otwiera się też sama, gdy karta jest ukryta lub okno traci fokus.

Języki

config/lang.yaml tłumaczy teksty (etykiety menu są tłumaczone automatycznie):

fr:
  PLAY: JOUER
  '{n} COINS': '{n} PIECES'

W kodzie: Lang::t('{n} COINS', ['n' => $this->coins]); zmiana języka: $this->engine->lang->setLang('fr').