JA
games/rob-revisited / src / Revisited / Ui / TalentsView.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;use App\Revisited\Land\Game;use App\Revisited\Sim\Talents;/** * The "Talents" tab (desktop; phones: PhoneScreens::pTalents): the level and its experience bar, the points to spend, * the three branches side by side (Combat, Survie, Artisanat), a card per talent (name, ranks, what a rank does, a * "+" to learn it). Keys / pad: arrows choose, Enter (A) learns; mouse: a click on a card chooses it, on its "+" learns. */final class TalentsView{    public int $col = 0;    public int $row = 0;    /** @return list<list<string>> the talents per column (Talents::BRANCHES order) */    public static function columns(): array    {        $out = [];        foreach (array_keys(Talents::BRANCHES) as $b) {            $out[] = Talents::of($b);        }        return $out;    }    public function selected(): string    {        $cols = self::columns();        $this->col = max(0, min(count($cols) - 1, $this->col));        $this->row = max(0, min(count($cols[$this->col]) - 1, $this->row));        return $cols[$this->col][$this->row];    }    /** @param \Closure(string): void $say the screens' note */    public function update(UiInput $in, Game $g, \Closure $say): bool    {        $cols = self::columns();        if ($in->up) {            $this->row--;        }        if ($in->down) {            $this->row++;        }        if ($in->left) {            $this->col = ($this->col + count($cols) - 1) % count($cols);        }        if ($in->right) {            $this->col = ($this->col + 1) % count($cols);        }        $id = $this->selected();        $learn = $in->ok ? $id : null;        if ($in->click && $in->hit !== null) {            if (str_starts_with($in->hit, 'tal:')) {                $this->pick(substr($in->hit, 4));            } elseif (str_starts_with($in->hit, 'tallearn:')) {                $learn = substr($in->hit, 9);                $this->pick($learn);            }        }        if ($learn !== null) {            $say($g->talents->learn($learn));        }        return true;    }    private function pick(string $id): void    {        foreach (self::columns() as $c => $list) {            $r = array_search($id, $list, true);            if ($r !== false) {                $this->col = $c;                $this->row = (int) $r;            }        }    }    /** "Niveau 4", "120 / 320 XP", the bar's fill 0..1. @return array{0: string, 1: string, 2: float} */    public static function levelLine(Game $g): array    {        $t = $g->talents;        [$in, $need] = $t->progress();        return ['Niveau ' . $t->level(), $need > 0 ? (int) floor($in) . ' / ' . $need . ' XP' : 'Niveau maximum', $need > 0 ? $in / $need : 1.0];    }    public function draw(Canvas $c, Game $g, float $time): void    {        $t = $g->talents;        [$level, $xp, $fill] = self::levelLine($g);        $c->text($level, 56, 114, 22, [255, 214, 140]);        $c->rect(200, 124, 380, 10, [255, 255, 255, 30]);        $c->rect(200, 124, 380 * $fill, 10, [255, 196, 90]);        $c->text($xp, 590, 120, 13, [220, 228, 236]);        $pts = $t->points();        $c->text($pts > 0 ? 'Points à dépenser : ' . $pts : 'Aucun point à dépenser', 904, 118, 15, $pts > 0 ? [140, 235, 160] : [160, 172, 184], 'right');        $c->textBox('L\'expérience vient des bêtes vaincues (plus elles sont dangereuses, plus elle compte), des recettes nouvelles, des régions découvertes et des chapitres de l\'histoire.',            52, 146, 856, 12, [160, 174, 188], 'left', 2);        $sel = $this->selected();        $x = 56.0;        foreach (array_keys(Talents::BRANCHES) as $b) {            $this->column($c, $g, $b, $x, 186.0, 276.0, $sel, $pts);            $x += 286.0;        }    }    private function column(Canvas $c, Game $g, string $branch, float $x, float $y, float $w, string $sel, int $pts): void    {        $spent = 0;        foreach (Talents::of($branch) as $id) {            $spent += $g->talents->rank($id);        }        $c->text(mb_strtoupper(Talents::BRANCHES[$branch]), $x, $y, 15, [255, 204, 110]);        $c->text($spent . ' pt' . ($spent > 1 ? 's' : ''), $x + $w, $y + 2, 12, [150, 166, 180], 'right');        $y += 24;        foreach (Talents::of($branch) as $id) {            [, $name, $max, $desc] = Talents::TREE[$id];            $rank = $g->talents->rank($id);            $on = $id === $sel;            $c->rect($x, $y, $w, 102, $on ? [120, 190, 255, 46] : [255, 255, 255, 14]);            $c->border($x, $y, $w, 102, $on ? [150, 210, 255, 210] : [255, 255, 255, 40]);            $c->hit($x, $y, $w, 102, 'tal:' . $id);            $c->textBox($name, $x + 6, $y + 6, $w - 80, 15, $rank > 0 ? [255, 255, 255] : [214, 222, 230]);            for ($i = 0; $i < $max; $i++) {                $px = $x + $w - 14 - ($max - 1 - $i) * 16;                $c->circle($px, $y + 16, 5.5, $i < $rank ? [255, 200, 90] : [255, 255, 255, 40]);            }            $c->textBox($desc, $x + 6, $y + 32, $w - 12, 12, [176, 190, 204], 'left', 3);            $c->text('Rang ' . $rank . ' / ' . $max, $x + 10, $y + 80, 12, $rank >= $max ? [140, 230, 160] : [150, 166, 180]);            if ($rank < $max && $pts > 0) {                $bx = $x + $w - 44;                $c->pill($bx, $y + 74, 36, 24, [110, 200, 140, 170]);                $c->text('+', $bx + 18, $y + 75, 18, [255, 255, 255], 'center', false);                $c->hit($bx - 4, $y + 70, 44, 32, 'tallearn:' . $id);            }            $y += 108;        }    }}