/**
 * Проба плавности камеры: отправляем героя на запад и сэмплируем
 * worldRoot.position каждые 100 мс — шаги должны быть монотонными и плавными.
 * Запуск: node tools/cam-probe.mjs [url]
 */
import puppeteer from 'puppeteer-core';

const url = process.argv[2] ?? 'http://localhost:5201/';
const browser = await puppeteer.launch({
    executablePath: '/usr/bin/chromium',
    headless: true,
    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('pageerror', (err) => console.log(`[ошибка страницы] ${err.message}`));

const booted = new Promise((resolve) => {
    page.on('console', (msg) => {
        if (msg.text().includes('[location]')) resolve();
    });
});

await page.goto(url, { waitUntil: 'networkidle2', timeout: 20000 });
await new Promise((r) => setTimeout(r, 2000));
await page.mouse.click(480, 283);
await booted;
await new Promise((r) => setTimeout(r, 1500));

await page.evaluate(() => {
    const sc = window.__game.scenes.current;
    const t = sc.player.currentTile();
    const p = sc.tileCenter(t.x - 8, t.y);
    const canvas = document.querySelector('canvas');
    const rect = canvas.getBoundingClientRect();
    const wr = window.__game.renderer.worldRoot.position;
    window.__pt = { x: rect.left + ((p.x + wr.x) / 480) * rect.width,
                    y: rect.top + ((p.y + wr.y) / 270) * rect.height };
});
await page.mouse.click(await page.evaluate(() => window.__pt.x),
                       await page.evaluate(() => window.__pt.y));

for (let i = 0; i < 12; i++) {
    const p = await page.evaluate(() => {
        const wr = window.__game.renderer.worldRoot.position;
        return `${wr.x.toFixed(1)},${wr.y.toFixed(1)}`;
    });
    console.log(`${String(i * 100).padStart(4)}мс мир: ${p}`);
    await new Promise((r) => setTimeout(r, 100));
}
await browser.close();