UK
games/alis / src / Scene / RevisitedScene.php · commit 9c722cf
<?phpnamespace App\Scene;use App\Revisited\Survivor;use App\Revisited\World;use JumPHP\Gl\Mat4;use JumPHP\Renderer\Renderer;use JumPHP\Scene\AbstractScene;/** * "Experience - Rob revisited - test": an original survival prototype (not a remake of the ALIS games). A castaway * whose survey pod crashed on an unknown planet: first-person 3D land (OpenGL / WebGL at the game's 320 x 240, big * pixels), day and night, food, water, rest, warmth, health; fruit, dead wood, a fire, something that hunts at night. * * Keys: arrows (walk, turn), Shift held (run), Space / Return (act: drink, pick, gather), E (eat), F (light a fire: 3 wood), * R (sleep by the fire), Escape (back to the list). */final class RevisitedScene extends AbstractScene{    private const VS = <<<'GLSL'        attribute vec3 a_pos;        attribute vec3 a_normal;        attribute vec3 a_color;        uniform mat4 u_mvp;        uniform mat4 u_model;        uniform vec3 u_eye;        uniform vec3 u_sundir;        uniform float u_sun;        uniform float u_ambient;        uniform float u_emissive;        uniform vec3 u_firepos;        uniform float u_fire;        uniform mediump float u_sky;        varying vec3 v_color;        varying float v_fog;        varying vec3 v_world;        varying vec2 v_tile;        void main() {            vec4 w = u_model * vec4(a_pos, 1.0);            v_world = u_sky > 0.5 ? a_pos : w.xyz;            v_tile = mod(w.xz, 32.0);              // small numbers for the fragment shader (mediump)            gl_Position = u_mvp * w;            vec3 n = normalize((u_model * vec4(a_normal, 0.0)).xyz);            float d = distance(w.xyz, u_firepos);            float fire = u_fire * clamp(1.0 - d / 14.0, 0.0, 1.0);            float light = u_ambient + u_sun * max(dot(n, u_sundir), 0.0) + fire * 0.9;            v_color = a_color * light * vec3(1.0 + fire * 0.4, 1.0 + fire * 0.1, 1.0 - fire * 0.2) + a_color * u_emissive;            v_fog = u_sky > 0.5 ? 0.0 : clamp((distance(w.xyz, u_eye) - 40.0) / 110.0, 0.0, 1.0);        }        GLSL;    private const FS = <<<'GLSL'        precision mediump float;        uniform vec3 u_fog;        uniform vec3 u_zenith;        uniform vec3 u_sunsky;        uniform float u_alpha;        uniform float u_sky;        uniform float u_detail;        varying vec3 v_color;        varying float v_fog;        varying vec3 v_world;        varying vec2 v_tile;        // a hash without sin() (mediump: no big numbers), periodic so that the 32 m tiles join without a seam        float hash(vec2 p, float period) {            p = mod(p, period);            vec2 q = fract(p * vec2(0.1031, 0.1137));            q += dot(q, q.yx + 19.19);            return fract((q.x + q.y) * q.x);        }        float noise(vec2 p, float period) {            vec2 i = floor(p); vec2 f = fract(p); f = f * f * (3.0 - 2.0 * f);            return mix(mix(hash(i, period), hash(i + vec2(1.0, 0.0), period), f.x),                       mix(hash(i + vec2(0.0, 1.0), period), hash(i + vec2(1.0, 1.0), period), f.x), f.y);        }        void main() {            if (u_sky > 0.5) {                // the sky: from the haze of the horizon to the zenith, the sun's disc and its glow                vec3 d = normalize(v_world);                vec3 c = mix(u_fog, u_zenith, clamp(d.y * 1.6, 0.0, 1.0));                float s = max(dot(d, normalize(u_sunsky)), 0.0);                c += vec3(1.0, 0.85, 0.6) * (pow(s, 400.0) * 1.5 + pow(s, 12.0) * 0.18) * step(0.0, u_sunsky.y);                gl_FragColor = vec4(c, 1.0);                return;            }            // the ground's grain: two scales of noise on the world's position (grass blades, pebbles)            float n = noise(v_tile * 2.0, 64.0) * 0.6 + noise(v_tile * 8.0, 256.0) * 0.4;            vec3 c = v_color * (1.0 + (n - 0.5) * 0.45 * u_detail);            gl_FragColor = vec4(mix(c, u_fog, v_fog), u_alpha);        }        GLSL;    private World $world;    private Survivor $body;    private float $x = 0;    private float $z = 0;    private float $angle = 0;    /** game minutes since the crash (day 1, 07:00) */    private float $minutes = 0;    private float $time = 0;    private ?array $fire = null;              // [x, z, minutes left]    private ?array $beast = null;             // [x, z, cooldown]    private string $prompt = '';    private bool $over = false;    private bool $running = false;    /** the texts at the window's resolution (sharp at any size), App\Hd\ScreenText */    private ?\App\Hd\ScreenText $text = null;    private int $prog = 0;    private int $land = 0;    private int $landIdx = 0;    private int $landCount = 0;    private int $water = 0;    private int $things = 0;    private int $thingsCount = 0;    private int $beastMesh = 0;    private int $beastCount = 0;    private int $fireMesh = 0;    private int $sky = 0;    private int $skyCount = 0;    private int $fireCount = 0;    private bool $thingsDirty = true;    public function enter(array $params = []): void    {        $this->world = new World((int) ($params['seed'] ?? 7));        $this->body = new Survivor();        $c = $this->world->extent() / 2;        $this->x = $c;        $this->z = $c - 2;        $this->angle = 0.4;        $this->minutes = 7 * 60;        $this->time = 0;        $this->fire = $this->beast = null;        $this->over = false;        $this->thingsDirty = true;        $this->body->note('Le module de reconnaissance est une épave fumante. La balise ne répond plus.');        $this->body->note('Il faut tenir : de l\'eau, de quoi manger, du feu avant la nuit.');        $this->text ??= new \App\Hd\ScreenText($this->engine->config->path('assets/fonts/hd.ttf'));        $this->text->attach($this->sdl()->getRenderer());        $gl = $this->engine->gl();        if ($gl->isAvailable()) {            $gl->setLayer('below', 'view', [255, 0, 255]);            $this->prog = $gl->program(self::VS, self::FS);            $this->buildLand();            $this->water = $gl->buffer(self::quad($this->world->extent(), World::WATER, [0.16, 0.24, 0.27]));            [$m, $n] = $this->mesh(fn (array &$v) => $this->beastShape($v));            $this->beastMesh = $m;            $this->beastCount = $n;            [$m, $n] = $this->mesh(fn (array &$v) => self::dome($v));            $this->sky = $m;            $this->skyCount = $n;            [$m, $n] = $this->mesh(fn (array &$v) => self::cone($v, 0, 0, 0, 0.45, 0.9, [1.0, 0.55, 0.15], 6));            $this->fireMesh = $m;            $this->fireCount = $n;        }    }    public function exit(): void    {        $this->text?->detach();        $this->engine->gl()->disable();    }    // ---- the world's meshes: x y z, normal, colour (9 floats per vertex)    private function buildLand(): void    {        $w = $this->world;        $n = World::SIZE + 1;        $v = [];        mt_srand($w->seed + 1);        for ($iz = 0; $iz < $n; $iz++) {            for ($ix = 0; $ix < $n; $ix++) {                $h = $w->height[$iz * $n + $ix];                $hx = $w->height[$iz * $n + min($ix + 1, $n - 1)] - $w->height[$iz * $n + max($ix - 1, 0)];                $hz = $w->height[min($iz + 1, $n - 1) * $n + $ix] - $w->height[max($iz - 1, 0) * $n + $ix];                $nx = -$hx / (2 * World::CELL);                $nz = -$hz / (2 * World::CELL);                $l = sqrt($nx * $nx + 1 + $nz * $nz);                $slope = 1 - 1 / $l;                $j = (mt_rand(0, 100) - 50) / 900;                $c = match (true) {                    $h < World::WATER + 0.3 => [0.42, 0.38, 0.27],                  // mud by the lake                    $slope > 0.35 || $h > 20 => [0.36 + $j, 0.33 + $j, 0.30 + $j], // rock                    $h > 12 => [0.33 + $j, 0.30 + $j, 0.18],                        // dry heights                    default => [0.20 + $j, 0.30 + $j * 2, 0.13],                    // the jungle's grass                };                array_push($v, $ix * World::CELL, $h, $iz * World::CELL, $nx / $l, 1 / $l, $nz / $l, ...$c);            }        }        $idx = [];        for ($iz = 0; $iz < World::SIZE; $iz++) {            for ($ix = 0; $ix < World::SIZE; $ix++) {                $a = $iz * $n + $ix;                array_push($idx, $a, $a + $n, $a + 1, $a + 1, $a + $n, $a + $n + 1);            }        }        $gl = $this->engine->gl();        $this->land = $gl->buffer($v);        $this->landIdx = $gl->indexBuffer($idx);        $this->landCount = count($idx);    }    /** @return array{0: int, 1: int} buffer, vertices */    private function mesh(callable $fill): array    {        $v = [];        $fill($v);        return [$this->engine->gl()->buffer($v), intdiv(count($v), 9)];    }    private function buildThings(): void    {        $v = [];        foreach ($this->world->things as $t) {            $y = $this->world->heightAt($t['x'], $t['z']);            switch ($t['kind']) {                case 'tree':                    // each tree its own size and shade (from its position: the same every time)                    $k = 0.75 + fmod(abs($t['x'] * 7.3 + $t['z'] * 3.1), 0.6);                    $g = fmod(abs($t['x'] * 1.7 + $t['z'] * 5.3), 0.08);                    self::box($v, $t['x'], $y - 0.2, $t['z'], 0.3 * $k, 3.4 * $k, 0.3 * $k, [0.24, 0.16, 0.10]);                    self::cone($v, $t['x'], $y + 1.8 * $k, $t['z'], 2.1 * $k, 2.6 * $k, [0.07 + $g, 0.19 + $g, 0.08], 9);                    self::cone($v, $t['x'], $y + 3.0 * $k, $t['z'], 1.7 * $k, 2.3 * $k, [0.09 + $g, 0.23 + $g, 0.09], 9);                    self::cone($v, $t['x'], $y + 4.2 * $k, $t['z'], 1.1 * $k, 2.0 * $k, [0.11 + $g, 0.27 + $g, 0.10], 9);                    break;                case 'rock':                    $k = 0.5 + fmod(abs($t['x'] * 3.7 + $t['z'] * 9.1), 1.0);                    self::cone($v, $t['x'], $y - 0.3, $t['z'], 1.1 * $k, 1.2 * $k, [0.40, 0.38, 0.35], 5);                    break;                case 'bush':                    self::cone($v, $t['x'], $y - 0.1, $t['z'], 1.0, 0.9, [0.12, 0.25, 0.10], 8);                    self::cone($v, $t['x'] + 0.2, $y + 0.3, $t['z'] - 0.1, 0.7, 0.9, [0.15, 0.30, 0.12], 8);                    for ($k = 0; $k < $t['left']; $k++) {                        self::box($v, $t['x'] + cos($k * 2.1) * 0.5, $y + 0.6, $t['z'] + sin($k * 2.1) * 0.5, 0.18, 0.18, 0.18, [0.75, 0.25, 0.12]);                    }                    break;                case 'wood':                    if ($t['left'] > 0) {                        self::box($v, $t['x'], $y, $t['z'], 1.2, 0.15, 0.15, [0.35, 0.25, 0.15]);                    }                    break;                case 'wreck':                    self::box($v, $t['x'], $y - 0.3, $t['z'], 3.0, 1.6, 5.0, [0.45, 0.46, 0.47]);                    self::box($v, $t['x'] + 1.8, $y, $t['z'] - 1.0, 2.2, 0.15, 1.0, [0.38, 0.38, 0.40]);                    break;            }        }        $gl = $this->engine->gl();        if ($this->things > 0) {            $gl->updateBuffer($this->things, $v);        } else {            $this->things = $gl->buffer($v, true);        }        $this->thingsCount = intdiv(count($v), 9);        $this->thingsDirty = false;    }    private function beastShape(array &$v): void    {        self::box($v, 0, 0.6, 0, 0.7, 0.8, 1.8, [0.08, 0.07, 0.07]);        // body        self::box($v, 0, 1.0, 1.05, 0.5, 0.5, 0.6, [0.10, 0.08, 0.08]);     // head        self::box($v, 0.12, 1.15, 1.36, 0.08, 0.08, 0.02, [0.9, 0.7, 0.2]); // eyes (lit by u_emissive... a little)        self::box($v, -0.12, 1.15, 1.36, 0.08, 0.08, 0.02, [0.9, 0.7, 0.2]);        foreach ([[-0.3, -0.6], [0.3, -0.6], [-0.3, 0.6], [0.3, 0.6]] as [$lx, $lz]) {            self::box($v, $lx, 0, $lz, 0.18, 0.6, 0.18, [0.07, 0.06, 0.06]);        }    }    /** A box standing on (x, y, z): its base centred there. */    private static function box(array &$v, float $x, float $y, float $z, float $w, float $h, float $d, array $c): void    {        $x0 = $x - $w / 2; $x1 = $x + $w / 2; $z0 = $z - $d / 2; $z1 = $z + $d / 2; $y1 = $y + $h;        $faces = [            [[0, 1, 0], [$x0, $y1, $z0], [$x0, $y1, $z1], [$x1, $y1, $z1], [$x1, $y1, $z0]],            [[0, 0, 1], [$x0, $y, $z1], [$x1, $y, $z1], [$x1, $y1, $z1], [$x0, $y1, $z1]],            [[0, 0, -1], [$x1, $y, $z0], [$x0, $y, $z0], [$x0, $y1, $z0], [$x1, $y1, $z0]],            [[1, 0, 0], [$x1, $y, $z1], [$x1, $y, $z0], [$x1, $y1, $z0], [$x1, $y1, $z1]],            [[-1, 0, 0], [$x0, $y, $z0], [$x0, $y, $z1], [$x0, $y1, $z1], [$x0, $y1, $z0]],        ];        foreach ($faces as [$n, $a, $b, $cc, $dd]) {            foreach ([$a, $b, $cc, $a, $cc, $dd] as $p) {                array_push($v, $p[0], $p[1], $p[2], $n[0], $n[1], $n[2], ...$c);            }        }    }    private static function cone(array &$v, float $x, float $y, float $z, float $r, float $h, array $c, int $seg): void    {        for ($i = 0; $i < $seg; $i++) {            $a0 = $i * 2 * M_PI / $seg;            $a1 = ($i + 1) * 2 * M_PI / $seg;            $am = ($a0 + $a1) / 2;            $n = [cos($am) * 0.8, 0.6, sin($am) * 0.8];            foreach ([[$x + cos($a0) * $r, $y, $z + sin($a0) * $r], [$x, $y + $h, $z], [$x + cos($a1) * $r, $y, $z + sin($a1) * $r]] as $p) {                array_push($v, $p[0], $p[1], $p[2], $n[0], $n[1], $n[2], ...$c);            }        }    }    /** The sky's dome: a hemisphere of radius 150 around the eye (its colour is computed by the shader). */    private static function dome(array &$v): void    {        $r = 150.0;        for ($i = 0; $i < 6; $i++) {            for ($j = 0; $j < 16; $j++) {                $p = [];                foreach ([[$i, $j], [$i + 1, $j], [$i + 1, $j + 1], [$i, $j], [$i + 1, $j + 1], [$i, $j + 1]] as [$a, $b]) {                    $el = ($a / 6 - 0.15) * M_PI / 2;                    $az = $b / 16 * 2 * M_PI;                    $p[] = [cos($el) * cos($az) * $r, sin($el) * $r, cos($el) * sin($az) * $r];                }                foreach ($p as $q) {                    array_push($v, $q[0], $q[1], $q[2], 0, 1, 0, 1, 1, 1);                }            }        }    }    private static function quad(float $size, float $y, array $c): array    {        $v = [];        foreach ([[0, 0], [0, $size], [$size, $size], [0, 0], [$size, $size], [$size, 0]] as [$x, $z]) {            array_push($v, $x, $y, $z, 0, 1, 0, ...$c);        }        return $v;    }    // ---- the day, the body, the night's hunter    /** 0 (midnight) .. 1: the sun's height, -1 .. 1 */    private function sun(): float    {        return sin((fmod($this->minutes, 1440) / 1440 - 0.25) * 2 * M_PI);    }    public function update(float $dt): void    {        $this->time += $dt;        $kb = $this->engine->input->getKeyboard();        $typed = $kb->getTyped();        if ($this->actions()->isPressed('back')) {            $this->scenes()->switchTo(TitleScene::class);            return;        }        if ($this->over) {            if ($this->actions()->isPressed('confirm')) {                $this->enter();            }            return;        }        $a = $this->actions();        $turn = $a->axis('left', 'right');        $walk = ($a->isHeld('up') ? 1 : 0) - ($a->isHeld('down') ? 1 : 0);        $this->angle -= $turn * 1.8 * $dt;        $tired = $this->body->rest < 15 ? 0.5 : 1.0;        // Shift (or the pad's A / B) to run: faster, but hunger, thirst and tiredness come twice as fast        $this->running = $walk > 0 && $this->body->rest > 10            && ($kb->isKeyHeld(\SDLK_LSHIFT) || $kb->isKeyHeld(\SDLK_RSHIFT) || $a->isHeld('fire'));        $speed = ($this->running ? 7.5 : 4.2) * $tired;        if ($walk !== 0) {            $nx = $this->x + sin($this->angle) * $walk * $speed * $dt;            $nz = $this->z + cos($this->angle) * $walk * $speed * $dt;            $e = $this->world->extent();            if ($nx > 4 && $nz > 4 && $nx < $e - 4 && $nz < $e - 4 && $this->world->heightAt($nx, $nz) > World::WATER - 0.6) {                $this->x = $nx;                $this->z = $nz;            }        }        // game time: 1 real second = 4 minutes (a day lasts 6 minutes)        $minutes = $dt * 4;        $this->minutes += $minutes;        $sun = $this->sun();        $height = $this->world->heightAt($this->x, $this->z);        $nearFire = $this->fire !== null && hypot($this->fire[0] - $this->x, $this->fire[1] - $this->z) < 5;        $cold = max(0.0, min(1.0, 0.35 - $sun * 0.6 + $height / 40));        $this->body->live($this->running ? $minutes * 2 : $minutes, $cold, $nearFire, $walk !== 0);        if ($this->fire !== null) {            $this->fire[2] -= $minutes;            if ($this->fire[2] <= 0) {                $this->fire = null;                $this->body->note('Le feu n\'est plus que des braises froides.');            }        }        $this->prompt = $this->act(in_array(\SDLK_SPACE, $typed, true) || in_array(\SDLK_RETURN, $typed, true));        if (in_array(\SDLK_e, $typed, true) && !$this->body->eat()) {            $this->body->note('Tu n\'as rien à manger.');        }        if (in_array(\SDLK_f, $typed, true)) {            if ($this->body->wood >= 3) {                $this->body->wood -= 3;                $this->fire = [$this->x + sin($this->angle) * 1.5, $this->z + cos($this->angle) * 1.5, 180];                $this->body->note('Après de longues minutes, une flamme prend. Elle tiendra trois heures.');            } else {                $this->body->note('Il faut trois branches sèches pour un feu.');            }        }        if (in_array(\SDLK_r, $typed, true)) {            if ($nearFire) {                $this->minutes += 120;                $this->fire[2] = max(10, $this->fire[2] - 120);                $this->body->rest = min(100, $this->body->rest + 45);                $this->body->live(120, 0.0, true, false);                $this->body->note('Tu dors deux heures, le dos contre la chaleur.');            } else {                $this->body->note('Dormir à découvert, sans feu ? Pas ici.');            }        }        $this->hunter($dt, $sun);        if ($this->body->dead()) {            $this->over = true;        }    }    /** What the act key does here (and the prompt saying it). */    private function act(bool $now): string    {        $w = $this->world;        $ahead = [$this->x + sin($this->angle) * 1.5, $this->z + cos($this->angle) * 1.5];        if ($w->heightAt($ahead[0], $ahead[1]) < World::WATER) {            if ($now) {                $this->body->drink();            }            return 'ESPACE : boire';        }        $i = $w->nearest($this->x, $this->z, 2.6, ['bush', 'wood', 'wreck']);        if ($i === null) {            return '';        }        $t = &$w->things[$i];        if ($t['kind'] === 'bush' && $t['left'] > 0) {            if ($now) {                $t['left']--;                $this->body->fruit++;                $this->thingsDirty = true;                $this->body->note('Tu cueilles un fruit violacé. Comestible ? On verra.');            }            return 'ESPACE : cueillir un fruit';        }        if ($t['kind'] === 'wood' && $t['left'] > 0) {            if ($now) {                $t['left'] = 0;                $this->body->wood++;                $this->thingsDirty = true;                $this->body->note('Une branche morte, assez sèche pour brûler.');            }            return 'ESPACE : ramasser la branche';        }        if ($t['kind'] === 'wreck') {            if ($now) {                $this->body->note('Coque éventrée, électronique noyée. La balise est hors d\'usage.');            }            return 'ESPACE : fouiller l\'épave';        }        return '';    }    /** At night something comes out of the jungle; it keeps away from the fire. */    private function hunter(float $dt, float $sun): void    {        if ($sun > -0.05) {            $this->beast = null;            return;        }        if ($this->beast === null) {            $a = $this->angle + M_PI + (fmod($this->minutes, 7) - 3.5) * 0.3;            $this->beast = [$this->x + sin($a) * 45, $this->z + cos($a) * 45, 0.0];            $this->body->note('Un cri rauque, quelque part derrière toi.');        }        [$bx, $bz, $cool] = $this->beast;        $dx = $this->x - $bx;        $dz = $this->z - $bz;        $d = max(0.01, hypot($dx, $dz));        $fear = $this->fire !== null && hypot($this->fire[0] - $bx, $this->fire[1] - $bz) < 9;        $speed = $fear ? -2.5 : 2.6;        if ($d > 1.6 || $fear) {            $bx += $dx / $d * $speed * $dt;            $bz += $dz / $d * $speed * $dt;        }        $cool = max(0.0, $cool - $dt);        if ($d < 1.8 && !$fear && $cool <= 0) {            $this->body->health = max(0, $this->body->health - 18);            $this->body->note('Des crocs dans ton mollet. Tu te dégages, en sang.');            $cool = 2.5;        }        $this->beast = [$bx, $bz, $cool];    }    // ---- drawing    public function render(Renderer $r): void    {        $gl = $this->engine->gl();        $sun = $this->sun();        $day = max(0.0, min(1.0, $sun * 2 + 0.3));        $dusk = max(0.0, 1 - abs($sun) * 5);        $sky = [0.08 + 0.50 * $day + 0.25 * $dusk, 0.09 + 0.55 * $day + 0.08 * $dusk, 0.14 + 0.55 * $day - 0.05 * $dusk];        if (!$gl->isAvailable()) {            $r->clear([20, 24, 30]);            $this->label($r, 'Ce prototype a besoin d\'OpenGL / WebGL.', 20, 100, [230, 200, 120]);        } else {            $r->clear([255, 0, 255]);            if ($this->thingsDirty) {                $this->buildThings();            }            $eyeY = $this->world->heightAt($this->x, $this->z) + 1.7 + ($this->running ? sin($this->time * 12) * 0.06 : sin($this->time * 7) * 0.02);            $eye = [$this->x, $eyeY, $this->z];            $target = [$this->x + sin($this->angle), $eyeY - 0.05, $this->z + cos($this->angle)];            $vp = Mat4::multiply(Mat4::perspective(62, 320 / 240, 0.1, 200), Mat4::lookAt($eye, $target));            $sunDir = [0.4, max(0.15, $sun), 0.5];            $l = sqrt($sunDir[0] ** 2 + $sunDir[1] ** 2 + $sunDir[2] ** 2);            $fire = $this->fire !== null ? 0.8 + 0.2 * sin($this->time * 11) : 0.0;            $firePos = $this->fire !== null ? [$this->fire[0], $this->world->heightAt($this->fire[0], $this->fire[1]) + 0.5, $this->fire[1]] : [0, -100, 0];            $common = [                'u_mvp' => $vp, 'u_model' => Mat4::identity(), 'u_eye' => $eye, 'u_fog' => $sky, 'u_alpha' => 1.0,                'u_sundir' => [$sunDir[0] / $l, $sunDir[1] / $l, $sunDir[2] / $l], 'u_sun' => 0.85 * $day,                'u_ambient' => 0.12 + 0.30 * $day, 'u_emissive' => 0.0, 'u_firepos' => $firePos, 'u_fire' => $fire,                'u_sky' => 0.0, 'u_detail' => 1.0, 'u_sunsky' => [0.4, $sun, 0.5],                'u_zenith' => [0.02 + 0.22 * $day, 0.03 + 0.36 * $day, 0.08 + 0.62 * $day],            ];            $attrs = static fn (int $b): array => ['a_pos' => [$b, 3, 9, 0], 'a_normal' => [$b, 3, 9, 3], 'a_color' => [$b, 3, 9, 6]];            $gl->clear([(int) ($sky[0] * 255), (int) ($sky[1] * 255), (int) ($sky[2] * 255), 255]);            $gl->draw($this->prog, ['attributes' => $attrs($this->sky), 'count' => $this->skyCount, 'depth' => false,                'uniforms' => ['u_model' => Mat4::translate($eye[0], $eye[1], $eye[2]), 'u_sky' => 1.0] + $common]);            $gl->draw($this->prog, ['attributes' => $attrs($this->land), 'indices' => $this->landIdx, 'count' => $this->landCount, 'uniforms' => $common]);            $gl->draw($this->prog, ['attributes' => $attrs($this->things), 'count' => $this->thingsCount, 'uniforms' => ['u_detail' => 0.5] + $common]);            if ($this->beast !== null) {                $by = $this->world->heightAt($this->beast[0], $this->beast[1]);                $face = atan2($this->x - $this->beast[0], $this->z - $this->beast[1]);                $gl->draw($this->prog, ['attributes' => $attrs($this->beastMesh), 'count' => $this->beastCount,                    'uniforms' => ['u_model' => Mat4::multiply(Mat4::translate($this->beast[0], $by, $this->beast[1]), Mat4::rotateY($face)), 'u_emissive' => 0.15] + $common]);            }            if ($this->fire !== null) {                $gl->draw($this->prog, ['attributes' => $attrs($this->fireMesh), 'count' => $this->fireCount,                    'uniforms' => ['u_model' => Mat4::translate($firePos[0], $firePos[1] - 0.5, $firePos[2]), 'u_emissive' => 1.2] + $common]);            }            $gl->draw($this->prog, ['attributes' => $attrs($this->water), 'blend' => true,                'uniforms' => ['u_alpha' => 0.78, 'u_emissive' => 0.25 * $day, 'u_detail' => 0.25] + $common]);        }        $this->hud($r);    }    private function hud(Renderer $r): void    {        // the panel under the view (the view is 320 x 200, the panel the 40 rows below)        $r->fillRect(0, 200, 320, 40, [26, 24, 20]);        $r->drawLine(0, 200, 320, 200, [92, 82, 60]);        $b = $this->body;        $gauges = [['FAIM', $b->food, [190, 140, 60]], ['EAU', $b->water, [70, 130, 190]], ['REPOS', $b->rest, [150, 150, 110]],            ['CHALEUR', $b->warmth, [200, 90, 50]], ['SANTÉ', $b->health, [170, 40, 40]]];        foreach ($gauges as $k => [$name, $value, $color]) {            $x = 4 + $k * 63;            $this->label($r, $name, $x, 203, [170, 160, 140]);            $r->fillRect($x, 214, 58, 4, [50, 46, 40]);            $r->fillRect($x, 214, 58 * $value / 100, 4, $color);        }        $day = (int) floor($this->minutes / 1440) + 1;        $m = (int) fmod($this->minutes, 1440);        $this->label($r, sprintf('Jour %d  %02d:%02d   fruits %d   bois %d', $day, intdiv($m, 60), $m % 60, $b->fruit, $b->wood), 4, 224, [200, 190, 160]);        // the notes, over the view (newest at the bottom), and what the act key does        if ($b->notes !== []) {            $r->fillRect(0, 0, 320, 4 + 11 * count($b->notes), [0, 0, 0, 90]);        }        $y = 4;        foreach ($b->notes as $note) {            $this->label($r, $note, 4, $y, [235, 228, 210], true);            $y += 11;        }        if (($f = $b->feeling()) !== null) {            $this->label($r, $f, 4, 176, [240, 170, 120], true);        }        if ($this->prompt !== '') {            $this->label($r, $this->prompt, 4, 188, [255, 240, 180], true);        }        if ($this->over) {            $r->fillRect(0, 70, 320, 60, [0, 0, 0, 200]);            $this->label($r, sprintf('Tu n\'as pas survécu. Jour %d.', $day), 90, 86, [230, 200, 190]);            $this->label($r, 'ENTRÉE : recommencer    ÉCHAP : quitter', 62, 104, [200, 190, 170]);        }    }    private function label(Renderer $r, string $text, float $x, float $y, array $color, bool $shadow = false, float $size = 7.5): void    {        if ($shadow) {            $this->text?->show($text, $x + 0.6, $y + 0.6, $size, [0, 0, 0]);        }        $this->text?->show($text, $x, $y, $size, $color);    }}