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 = 1000;
        const height = 1200;
        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);
    }
    init() {
        this
            .setOrigin(0, 0)
            .setDepth(1);
        this.scaleToFit();
        EventBus.emit('mountain-ready', this);
        // Handle resizing
        this.scene.scale.once('resize', this.scaleToFit, this);
    }
    update() {
        if (!this.scene.isGameRunning)
            return;
        this.tilePositionY += (this.scene.gameSpeed * this.scene.gameSpeedModifier) / this.scaleFactors.height * .5;
    }
    scaleToFit() {
        // console.log("Mountain scaleToFit triggered");
        const gameWidth = this.scene.scale.width;
        const gameHeight = this.scene.scale.height;
        const bgWidth = 1000; // Actual width of the background image
        const bgHeight = 1200; // Actual height of the background image
        // Calculate aspect ratio for the background
        const bgAspectRatio = bgWidth / bgHeight;
        const screenAspectRatio = gameWidth / gameHeight;
        let scaleFactor;
        // Calculate scale factor based on the height or width
        if (screenAspectRatio > bgAspectRatio) {
            scaleFactor = gameHeight / bgHeight;
        }
        else {
            scaleFactor = gameWidth / bgWidth;
        }
        // Set the background to the new scaled size
        this.setDisplaySize(bgWidth * scaleFactor, bgHeight * scaleFactor);
        this.scaleFactors = { width: scaleFactor, height: scaleFactor };
    }
}
