import { EventBus } from "../EventBus";
export class Mountain extends Phaser.GameObjects.TileSprite {
    scene;
    scaleFactors = { width: 0, height: 0 };
    constructor(scene) {
        const x = 0;
        const y = 0;
        const width = 2500;
        const height = 2003;
        super(scene, x, y, width, height, "mountain");
        this.scene = scene;
        scene.add.existing(this);
        // scene.physics.add.existing(this);
        this.init();
        this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
        this.once(Phaser.GameObjects.Events.DESTROY, this.cleanup, this);
    }
    init() {
        this
            .setOrigin(0, 0)
            .setDepth(1);
        this.scaleToFit();
        EventBus.emit('mountain-ready', this);
        // Handle resizing
        this.scene.scale.on('resize', this.scaleToFit, this);
    }
    update(time, delta) {
        if (!this.scene.isGameRunning)
            return;
        const frameScale = delta / (1000 / 60);
        this.tilePositionY += (this.scene.gameSpeed * this.scene.gameSpeedModifier * frameScale) / this.scaleFactors.height;
    }
    scaleToFit() {
        // console.log("Mountain scaleToFit triggered");
        const gameWidth = this.scene.scale.width;
        const gameHeight = this.scene.scale.height;
        const bgWidth = 2500; // Actual width of the background image
        const bgHeight = 2003; // Actual height of the background image
        const scaleFactor = Math.max(gameWidth / bgWidth, gameHeight / bgHeight);
        // Set the background to the new scaled size
        this.setDisplaySize(bgWidth * scaleFactor, bgHeight * scaleFactor);
        this.scaleFactors = { width: scaleFactor, height: scaleFactor };
    }
    cleanup() {
        this.scene?.events.off(Phaser.Scenes.Events.UPDATE, this.update, this);
        this.scene?.scale.off('resize', this.scaleToFit, this);
    }
}
