IT

Tutorial 5 — Menu, opzioni, pausa e lingue

Un menu

Ogni scena che eredita da MenuScene è un menu: un $title, delle righe items().

protected function items(): array
{
    return [
        // [etichetta, valore a destra, su Invio, su sinistra / destra]
        ['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 in un sottomenu
}

switchTo() sostituisce la schermata (con una dissolvenza), push() ne apre una sopra, pop() ci torna.

La pausa

Una pausa è un menu sopra il gioco: isOverlay() restituisce true, e il gioco resta disegnato sotto.

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)],
        ];
    }
}

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

Con pause_scene: App\Scene\PauseScene in game.yaml, la pausa si apre anche da sola quando la scheda viene nascosta o la finestra perde il focus.

Le lingue

config/lang.yaml traduce i testi (le etichette dei menu lo sono automaticamente):

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

Nel codice: Lang::t('{n} COINS', ['n' => $this->coins]); per cambiare lingua: $this->engine->lang->setLang('fr').