Newer
Older
rpg / tools / smoke-zvenets.mjs
/**
 * Смоук перехода луга -> Звенец: герой идёт кликами на восток карты,
 * где триггер (26,14). Запуск: node tools/smoke-zvenets.mjs [url] [скриншот]
 */
import puppeteer from 'puppeteer-core';

const url = process.argv[2] ?? 'http://localhost:5199/';
const shot = process.argv[3] ?? '/tmp/rpg_zvenets.png';

const browser = await puppeteer.launch({
    executablePath: '/usr/bin/chromium',
    headless: true,
    // autoplay-policy: в headless ctx.resume() без флага может не резолвиться,
    // и навигация, завязанная на звук, зависает.
    args: ['--no-sandbox', '--enable-unsafe-swiftshader', '--use-angle=swiftshader',
           '--autoplay-policy=no-user-gesture-required', '--window-size=960,540']
});
const page = await browser.newPage();
await page.setViewport({ width: 960, height: 540 });

page.on('console', (msg) => console.log(`[консоль] ${msg.text()}`));
page.on('pageerror', (err) => console.log(`[ошибка страницы] ${err.message}\n${err.stack ?? ''}`));

// Ждём маяк BootScene: без него клики уходят «в загрузку».
const booted = new Promise((resolve) => {
    page.on('console', (msg) => {
        if (msg.text().includes('[boot] ассеты загружены')) resolve();
    });
});

await page.goto(url, { waitUntil: 'networkidle2', timeout: 20000 });
await booted;
await new Promise((r) => setTimeout(r, 1500)); // fade перехода в меню

// «Новая игра» (первый пункт в свежем профиле; канвас 480x270 растянут x2)
await page.mouse.click(480, 283);
await new Promise((r) => setTimeout(r, 2500));

// Клик в тайл на 2 шага восточнее ног героя (но не дальше триггера):
// центр ромба (tx,ty) — ((tx-ty)*16, (tx+ty)*8+8) виртуальных px.
for (let i = 0; i < 20; i++) {
    const p = await page.evaluate(() => {
        const g = window.__game;
        const sc = g.scenes.current;
        const tile = sc.player.currentTile();
        const target = { x: Math.min(26, tile.x + 2), y: tile.y };
        const wx = (target.x - target.y) * 16;
        const wy = (target.x + target.y) * 8 + 8;
        const rect = document.querySelector('canvas').getBoundingClientRect();
        const wr = g.renderer.worldRoot.position;
        return {
            x: rect.left + ((wx + wr.x) / 480) * rect.width,
            y: rect.top + ((wy + wr.y) / 270) * rect.height
        };
    });
    await page.mouse.click(p.x, p.y);
    await new Promise((r) => setTimeout(r, 700));
}

// Даём fade-переходу (0.4 с) и enter() новой сцены завершиться до скриншота.
await new Promise((r) => setTimeout(r, 1500));

await page.screenshot({ path: shot });
console.log(`Скриншот: ${shot}`);
await browser.close();