import { Graphics } from './Graphics.js';
import { World } from './World.js';
import { Player } from './Player.js';

export class Game {
    constructor() {
        this.graphics = new Graphics();
        this.world = new World(this.graphics.scene);
        this.player = new Player(this.graphics.camera, this.world.colliders);

        this.isRunning = false;
        this.lastTime = 0;
        this.setupUI();
    }

    setupUI() {
        const startScreen = document.getElementById('start-screen');
        const hud = document.getElementById('hud');

        if (startScreen) {
            startScreen.addEventListener('click', () => {
                if (this.player) this.player.lockControls();
                startScreen.style.display = 'none';
                if (hud) hud.style.display = 'block';
                this.isRunning = true;
            });
        }
    }

    start() {
        this.world.load();
        this.graphics.scene.add(this.player.getObject());
        requestAnimationFrame(this.loop.bind(this));
    }

    loop(time) {
        const dt = this.lastTime === 0 ? 0 : Math.min((time - this.lastTime) / 1000, 0.1);
        this.lastTime = time;

        if (this.isRunning) {
            this.player.update(dt);
        }

        this.graphics.render();
        requestAnimationFrame(this.loop.bind(this));
    }
}
