IT
apps/winzik / src / Scene / LockScene.php · commit 6576d6f
<?phpnamespace App\Scene;use JumPHP\Renderer\Renderer;/** * Form14 of 2001, the Lock mode: the screen goes black, the title of the song crosses it at random heights, the lyrics * go by at the bottom, the pointer is hidden. Moving the mouse or typing shows "Mot de passe:" for 2 seconds; the right * password (or any key when it is empty) gives the screen back. The music goes on. */final class LockScene extends FullScreen{    private string $typed = '';    /** seconds the password field stays (Timer2: 2000 ms) */    private float $promptTime = 0.0;    private float $titleX = 0.0;    private float $titleY = 200.0;    private float $step = 0.0;    private float $lastMouseX = -1;    private float $lastMouseY = -1;    private float $wrong = 0.0;    public function enter(array $params = []): void    {        parent::enter($params);        $this->typed = '';        $this->promptTime = 0.0;        $this->titleX = -200;        $this->titleY = mt_rand(20, 400);        $this->wrong = 0;        $mouse = $this->engine->input->getMouse();        [$this->lastMouseX, $this->lastMouseY] = [$mouse->x, $mouse->y];        $this->showCursor(false);    }    public function exit(): void    {        $this->showCursor(true);    }    public function update(float $dt): void    {        parent::update($dt);        // Timer4: the title moves 200 twips every 250 ms, then comes back at the left, at another height        $this->step += $dt;        while ($this->step >= 0.25) {            $this->step -= 0.25;            $this->titleX += 13;            if ($this->titleX > FullScreen::W) {                $this->titleX = -320;                $this->titleY = mt_rand(20, 400);            }        }        $this->wrong = max(0, $this->wrong - $dt);        $this->promptTime = max(0, $this->promptTime - $dt);        $input = $this->engine->input;        $mouse = $input->getMouse();        $moved = abs($mouse->x - $this->lastMouseX) + abs($mouse->y - $this->lastMouseY) > 2;        [$this->lastMouseX, $this->lastMouseY] = [$mouse->x, $mouse->y];        $keyboard = $input->getKeyboard();        $keys = $keyboard->getTyped();        if ($this->time < 0.4) {            return;    // the click that chose the Lock mode        }        if ($this->wz->lockPassword === '' && ($moved || $this->anyInput())) {            $this->unlock();            return;        }        if ($moved || $keys !== [] || $mouse->isPressed(1)) {            $this->promptTime = 2.0;        }        $text = $keyboard->hasTextEvents() ? $keyboard->getText() : '';        foreach ($keys as $key) {            if ($key === 13 || $key === 1073741912) {                if ($this->typed === $this->wz->lockPassword) {                    $this->unlock();                    return;                }                $this->typed = '';                $this->wrong = 1.0;                $this->promptTime = 0;    // Text1.Visible = False: the field goes away            } elseif ($key === 8) {                $this->typed = mb_substr($this->typed, 0, -1);            } elseif ($key === 27) {                $this->typed = '';            } elseif (!$keyboard->hasTextEvents() && $key >= 32 && $key < 127) {                $text .= chr($key);            }        }        $this->typed .= $text;    }    private function unlock(): void    {        $this->showCursor(true);        $this->scenes()->pop();    }    public function render(Renderer $r): void    {        $c = $this->canvas($r);        $c->text($this->wz->plainTitle(), $this->titleX, $this->titleY, 16, '#ffc080');        $line = $this->wz->lyrics !== null ? $this->wz->lyricLine() : '';        if ($line !== '') {            $c->text($line, 4, FullScreen::H - 30, 18, '#ffc080');        }        if ($this->promptTime > 0) {            $x = FullScreen::W / 2 - 110;            $y = FullScreen::H / 2 - 20;            $c->text('Mot de passe:', $x, $y - 22, 16, '#ff0000');            $c->fill($x, $y, 220, 26, '#000000');            $c->frame($x, $y, 220, 26, '#808000');            $c->text(str_repeat('*', mb_strlen($this->typed)) . (fmod($this->time, 1) < 0.5 ? '_' : ''), $x + 6, $y + 4, 16, '#ffff00');        }        if ($this->wrong > 0) {            $c->text('Mauvais mot de passe', FullScreen::W / 2, FullScreen::H / 2 + 20, 12, '#ff0000', 'center');        }    }}