import Phaser from 'phaser';
import { GameScene } from './scenes/GameScene.js';
import { PreloadScene } from './scenes/PreloadScene.js';
import { MenuScene } from './scenes/MenuScene.js';

// 获取设备的像素密度
const pixelRatio = window.devicePixelRatio || 1;

// 基础分辨率
const baseWidth = 375;
const baseHeight = 667;

// 高分辨率适配
const gameWidth = baseWidth * Math.min(pixelRatio, 2);
const gameHeight = baseHeight * Math.min(pixelRatio, 2);

const config = {
    type: Phaser.AUTO,
    width: gameWidth,
    height: gameHeight,
    parent: 'game-container',
    backgroundColor: '#2c3e50',
    scale: {
        mode: Phaser.Scale.FIT,
        autoCenter: Phaser.Scale.CENTER_BOTH,
        width: gameWidth,
        height: gameHeight,
        // 高分辨率渲染配置
        zoom: 1 / Math.min(pixelRatio, 2)
    },
    render: {
        // 启用抗锯齿
        antialias: true,
        // 像素完美渲染
        pixelArt: false,
        // 平滑图像缩放
        roundPixels: false
    },
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 0 },
            debug: false
        }
    },
    scene: [PreloadScene, MenuScene, GameScene]
};

// 移动端优化
if (typeof window !== 'undefined') {
    // 防止双击缩放
    window.addEventListener('touchstart', function(e) {
        if (e.touches.length > 1) {
            e.preventDefault();
        }
    });
    
    let lastTouchEnd = 0;
    window.addEventListener('touchend', function(e) {
        const now = Date.now();
        if (now - lastTouchEnd <= 300) {
            e.preventDefault();
        }
        lastTouchEnd = now;
    }, false);
    
    // 防止页面滚动
    window.addEventListener('touchmove', function(e) {
        e.preventDefault();
    }, { passive: false });
    
    // 设置视口元标签以确保正确的像素密度
    const viewport = document.querySelector('meta[name="viewport"]');
    if (viewport) {
        viewport.setAttribute('content', 
            'width=device-width, initial-scale=1.0, user-scalable=no, ' +
            'shrink-to-fit=no, viewport-fit=cover'
        );
    }
}

const game = new Phaser.Game(config);

// 在控制台输出分辨率信息（调试用）
console.log(`设备像素密度: ${pixelRatio}`);
console.log(`游戏分辨率: ${gameWidth}x${gameHeight}`);
console.log(`缩放比例: ${1 / Math.min(pixelRatio, 2)}`);

export default game; 