game; $g->load(fn (string $p) => $this->engine->config->path($p), $this->sound(), $params['iwad'] ?? null); $g->inLevel = true; $g->alwaysRun = (bool) $this->engine->options->get('always_run', true); $g->mouseSpeed = (float) $this->engine->options->get('mouse_speed', 0.012); $this->demo = !empty($params['demo']); if (isset($params['load']) && is_array($params['load'])) { $this->restore($params['load']); return; } $this->episode = $g->commercial ? 1 : (int) ($params['episode'] ?? 1); $this->map = (int) ($params['map'] ?? 1); if (isset($params['skill'])) { $g->skill = (int) $params['skill']; } if (!empty($params['newGame']) || $this->demo) { $g->world->player = new Player(); $g->world->player->playerState = Player::REBORN; } $this->startLevel(); } private function startLevel(): void { $g = $this->game; $g->world->setupLevel($this->episode, $this->map, $g->skill); $this->showLevel(); } private function showLevel(): void { $g = $this->game; $g->view->setSize(320, 200 - StatusBar::HEIGHT); $g->view->setMap($g->world->map, $g->skyName($this->episode, $this->map)); $g->statusBar->reset($g->world->player); $g->automap->reset($g->world); $g->changeMusic($this->engine->pcm(), 'level', $this->episode, $this->map); $this->accumulator = 0; $this->turnHeld = 0; $this->botTurn = 0; } public function exit(): void { $this->game->inLevel = false; } public function resume(mixed $result = null): void { if (!is_array($result)) { return; } if (!empty($result['restart'])) { $this->game->world->player->playerState = Player::REBORN; $this->startLevel(); } elseif (isset($result['save'])) { $ok = SaveGames::write($this->engine->storage, $this->game->iwad, (int) $result['save'], $this->game->world, $this->episode, $this->map); $this->game->world->player->message = $ok ? 'GAME SAVED.' : 'SAVE FAILED.'; } elseif (isset($result['load']) && is_array($result['load'])) { $this->restore($result['load']); } } /** A save game: its world replaces the running one. @param array $state */ private function restore(array $state): void { $g = $this->game; $world = $state['world']; $world->attach($g->wad, $g->gfx); $g->world = $world; $g->skill = $world->skill; Random::restore($state['random']); $this->episode = (int) $state['episode']; $this->map = (int) $state['map']; $this->showLevel(); $this->accumulator = 0; $world->player->message = 'GAME LOADED.'; } public function hotState(): array { return ['episode' => $this->episode, 'map' => $this->map]; } public function hotRestore(array $state): void { $this->episode = (int) $state['episode']; $this->map = (int) $state['map']; $this->startLevel(); } public function update(float $dt): void { if (!$this->demo && $this->actions()->isPressed('pause')) { $this->scenes()->push(PauseScene::class); return; } $this->game->pumpMusic($this->engine->pcm()); $this->latchInput(); $this->accumulator += min($dt, 0.25) * self::TICRATE; while ($this->accumulator >= 1) { $this->accumulator -= 1; if (!$this->runTic()) { return; } } } /** Frames are faster than tics (60 / 35): a short press must not be lost between two tics. */ private function latchInput(): void { $a = $this->actions(); if ($a->isPressed('map')) { $this->game->automap->active = !$this->game->automap->active; } if ($this->game->automap->active) { if ($a->isHeld('zoom_in')) { $this->game->automap->zoom(1.03); } if ($a->isHeld('zoom_out')) { $this->game->automap->zoom(1 / 1.03); } } // the mouse turns the view (not on touch screens: the touches move it) $input = $this->engine->input; $mx = $input->getMouse()->x; if ($input->getTouch()->active) { $this->game->touchSeen = true; } if ($this->game->touchSeen || $this->game->mouseSpeed <= 0) { $this->mouseX = null; } else { if ($this->mouseX !== null) { $this->mouseTurn -= ($mx - $this->mouseX) * $this->game->mouseSpeed; } $this->mouseX = $mx; } $this->latchUse = $this->latchUse || $a->isHeld('use') || $a->isPressed('use'); $this->latchFire = $this->latchFire || $a->isHeld('fire') || $a->isPressed('fire'); for ($i = 1; $i <= 7; $i++) { if ($a->isPressed("weapon$i")) { $this->latchWeapon = $i - 1; } } if ($a->isPressed('next_weapon')) { $this->latchWeapon = $this->nextOwnedWeapon($this->game->world->player); } } /** One tic of the game; false when the scene changed. */ public function runTic(): bool { $g = $this->game; $world = $g->world; $player = $world->player; if ($this->demo) { $this->botCmd($player); } else { $this->buildTicCmd($player); } $world->tick(); $g->statusBar->tick($world); $g->playSounds(); if ($player->playerState === Player::REBORN) { // single player: back to the start of the level, with a pistol $this->startLevel(); return true; } if ($world->exit !== null) { if ($this->demo) { $this->startLevel(); return true; } $this->completeLevel($world->exit === 'secret'); return false; } return true; } /** G_DoCompleted: the stats screen, then the next level (or the end of the episode). */ private function completeLevel(bool $secret): void { $world = $this->game->world; $p = $world->player; $p->finishLevel(); [$nextMap, $finale, $text] = $this->game->commercial ? self::nextMapPhase2($this->map, $secret) : [...self::nextMap($this->episode, $this->map, $secret), $this->map === 8 ? "E{$this->episode}TEXT" : null]; $this->scenes()->switchTo(IntermissionScene::class, [ 'episode' => $this->episode, 'map' => $this->map, 'next' => $nextMap, 'finale' => $finale, 'text' => $text, 'kills' => $p->killCount, 'totalKills' => $world->totalKills, 'items' => $p->itemCount, 'totalItems' => $world->totalItems, 'secrets' => $p->secretCount, 'totalSecrets' => $world->totalSecret, 'time' => intdiv($world->levelTime, self::TICRATE), ]); } /** * The map after this one: secret exits go to map 9, map 9 returns to the map after the secret exit, * map 8 ends the episode. @return array{0:int,1:bool} [next map, end of the episode] */ public static function nextMap(int $episode, int $map, bool $secret): array { if ($secret) { return [9, false]; } if ($map === 9) { return [[1 => 4, 2 => 6, 3 => 7, 4 => 3][$episode] ?? 1, false]; } if ($map === 8) { return [1, true]; } return [$map + 1, false]; } /** * Phase 2 (G_DoCompleted, G_WorldDone of the commercial game): MAP15 secret to 31, 31 secret to 32, back to 16; * a story text after MAP06, 11, 20, 30 and the secret levels; MAP30 ends the game. * * @return array{0:int,1:bool,2:?string} [next map, end of the game, the text to show first] */ public static function nextMapPhase2(int $map, bool $secret): array { if ($secret && $map === 15) { return [31, false, 'C5TEXT']; } if ($secret && $map === 31) { return [32, false, 'C6TEXT']; } if ($map === 31 || $map === 32) { return [16, false, null]; } $text = [6 => 'C1TEXT', 11 => 'C2TEXT', 20 => 'C3TEXT', 30 => 'C4TEXT'][$map] ?? null; return [$map + 1, $map === 30, $text]; } /** G_BuildTiccmd: the keys of this tic. */ private function buildTicCmd(Player $player): void { $a = $this->actions(); $cmd = $player->cmd; $cmd->clear(); $run = $this->game->alwaysRun xor $a->isHeld('run'); $speed = $run ? 1 : 0; $turning = $a->isHeld('left') || $a->isHeld('right'); $this->turnHeld = $turning ? $this->turnHeld + 1 : 0; $tspeed = $this->turnHeld < self::SLOWTURNTICS ? 0 : ($speed ? 2 : 1); if ($a->isHeld('right')) { $cmd->angleTurn -= self::ANGLETURN[$tspeed]; } if ($a->isHeld('left')) { $cmd->angleTurn += self::ANGLETURN[$tspeed]; } $cmd->angleTurn += $this->mouseTurn; $this->mouseTurn = 0; if ($a->isHeld('up')) { $cmd->forwardMove += self::FORWARDMOVE[$speed]; } if ($a->isHeld('down')) { $cmd->forwardMove -= self::FORWARDMOVE[$speed]; } if ($a->isHeld('strafe_right')) { $cmd->sideMove += self::SIDEMOVE[$speed]; } if ($a->isHeld('strafe_left')) { $cmd->sideMove -= self::SIDEMOVE[$speed]; } $cmd->attack = $this->latchFire; $cmd->use = $this->latchUse; $cmd->changeWeapon = $this->latchWeapon; $this->latchFire = $a->isHeld('fire'); $this->latchUse = $a->isHeld('use'); $this->latchWeapon = -1; } private function nextOwnedWeapon(Player $p): int { for ($i = 1; $i <= 7; $i++) { $w = ($p->readyWeapon + $i) % 7; if ($p->weaponOwned[$w]) { return $w; } } return $p->readyWeapon; } /** The self-playing demo (smoke test): walks, turns when stuck, shoots, opens doors. */ private function botCmd(Player $player): void { $cmd = $player->cmd; $cmd->clear(); $mo = $player->mo; $t = $this->game->world->levelTime; if ($player->playerState === Player::DEAD) { $cmd->use = ($t % 35) === 0; return; } $moved = abs($mo->x - $this->botX) + abs($mo->y - $this->botY); $this->botX = $mo->x; $this->botY = $mo->y; if ($this->botTurn > 0) { $this->botTurn--; $cmd->angleTurn = self::ANGLETURN[1]; } elseif ($moved < 1 && $t > 5) { $this->botTurn = 8 + ($t % 13); } $cmd->forwardMove = self::FORWARDMOVE[1]; $cmd->attack = ($t % 70) < 10; $cmd->use = ($t % 15) === 0; } public function render(Renderer $r): void { $g = $this->game; $p = $g->world->player; $mo = $p->mo; if ($mo === null) { return; } $view = $g->view; $view->extraLight = $p->extraLight; $view->fixedColormap = $p->fixedColormap > 0 ? $p->fixedColormap : -1; $view->psprLight = max(0, min(15, ($mo->subsector->sector->lightLevel >> 4) + $p->extraLight)); $psprites = []; foreach ($p->psprites as $psp) { if ($psp->state >= 0) { $psprites[] = [$psp->state, $psp->sx, $psp->sy, false]; } } $inv = $p->powers[Player::PW_INVISIBILITY]; if ($g->automap->active) { $g->automap->draw($g->fb, $g->world, 200 - StatusBar::HEIGHT, $g->levelName($this->episode, $this->map), $g->gfx); } else { $view->render($mo->x, $mo->y, $p->viewZ, $mo->angle, $psprites, $inv > 4 * 32 || ($inv & 8)); } $g->statusBar->draw($g->fb, $p); $g->statusBar->drawMessage($g->fb); $g->present($r, $this->sdl(), StatusBar::palette($p)); } }