export class Ground extends Phaser.GameObjects.TileSprite {
    scene;
    heightScaled = 0;
    scaleFactor = 0;
    constructor(scene) {
        const x = 0;
        const y = scene.gameHeight;
        const width = scene.gameWidth;
        const height = 24;
        super(scene, x, y, width, height, "ground");
        this.scene = scene;
        scene.add.existing(this);
        scene.physics.add.existing(this, false);
        // make ground immovable
        this.body.setImmovable(true);
        this.init();
        this.once(Phaser.GameObjects.Events.DESTROY, this.cleanup, this);
    }
    init() {
        this
            .setOrigin(0, 0)
            .setDepth(2);
        this.scaleToFit();
        // Handle resizing
        this.scene.scale.on('resize', this.scaleToFit, this);
        this.scene.scale.on('resize', this.positionOnSky, this);
        this.scaleToFit();
        this.positionOnSky();
    }
    positionOnSky() {
        this.setY(this.scene.scale.height - this.displayHeight);
    }
    scaleToFit() {
        // Use the scale factor from the Sky class to maintain consistency
        const scaleFactor = this.scene.sky?.scaleFactors.height || 1;
        const height = this.height * scaleFactor;
        const width = this.scene.scale.width;
        this.setSize(width, this.height);
        this.setDisplaySize(width, height);
        this.body.setSize(width, height);
    }
    cleanup() {
        this.scene?.scale.off('resize', this.scaleToFit, this);
        this.scene?.scale.off('resize', this.positionOnSky, this);
    }
}
