KO
games/alis / src / Revisited / Survivor.php · commit 9c722cf
<?phpnamespace App\Revisited;/** * The castaway's body (0..100 each, 100 = fine): food, water, rest, warmth, health. Time passes in game minutes; * the needs fall, the lack of one hurts the health. Rules of this prototype, written for it. */final class Survivor{    public float $food = 80;    public float $water = 70;    public float $rest = 90;    public float $warmth = 85;    public float $health = 100;    public int $wood = 0;    public int $fruit = 0;    /** @var list<string> */    public array $notes = [];    /**     * $minutes of game time: $cold 0..1 (night, altitude), $warmSource (near a fire), $moving (walking costs more).     */    public function live(float $minutes, float $cold, bool $warmSource, bool $moving): void    {        $h = $minutes / 60;        $this->food = max(0, $this->food - $h * ($moving ? 3.2 : 2.2));        $this->water = max(0, $this->water - $h * ($moving ? 6.0 : 4.0));        $this->rest = max(0, $this->rest - $h * ($moving ? 3.0 : 1.5));        $this->warmth = $warmSource ? min(100, $this->warmth + $h * 40) : max(0, min(100, $this->warmth - $h * ($cold * 14 - 3)));        $hurt = 0.0;        foreach (['food' => 1.5, 'water' => 3.0, 'rest' => 1.0, 'warmth' => 2.5] as $need => $rate) {            if ($this->$need <= 0) {                $hurt += $rate;            }        }        if ($hurt > 0) {            $this->health = max(0, $this->health - $h * $hurt * 4);        } elseif ($this->food > 40 && $this->water > 40) {            $this->health = min(100, $this->health + $h * 1.5);        }    }    public function dead(): bool    {        return $this->health <= 0;    }    public function drink(): void    {        $this->water = min(100, $this->water + 35);        $this->note("L'eau est tiède et a un goût de fer. Tu bois quand même.");    }    public function eat(): bool    {        if ($this->fruit <= 0) {            return false;        }        $this->fruit--;        $this->food = min(100, $this->food + 22);        $this->note('Le fruit est acide, sa chair fibreuse. Ça cale.');        return true;    }    public function note(string $text): void    {        $this->notes[] = $text;        if (count($this->notes) > 3) {            array_shift($this->notes);        }    }    /** The worst need, as the castaway feels it (null: nothing urgent). */    public function feeling(): ?string    {        $worst = min($this->food, $this->water, $this->rest, $this->warmth);        if ($worst > 30) {            return null;        }        return match ($worst) {            $this->water => 'Ta gorge brûle. Il te faut de l\'eau.',            $this->food => 'Ton estomac se tord.',            $this->warmth => 'Tu grelottes.',            default => 'Tes jambes ne te portent plus.',        };    }}