KO
games/dmmotor / tests / PlayTest.php · commit 9d248a4
<?phpnamespace Tests;use App\Dm\Info;use App\Dm\Line;use App\Dm\Play\Mobj;use App\Dm\Play\World;use App\Scene\IntermissionScene;use App\Scene\PlayScene;use App\Service\DmGame;use JumPHP\Testing\GameTestCase;/** * Game tests: they play the first level of Freedoom Phase 1 (E1M1) and check what happens. */final class PlayTest extends GameTestCase{    private function world(): World    {        /** @var DmGame $game */        $game = $this->game->peek('game');        return $game->world;    }    private function startE1M1(): World    {        $this->game->start(PlayScene::class, ['iwad' => 'freedoom1', 'episode' => 1, 'map' => 1, 'skill' => 2, 'newGame' => true]);        return $this->world();    }    /** Puts the player 40 units in front of a line, facing it; false when he does not fit there. */    private function standBefore(World $w, Line $line): bool    {        $mo = $w->player->mo;        $len = hypot($line->dx, $line->dy);        $nx = $line->dy / $len;        $ny = -$line->dx / $len;        $x = ($line->v1->x + $line->v2->x) / 2 + $nx * 40;        $y = ($line->v1->y + $line->v2->y) / 2 + $ny * 40;        if (!$w->teleportMove($mo, $x, $y)) {            return false;        }        $mo->z = $mo->floorZ;        $mo->momX = $mo->momY = 0;        $mo->angle = atan2(-$ny, -$nx);        return true;    }    public function testTheLevelStartsWithThePlayerAndItsMonsters(): void    {        $w = $this->startE1M1();        self::assertNotNull($w->player->mo);        self::assertSame(100, $w->player->health);        self::assertGreaterThan(0, $w->totalKills);        $this->game->run(30);        $this->game->shot('start');    }    public function testThePlayerWalksForwardWhenUpIsHeld(): void    {        $w = $this->startE1M1();        $mo = $w->player->mo;        $x = $mo->x;        $y = $mo->y;        $this->game->hold('up', 30);        self::assertGreaterThan(40, hypot($mo->x - $x, $mo->y - $y));        self::assertEquals($mo->floorZ, $mo->z);    }    public function testThePlayerTurnsWithTheArrows(): void    {        $w = $this->startE1M1();        $angle = $w->player->mo->angle;        $this->game->hold('left', 20);        self::assertGreaterThan(0.3, World::normAngle($w->player->mo->angle - $angle));    }    public function testTheWallsStopThePlayer(): void    {        $w = $this->startE1M1();        $mo = $w->player->mo;        $this->game->hold('up', 600);   // ten seconds against whatever is in front        $sub = $w->map->pointInSubsector($mo->x, $mo->y);        self::assertSame($sub, $mo->subsector);        self::assertTrue($w->checkPosition($mo, $mo->x, $mo->y), 'the player is not stuck inside a wall');    }    public function testThePistolKillsAZombieInFront(): void    {        $w = $this->startE1M1();        $mo = $w->player->mo;        $zombie = $w->spawnMobj($mo->x + 96 * cos($mo->angle), $mo->y + 96 * sin($mo->angle), World::ONFLOORZ, Info::MT_POSSESSED);        $w->totalKills++;        $ammo = $w->player->ammo[0];        $this->game->hold('fire', 240);        self::assertLessThanOrEqual(0, $zombie->health, 'the zombie is dead');        self::assertSame(1, $w->player->killCount);        self::assertLessThan($ammo, $w->player->ammo[0], 'bullets were used');        $this->game->shot('zombie');    }    public function testUsingADoorOpensIt(): void    {        $w = $this->startE1M1();        foreach ($w->map->lines as $line) {            if ($line->special !== 1 || $line->backSector === null || !$this->standBefore($w, $line)) {                continue;            }            $door = $line->backSector;            $closed = $door->ceilingHeight;            $this->game->run(5);            $this->game->press('use', 70);            self::assertGreaterThan($closed + 32, $door->ceilingHeight, 'the door went up');            $this->game->shot('door');            return;        }        self::fail('no door to try in E1M1');    }    public function testAMedikitHealsThePlayer(): void    {        $w = $this->startE1M1();        $p = $w->player;        $p->health = $p->mo->health = 40;        $mo = $p->mo;        $w->spawnMobj($mo->x + 48 * cos($mo->angle), $mo->y + 48 * sin($mo->angle), World::ONFLOORZ, Info::MT_MISC11);        $this->game->hold('up', 20);        self::assertSame(65, $p->health);    }    public function testTheExitSwitchEndsTheLevel(): void    {        $w = $this->startE1M1();        foreach ($w->map->lines as $line) {            if ($line->special !== 11 || !$this->standBefore($w, $line)) {                continue;            }            $this->game->run(5);            $this->game->press('use', 90);            self::assertInstanceOf(IntermissionScene::class, $this->game->scene());            $this->game->run(200);            $this->game->shot('intermission');            return;        }        self::fail('no exit switch to try in E1M1');    }    public function testTheSecretExitAndMapNineFollowTheOriginalOrder(): void    {        self::assertSame([2, false], PlayScene::nextMap(1, 1, false));        self::assertSame([9, false], PlayScene::nextMap(1, 3, true));        self::assertSame([4, false], PlayScene::nextMap(1, 9, false));        self::assertSame([1, true], PlayScene::nextMap(2, 8, false));    }    public function testAnImpFireballHurtsThePlayer(): void    {        $w = $this->startE1M1();        $mo = $w->player->mo;        $imp = $w->spawnMobj($mo->x + 200 * cos($mo->angle), $mo->y + 200 * sin($mo->angle), World::ONFLOORZ, Info::MT_TROOP);        $imp->target = $mo;        $imp->angle = $mo->angle + M_PI;        $w->setMobjState($imp, Info::S_TROO_ATK1);        $this->game->run(120);        self::assertLessThan(100, $w->player->health);        self::assertInstanceOf(Mobj::class, $imp);    }}