IT
games/dmmotor / src / Scene / PauseScene.php · commit 9d248a4
<?phpnamespace App\Scene;use App\Service\DmGame;use App\Service\SaveGames;use JumPHP\Ui\MenuScene;/** * The pause menu over the game (also pushed when the window loses the focus): save / load, options, restart. */final class PauseScene extends MenuScene{    protected string $title = 'PAUSE';    protected float $titleScale = 2.0;    protected float $menuTop = 0.32;    private int $slot = 0;    public function __construct(private DmGame $game)    {    }    public function isOverlay(): bool    {        return true;    }    private function slotLabel(): string    {        $info = SaveGames::info($this->engine->storage, $this->game->iwad, $this->slot);        return ($this->slot + 1) . ': ' . strtoupper((string) ($info['name'] ?? '-'));    }    private function toggleRun(): void    {        $this->game->alwaysRun = !$this->game->alwaysRun;        $this->engine->options->set('always_run', $this->game->alwaysRun);        $this->refresh();    }    protected function items(): array    {        $pickSlot = function (int $dir) {            $this->slot = ($this->slot + $dir + SaveGames::SLOTS) % SaveGames::SLOTS;            $this->refresh();        };        return [            ['RESUME', null, fn () => $this->scenes()->pop()],            ['SAVE GAME', fn () => $this->slotLabel(), fn () => $this->scenes()->pop(['save' => $this->slot]), $pickSlot],            ['LOAD GAME', fn () => $this->slotLabel(), function () {                $data = SaveGames::read($this->engine->storage, $this->game->iwad, $this->slot);                if ($data !== null) {                    $this->scenes()->pop(['load' => $data]);                }            }, $pickSlot],            ['ALWAYS RUN', fn () => $this->game->alwaysRun ? 'ON' : 'OFF', fn () => $this->toggleRun(), fn (int $dir) => $this->toggleRun()],            ['MOUSE TURN', fn () => (string) round($this->game->mouseSpeed * 1000), null, function (int $dir) {                $this->game->mouseSpeed = max(0, min(0.05, $this->game->mouseSpeed + $dir * 0.002));                $this->engine->options->set('mouse_speed', $this->game->mouseSpeed);                $this->refresh();            }],            ['SOUND', fn () => round($this->sound()->getSfxVolume() * 100) . '%', null,                fn (int $dir) => $this->sound()->setSfxVolume($this->sound()->getSfxVolume() + $dir / 10)],            ['MUSIC', fn () => round($this->sound()->getMusicVolume() * 100) . '%', null,                fn (int $dir) => $this->sound()->setMusicVolume($this->sound()->getMusicVolume() + $dir / 10)],            ['RESTART LEVEL', null, fn () => $this->scenes()->pop(['restart' => true])],            ['QUIT TO TITLE', null, fn () => $this->scenes()->switchTo(TitleScene::class, ['menu' => true])],        ];    }    protected function back(): void    {        $this->scenes()->pop();    }    public function update(float $dt): void    {        if (!$this->game->inLevel) {            $this->scenes()->pop();   // the window lost the focus over a menu: nothing to pause            return;        }        if ($this->actions()->isPressed('pause') && !$this->actions()->isPressed('back')) {            $this->scenes()->pop();            return;        }        parent::update($dt);    }}