*/ private array $oldWeapons = []; private int $lastAttackDown = -1; private ?string $message = null; private int $messageTics = 0; public function __construct(private Gfx $gfx) { } public function reset(Player $p): void { $this->faceIndex = 0; $this->faceCount = 0; $this->priority = 0; $this->oldHealth = -1; $this->oldWeapons = $p->weaponOwned; $this->lastAttackDown = -1; $this->message = null; $this->messageTics = 0; } /** One tic: the face and the message line (ST_Ticker, HU_Ticker). */ public function tick(World $world): void { $p = $world->player; if ($p->message !== null) { $this->message = $p->message; $this->messageTics = self::MESSAGE_TICS; $p->message = null; } elseif ($this->messageTics > 0 && --$this->messageTics === 0) { $this->message = null; } $this->updateFace($p); $this->oldHealth = $p->health; } /** ST_calcPainOffset: 0 (healthy) .. 4 (nearly dead), times 8 faces. */ private function painOffset(Player $p): int { $health = min(100, $p->health); return 8 * intdiv((100 - $health) * 5, 101); } /** ST_updateFaceWidget, simplified: the faces by priority. */ private function updateFace(Player $p): void { // face numbers: pain*8 + 0..2 straight, 3 right, 4 left, 5 ouch, 6 evil grin, 7 rampage; 40 god, 41 dead if ($this->priority < 10 && !$p->health) { $this->priority = 9; $this->faceIndex = 41; $this->faceCount = 1; } if ($this->priority < 9 && $p->bonusCount) { foreach ($p->weaponOwned as $i => $owned) { if ($owned && !($this->oldWeapons[$i] ?? false)) { $this->priority = 8; // a new weapon: evil grin $this->faceCount = 70; $this->faceIndex = $this->painOffset($p) + 6; } } $this->oldWeapons = $p->weaponOwned; } if ($this->priority < 8 && $p->damageCount && $p->attacker !== null && $p->attacker !== $p->mo) { $this->priority = 7; if ($this->oldHealth - $p->health > 20) { $this->faceIndex = $this->painOffset($p) + 5; // ouch } else { $mo = $p->mo; $angle = World::normAngle(World::pointToAngle($mo->x, $mo->y, $p->attacker->x, $p->attacker->y) - $mo->angle); if ($angle < M_PI / 4 || $angle > 7 * M_PI / 4) { $this->faceIndex = $this->painOffset($p) + 7; // in front: rampage face } elseif ($angle < M_PI) { $this->faceIndex = $this->painOffset($p) + 4; // on the left } else { $this->faceIndex = $this->painOffset($p) + 3; } } $this->faceCount = 35; } if ($this->priority < 7 && $p->damageCount) { // hurt by the floor or a crusher $this->priority = 6; $this->faceIndex = $this->painOffset($p) + ($this->oldHealth - $p->health > 20 ? 5 : 7); $this->faceCount = 35; } if ($this->priority < 6) { // shooting for a while: rampage face if ($p->attackDown) { if ($this->lastAttackDown === -1) { $this->lastAttackDown = 2 * 35; } elseif (!--$this->lastAttackDown) { $this->priority = 5; $this->faceIndex = $this->painOffset($p) + 7; $this->faceCount = 1; $this->lastAttackDown = 1; } } else { $this->lastAttackDown = -1; } } if ($this->priority < 5 && (($p->cheats & 2) || $p->powers[Player::PW_INVULNERABILITY])) { $this->priority = 4; $this->faceIndex = 40; $this->faceCount = 1; } if (!$this->faceCount) { // look around $this->faceIndex = $this->painOffset($p) + (Random::m() % 3); $this->faceCount = 17; $this->priority = 0; } $this->faceCount--; } private function facePatch(int $index): string { if ($index === 40) { return 'STFGOD0'; } if ($index === 41) { return 'STFDEAD0'; } $pain = intdiv($index, 8); return match ($index % 8) { 0, 1, 2 => "STFST{$pain}" . ($index % 8), 3 => "STFTR{$pain}0", 4 => "STFTL{$pain}0", 5 => "STFOUCH{$pain}", 6 => "STFEVL{$pain}", default => "STFKILL{$pain}", }; } /** STlib_drawNum: right-aligned at x. */ private function drawNum(Framebuffer $fb, int $x, int $y, int $num, string $font, int $digits): void { $num = max(0, $num); $w = $this->gfx->patch("{$font}0")->width; if ($num === 0) { $fb->drawPatch($x - $w, $y, $this->gfx->patch("{$font}0")); return; } while ($num > 0 && $digits-- > 0) { $x -= $w; $fb->drawPatch($x, $y, $this->gfx->patch($font . ($num % 10))); $num = intdiv($num, 10); } } public function draw(Framebuffer $fb, Player $p): void { $g = $this->gfx; $fb->drawPatch(0, self::Y, $g->patch('STBAR')); $ammoType = Player::WEAPONINFO[$p->readyWeapon][0]; if ($ammoType !== Player::AM_NOAMMO) { $this->drawNum($fb, 44, 171, $p->ammo[$ammoType], 'STTNUM', 3); } $this->drawNum($fb, 90, 171, $p->health, 'STTNUM', 3); $fb->drawPatch(90, 171, $g->patch('STTPRCNT')); $this->drawNum($fb, 221, 171, $p->armorPoints, 'STTNUM', 3); $fb->drawPatch(221, 171, $g->patch('STTPRCNT')); // arms $fb->drawPatch(104, 168, $g->patch('STARMS')); for ($i = 0; $i < 6; $i++) { $owned = $p->weaponOwned[$i + 1] || ($i === 1 && $p->weaponOwned[Player::WP_SUPERSHOTGUN]); $font = $owned ? 'STYSNUM' : 'STGNUM'; $fb->drawPatch(111 + ($i % 3) * 12, 172 + intdiv($i, 3) * 10, $g->patch($font . ($i + 2))); } // face $fb->drawPatch(143, 168, $g->patch($this->facePatch($this->faceIndex))); // keys: the skull when he has it, else the card foreach ([171, 181, 191] as $k => $y) { if ($p->cards[$k + 3]) { $fb->drawPatch(239, $y, $g->patch('STKEYS' . ($k + 3))); } elseif ($p->cards[$k]) { $fb->drawPatch(239, $y, $g->patch('STKEYS' . $k)); } } // ammo counts: bullets, shells, cells, rockets (rows 173, 179, 191, 185) foreach ([173, 179, 191, 185] as $a => $y) { $this->drawNum($fb, 288, $y, $p->ammo[$a], 'STYSNUM', 3); $this->drawNum($fb, 314, $y, $p->maxAmmo[$a], 'STYSNUM', 3); } } /** The message line (HU_Drawer). */ public function drawMessage(Framebuffer $fb): void { if ($this->message !== null) { self::drawText($this->gfx, $fb, 0, 0, $this->message); } } /** The hu_font (STCFN033..095): upper case letters, digits, punctuation. */ public static function drawText(Gfx $g, Framebuffer $fb, int $x, int $y, string $text): void { $sx = $x; $text = strtoupper($text); $n = strlen($text); for ($i = 0; $i < $n; $i++) { $c = ord($text[$i]); if ($c === 10) { $x = $sx; $y += 11; continue; } if ($c < 33 || $c > 95) { $x += 4; continue; } $patch = $g->patch(sprintf('STCFN%03d', $c)); if ($x + $patch->width > 320) { break; } $fb->drawPatch($x, $y, $patch); $x += $patch->width; } } public static function textWidth(Gfx $g, string $text): int { $w = 0; $text = strtoupper($text); for ($i = 0, $n = strlen($text); $i < $n; $i++) { $c = ord($text[$i]); $w += ($c < 33 || $c > 95) ? 4 : $g->patch(sprintf('STCFN%03d', $c))->width; } return $w; } /** ST_doPaletteStuff: red when hurt, gold for a pickup, green with the suit. */ public static function palette(Player $p): int { $cnt = $p->damageCount; if ($p->powers[Player::PW_STRENGTH]) { $bzc = 12 - ($p->powers[Player::PW_STRENGTH] >> 6); if ($bzc > $cnt) { $cnt = $bzc; } } if ($cnt > 0) { return 1 + min(7, ($cnt + 7) >> 3); } if ($p->bonusCount) { return 9 + min(3, ($p->bonusCount + 7) >> 3); } $iron = $p->powers[Player::PW_IRONFEET]; if ($iron > 4 * 32 || ($iron & 8)) { return 13; } return 0; } }