JA
games/dmmotor / src / Scene / IntermissionScene.php · commit 9d248a4
<?php/* * dmmotor — the stats screen between two levels: kills, items, secrets, time and par counting up, then the * name of the next level (after wi_stuff.c of linuxdoom-1.10). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Scene;use App\Dm\MenuScreen;use App\Dm\Render\Framebuffer;use App\Dm\Strings;use App\Service\DmGame;use JumPHP\Renderer\Renderer;use JumPHP\Scene\AbstractScene;final class IntermissionScene extends AbstractScene{    private const STATSX = 50;    private const STATSY = 50;    private const TIMEX = 16;    private const TIMEY = 168;    /** @var array<string,mixed> */    private array $p = [];    /** 0 counting, 1 stats shown, 2 "entering" */    private int $stage = 0;    private float $time = 0;    private float $count = 0;    private int $lastCount = -1;    public function __construct(private DmGame $game)    {    }    public function enter(array $params = []): void    {        $this->game->load(fn (string $p) => $this->engine->config->path($p), $this->sound());        $this->game->changeMusic($this->engine->pcm(), 'intermission');        $this->p = $params + ['episode' => 1, 'map' => 1, 'next' => 2, 'finale' => false, 'text' => null, 'kills' => 0,            'totalKills' => 0, 'items' => 0, 'totalItems' => 0, 'secrets' => 0, 'totalSecrets' => 0, 'time' => 0];        $this->stage = 0;        $this->time = 0;        $this->count = 0;        $this->lastCount = -1;    }    private static function percent(int $n, int $total): int    {        return $total > 0 ? intdiv($n * 100, $total) : 100;    }    public function update(float $dt): void    {        $this->time += $dt;        $this->game->pumpMusic($this->engine->pcm());        $pressed = $this->actions()->isPressed('confirm') || $this->actions()->isPressed('fire')            || $this->actions()->isPressed('use') || $this->actions()->isPressed('back');        if ($this->stage === 0) {            $this->count += $dt * 35 * 2;   // two percent a tic            if (intdiv((int) $this->count, 6) !== $this->lastCount) {                $this->lastCount = intdiv((int) $this->count, 6);                $this->game->sounds?->menu('pistol');            }            if ($this->count >= 300 || $pressed) {                $this->count = 300;                $this->stage = 1;                $this->game->sounds?->menu('barexp');            }            return;        }        if (!$pressed) {            return;        }        $this->game->sounds?->menu('sgcock');        if ($this->p['text'] !== null || $this->p['finale']) {            // the story between the levels, or the end            $this->scenes()->switchTo(FinaleScene::class, [                'episode' => $this->p['episode'], 'text' => $this->p['text'], 'next' => $this->p['next'], 'end' => $this->p['finale'],            ]);            return;        }        if ($this->stage === 1) {            $this->stage = 2;            return;        }        $this->scenes()->switchTo(PlayScene::class, ['episode' => $this->p['episode'], 'map' => $this->p['next']]);    }    private function patch(Framebuffer $fb, int $x, int $y, string $name, bool $center = false): int    {        $g = $this->game->gfx;        if (!$g->wad->has($name)) {            return 0;        }        $patch = $g->patch($name);        $fb->drawPatch($center ? intdiv(320 - $patch->width, 2) : $x, $y, $patch);        return $patch->height;    }    /** WI_drawNum: right-aligned at x. */    private function num(Framebuffer $fb, int $x, int $y, int $n, int $digits = 0): int    {        $g = $this->game->gfx;        $w = $g->patch('WINUM0')->width;        $n = max(0, $n);        $digits = $digits ?: max(1, strlen((string) $n));        for ($i = 0; $i < $digits; $i++) {            $x -= $w;            $fb->drawPatch($x, $y, $g->patch('WINUM' . ($n % 10)));            $n = intdiv($n, 10);        }        return $x;    }    private function percentAt(Framebuffer $fb, int $x, int $y, int $p): void    {        $this->patch($fb, $x, $y, 'WIPCNT');        $this->num($fb, $x, $y, $p);    }    private function timeAt(Framebuffer $fb, int $x, int $y, int $t): void    {        $g = $this->game->gfx;        $x = $this->num($fb, $x, $y, $t % 60, 2);        $colon = $g->patch('WICOLON');        $x -= $colon->width;        $fb->drawPatch($x, $y, $colon);        $this->num($fb, $x, $y, intdiv($t, 60));    }    private function levelName(int $episode, int $map): string    {        return $this->game->commercial ? sprintf('CWILV%02d', $map - 1) : sprintf('WILV%d%d', $episode - 1, $map - 1);    }    public function render(Renderer $r): void    {        $g = $this->game;        $fb = $g->fb;        $fb->clear(0);        $ep = (int) $this->p['episode'];        $background = !$g->commercial && $ep <= 3 && $g->wad->has('WIMAP' . ($ep - 1)) ? 'WIMAP' . ($ep - 1) : 'INTERPIC';        MenuScreen::fullPicture($g->gfx, $fb, $background);        if ($this->stage === 2) {            $h = $this->patch($fb, 0, 2, 'WIENTER', true);            $this->patch($fb, 0, 2 + (int) ($h * 5 / 4), $this->levelName($ep, (int) $this->p['next']), true);            $g->present($r, $this->sdl());            return;        }        $h = $this->patch($fb, 0, 2, $this->levelName($ep, (int) $this->p['map']), true);        $this->patch($fb, 0, 2 + (int) ($h * 5 / 4), 'WIF', true);        $lh = (int) (3 * $g->gfx->patch('WINUM0')->height / 2);        $c = (int) $this->count;        $this->patch($fb, self::STATSX, self::STATSY, 'WIOSTK');        $this->percentAt($fb, 320 - self::STATSX, self::STATSY, min($c, self::percent($this->p['kills'], $this->p['totalKills'])));        $this->patch($fb, self::STATSX, self::STATSY + $lh, 'WIOSTI');        $this->percentAt($fb, 320 - self::STATSX, self::STATSY + $lh, min($c, self::percent($this->p['items'], $this->p['totalItems'])));        $this->patch($fb, self::STATSX, self::STATSY + 2 * $lh, 'WISCRT2');        $this->percentAt($fb, 320 - self::STATSX, self::STATSY + 2 * $lh, min($c, self::percent($this->p['secrets'], $this->p['totalSecrets'])));        $this->patch($fb, self::TIMEX, self::TIMEY, 'WITIME');        $this->timeAt($fb, 160 - self::TIMEX, self::TIMEY, min($c * 2, (int) $this->p['time']));        $par = Strings::par($g->world->mapName($ep, (int) $this->p['map']));        if ($par > 0) {            $this->patch($fb, 160 + self::TIMEX, self::TIMEY, 'WIPAR');            $this->timeAt($fb, 320 - self::TIMEX, self::TIMEY, min($c * 2, $par));        }        $g->present($r, $this->sdl());    }}