FR
games/dmmotor / src / Scene / FinaleScene.php · commit 9d248a4
<?php/* * dmmotor — the story between the levels and the end of an episode: the text typed over a flat, then the next * level or the end picture (after f_finale.c of linuxdoom-1.10). The texts and the flats come from Freedoom's * DEHACKED lump. 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\StatusBar;use App\Dm\Strings;use App\Service\DmGame;use JumPHP\Renderer\Renderer;use JumPHP\Scene\AbstractScene;final class FinaleScene extends AbstractScene{    private const TEXTSPEED = 3;    /** the flat behind each text (f_finale.c), names given by DEHACKED */    private const FLATS = [        'E1TEXT' => 'BGFLATE1', 'E2TEXT' => 'BGFLATE2', 'E3TEXT' => 'BGFLATE3', 'E4TEXT' => 'BGFLATE4',        'C1TEXT' => 'BGFLAT06', 'C2TEXT' => 'BGFLAT11', 'C3TEXT' => 'BGFLAT20', 'C4TEXT' => 'BGFLAT30',        'C5TEXT' => 'BGFLAT15', 'C6TEXT' => 'BGFLAT31',    ];    private int $episode = 1;    private float $time = 0;    private string $text = '';    private string $flat = 'FLOOR4_8';    private bool $picture = false;    private bool $end = true;    private int $next = 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(), 'finale');        $this->episode = (int) ($params['episode'] ?? 1);        $key = $params['text'] ?? "E{$this->episode}TEXT";        $this->text = Strings::get($key, 'THE END');        $this->flat = Strings::get(self::FLATS[$key] ?? '', 'FLOOR4_8');        if (!isset($this->game->gfx->flatNum[strtoupper($this->flat)])) {            $this->flat = 'FLOOR4_8';        }        $this->end = (bool) ($params['end'] ?? true);        $this->next = (int) ($params['next'] ?? 1);        $this->time = 0;        $this->picture = false;    }    public function update(float $dt): void    {        $this->time += $dt;        $this->game->pumpMusic($this->engine->pcm());        $a = $this->actions();        if (!($a->isPressed('confirm') || $a->isPressed('fire') || $a->isPressed('use') || $a->isPressed('back'))) {            return;        }        $shown = (int) ($this->time * 35 / self::TEXTSPEED);        if (!$this->picture && $shown < strlen($this->text)) {            $this->time = 1000;   // show the whole text            return;        }        if (!$this->end) {            $this->scenes()->switchTo(PlayScene::class, ['episode' => $this->episode, 'map' => $this->next]);            return;        }        if (!$this->picture) {            $this->picture = true;            return;        }        $this->scenes()->switchTo(TitleScene::class);    }    public function render(Renderer $r): void    {        $g = $this->game;        $fb = $g->fb;        if ($this->picture) {            $fb->clear(0);            $pic = $g->commercial ? 'BOSSBACK' : match ($this->episode) {                1 => $g->wad->has('CREDIT') ? 'CREDIT' : 'HELP2',                2 => 'VICTORY2',                default => 'ENDPIC',            };            MenuScreen::fullPicture($g->gfx, $fb, $g->wad->has($pic) ? $pic : 'TITLEPIC');        } else {            MenuScreen::tileFlat($g->gfx, $fb, $this->flat);            $n = (int) ($this->time * 35 / self::TEXTSPEED);            StatusBar::drawText($g->gfx, $fb, 10, 10, substr($this->text, 0, max(0, $n)));        }        $g->present($r, $this->sdl());    }}