Eğitim 5 — Menüler, ayarlar, duraklatma ve diller
Bir menü
MenuScene sınıfından türeyen her sahne bir menüdür: bir $title, items() satırları.
protected function items(): array
{
return [
// [etiket, sağdaki değer, Enter'da, sol / sağda]
['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(); // bir alt menüde Esc
}
switchTo() ekranı değiştirir (bir geçişle), push() bir tanesini üstüne açar, pop() ona geri döner.
Duraklatma
Duraklatma, oyunun üstünde bir menüdür: isOverlay() true döndürür ve oyun altta çizili kalır.
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)],
];
}
}
// oyunun içinde:
if ($this->actions()->isPressed('pause')) {
$this->scenes()->push(PauseScene::class);
return;
}
game.yaml içinde pause_scene: App\Scene\PauseScene ile, sekme gizlendiğinde ya da pencere odağı kaybettiğinde duraklatma kendiliğinden de açılır.
Diller
config/lang.yaml metinleri çevirir (menü etiketleri otomatik olarak çevrilir):
fr:
PLAY: JOUER
'{n} COINS': '{n} PIECES'
Kodda: Lang::t('{n} COINS', ['n' => $this->coins]); dili değiştirmek için: $this->engine->lang->setLang('fr').