KO
games/rob-revisited / src / Revisited / Ui / Hints.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;/** * How the context action is shown in the centre of the screen. Everyday actions (pull grass, pick berries, drink...) * are explained by the big hint the first few times they come up (a few seconds each, fading), or until the player * has done them once; after that only a small key cap under the crosshair says something can be done there. Actions * that matter (talk, a door, the mast, a fight...) always get the big hint. Mode: always | auto | never (Options). */final class Hints{    public const MODES = ['auto' => 'Auto', 'always' => 'Toujours', 'never' => 'Jamais'];    /** shown this many times (separate appearances) at most */    public const TIMES = 3;    /** seconds a big hint stays, the last one fading */    public const SECONDS = 4.0;    /** the actions always explained in full */    private const IMPORTANT = ['talk', 'exit', 'attack', 'brandish', 'read', 'open', 'fit_cell', 'fit_tuner', 'repair', 'transmit', 'mast', 'fire', 'skin', 'snare'];    public string $mode = 'auto';    /** @var array<string, array{0: int, 1: bool}> action id => [times shown, done once] */    public array $learned = [];    private ?string $current = null;    private float $age = 0.0;    public static function important(string $id): bool    {        return in_array($id, self::IMPORTANT, true) || str_starts_with($id, 'mast') || str_starts_with($id, 'trade');    }    /**     * The context action this frame ($id null: none). Returns 0..1: how much of the big hint to show (0 = only the     * small marker).     */    public function update(?string $id, float $dt): float    {        if ($id !== $this->current) {            $this->current = $id;            $this->age = 0.0;            if ($id !== null && !self::important($id) && $this->mode === 'auto') {                $l = $this->learned[$id] ?? [0, false];                $this->learned[$id] = [$l[0] + 1, $l[1]];            }        } else {            $this->age += $dt;        }        if ($id === null || $this->mode === 'never') {            return 0.0;        }        if ($this->mode === 'always' || self::important($id)) {            return 1.0;        }        [$times, $done] = $this->learned[$id] ?? [0, false];        if ($done || $times > self::TIMES) {            return 0.0;        }        if ($this->age >= self::SECONDS) {            return 0.0;        }        return min(1.0, (self::SECONDS - $this->age) / 0.8);    }    /** The player did that action: its big hint is not needed any more. */    public function done(?string $id): void    {        if ($id === null) {            return;        }        $l = $this->learned[$id] ?? [1, false];        $this->learned[$id] = [$l[0], true];    }    public function isLearned(string $id): bool    {        [$times, $done] = $this->learned[$id] ?? [0, false];        return $done || $times > self::TIMES;    }    /** @return array<string, array{0: int, 1: bool}> */    public function toArray(): array    {        return $this->learned;    }    public function load(mixed $a): void    {        $this->learned = [];        foreach (is_array($a) ? $a : [] as $id => $l) {            if (is_array($l)) {                $this->learned[(string) $id] = [(int) ($l[0] ?? 0), (bool) ($l[1] ?? false)];            }        }        $this->current = null;    }}