the modes offered */ public array $modes = []; /** @var array Game::sleepInfo() */ public array $info = []; public string $summary = ''; public string $refused = ''; private float $t = 0.0; private float $fade = 0.0; public function open(Game $g, bool $rest = false): void { $this->phase = 'choose'; $this->info = $g->sleepInfo(); $this->modes = $g->isNight() ? ['dawn', '1h', '4h', 'healed', 'rest'] : ['1h', '4h', 'healed', 'dawn', 'rest']; $this->sel = 0; $this->summary = ''; $this->refused = ''; $this->t = 0.0; $this->fade = 0.0; if ($rest) { $this->start($g, 'rest'); } } /** Lies down (or sits): false when Rob cannot (the reason in $refused). */ public function start(Game $g, string $mode): bool { $why = $g->startSleep($mode); if ($why !== null) { $this->refused = $why; return false; } $this->phase = 'sleep'; $this->t = 0.0; return true; } /** Returns false when the screen closes. */ public function update(UiInput $in, Game $g, float $dt): bool { $this->t += $dt; switch ($this->phase) { case 'choose': if ($in->back) { return false; } $n = count($this->modes); if ($in->up) { $this->sel = ($this->sel - 1 + $n) % $n; } if ($in->down) { $this->sel = ($this->sel + 1) % $n; } if ($in->moved && $in->hover !== null && str_starts_with($in->hover, 'sleep:')) { $k = array_search(substr($in->hover, 6), $this->modes, true); $this->sel = $k === false ? $this->sel : (int) $k; } $pick = null; if ($in->ok) { $pick = $this->modes[$this->sel]; } elseif ($in->digit >= 1 && $in->digit <= $n) { $pick = $this->modes[$in->digit - 1]; } elseif ($in->click && $in->hit !== null) { if (str_starts_with($in->hit, 'sleep:')) { $pick = substr($in->hit, 6); } elseif ($in->hit === 'sleepclose') { return false; } } if ($pick !== null) { $this->start($g, $pick); } return true; case 'sleep': $s = $g->sleeping; if ($s === null) { $this->phase = 'woke'; return true; } $this->fade = min(1.0, $this->fade + $dt * 1.4); if ($in->ok || $in->back || $in->click) { $this->summary = $g->finishSleep(); $this->phase = 'woke'; $this->t = 0.0; return true; } if ($this->fade >= 0.85 || !empty($s['rest'])) { $g->sleepStep($dt * (!empty($s['rest']) ? self::REST_SPEED : self::SPEED)); } if (!empty($g->sleeping['done'])) { $this->summary = $g->wake(); $this->phase = 'woke'; $this->t = 0.0; } return true; default: $this->fade = max(0.0, $this->fade - $dt * 1.2); return !(($in->ok || $in->back || $in->click) && $this->t > 0.4); } } /** Is a beast or the cold what woke Rob (the scene plays a sting)? */ public function rude(): bool { return $this->phase === 'woke' && (str_contains($this->summary, 'réveille') || str_contains($this->summary, 'sursaut') || str_contains($this->summary, 'yeux')); } public function draw(Canvas $c, Game $g): void { if ($this->phase === 'choose') { $this->drawChoose($c, $g); return; } $c->rect(0, 0, Canvas::W, Canvas::H, [2, 3, 6, (int) (245 * $this->fade)]); if ($this->phase === 'sleep') { $s = $g->sleeping; $rest = !empty($s['rest']); $h = $g->hour(); $c->text(sprintf('%02d:%02d', (int) $h, (int) (($h - (int) $h) * 60)), 480, 250, 64, [230, 236, 246], 'center'); $c->text($rest ? 'Tu te reposes…' : 'Tu dors…', 480, 330, 20, [170, 186, 204], 'center'); if ($s !== null && !$rest) { $dreams = (array) $s['dreams']; $share = (float) $s['slept'] / max(1.0, (float) $s['target']); $i = min(count($dreams) - 1, (int) floor($share * count($dreams))); if ($i >= 0 && isset($dreams[$i])) { $ph = fmod($share * count($dreams), 1.0); // the text has no alpha: it fades by its brightness (8 steps, few textures) $f = round(8 * min(1.0, min($ph, 1 - $ph) * 5)) / 8; foreach ($c->wrap('« ' . $dreams[$i] . ' »', 17, 620) as $k => $line) { $c->text($line, 480, 390 + $k * 24, 17, [(int) (190 * $f), (int) (200 * $f), (int) (220 * $f)], 'center', false); } } $c->rect(330, 470, 300, 3, [255, 255, 255, 30]); $c->rect(330, 470, 300 * min(1.0, $share), 3, [150, 190, 255, 200]); } $c->text('Entrée / clic : passer', 480, 520, 12, [120, 130, 150], 'center'); return; } $lines = $c->wrap($this->summary, 17, 600); $h = 110 + 24 * count($lines); $y = 360 - $h / 2; $c->panel(170, $y, 620, $h, 230, $this->rude() ? [255, 120, 90, 230] : [150, 190, 255, 220]); $c->text($this->rude() ? 'Réveil brutal' : 'Réveil', 480, $y + 16, 24, [255, 255, 255], 'center'); foreach ($lines as $k => $line) { $c->text($line, 480, $y + 56 + $k * 24, 17, [226, 232, 240], 'center'); } $c->text('Entrée / clic : continuer', 480, $y + $h - 26, 12, [140, 150, 170], 'center'); } private function drawChoose(Canvas $c, Game $g): void { $c->rect(0, 0, Canvas::W, Canvas::H, [4, 6, 12, 150]); $risks = (array) ($this->info['risks'] ?? []); $h = 196 + 26 * max(1, count($risks)) + 40 * count($this->modes); $y = max(40.0, 360 - $h / 2); $c->panel(230, $y, 500, $h, 232, [150, 190, 255, 220]); $c->hit(230, $y, 500, $h, 'sleeppanel'); $c->text('Dormir', 250, $y + 14, 26, [255, 255, 255]); $c->text($g->clock(), 710, $y + 22, 14, [170, 186, 200], 'right'); $c->line(696, $y + 50, 708, $y + 62, [190, 200, 210], 2); $c->line(708, $y + 50, 696, $y + 62, [190, 200, 210], 2); $c->text('OÙ', 250, $y + 56, 12, [150, 166, 180]); $c->text((string) ($this->info['label'] ?? ''), 250, $y + 72, 17, [240, 244, 248]); $q = (float) ($this->info['comfort'] ?? 1.0); $c->text('Qualité du sommeil', 520, $y + 56, 12, [150, 166, 180]); $c->rect(520, $y + 78, 190, 8, [255, 255, 255, 26]); $c->rect(520, $y + 78, 190 * min(1.0, $q / 1.15), 8, $q < 0.85 ? [230, 160, 70] : [110, 210, 150]); $c->text('RISQUES', 250, $y + 104, 12, [150, 166, 180]); $ry = $y + 122; if ($risks === []) { $c->text('Aucun : tu peux dormir tranquille.', 250, $ry, 15, [140, 220, 160]); $ry += 26; } foreach ($risks as [$label, $level]) { $col = $level > 0.6 ? [255, 110, 100] : ($level > 0.3 ? [255, 180, 100] : [230, 214, 140]); $c->circle(258, $ry + 9, 5, $col); $c->text((string) $label, 272, $ry, 15, $col); $ry += 26; } if (($this->info['can'] ?? null) !== null) { $c->text((string) $this->info['can'], 480, $ry + 10, 15, [255, 130, 110], 'center'); } $my = $ry + 14 + (($this->info['can'] ?? null) !== null ? 24 : 0); foreach ($this->modes as $i => $mode) { $on = $i === $this->sel; $c->rect(246, $my, 468, 34, $on ? [120, 190, 255, 60] : [255, 255, 255, 12]); if ($on) { $c->rect(246, $my, 3, 34, [150, 210, 255, 230]); } $c->text(($i + 1) . '.', 258, $my + 8, 14, [150, 210, 255]); $c->text(Game::SLEEP_MODES[$mode], 282, $my + 7, 16, [255, 255, 255]); $c->text(self::note($g, $mode), 704, $my + 9, 12, [150, 170, 186], 'right'); $c->hit(246, $my, 468, 34, 'sleep:' . $mode); $my += 40; } if ($this->refused !== '') { $c->text($this->refused, 480, $my + 4, 14, [255, 150, 120], 'center'); } $c->text('Échap : annuler', 480, $y + $h - 22, 12, [130, 146, 160], 'center'); $c->hit(688, $y + 44, 28, 26, 'sleepclose'); } private static function note(Game $g, string $mode): string { $h = $g->hour(); return match ($mode) { 'dawn' => sprintf('réveil vers 06:30 (%d h)', (int) round(($h >= 6.5 ? 24 - $h + 6.5 : 6.5 - $h))), 'healed' => 'jusqu\'à être bien reposé', 'rest' => 'souffle, un peu de repos', default => '', }; } }