"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Mountain = void 0;
var _EventBus = require("../EventBus");
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
class Mountain extends Phaser.GameObjects.TileSprite {
  constructor(scene) {
    const x = 0;
    const y = 0;
    const width = 2500;
    const height = 2003;
    super(scene, x, y, width, height, "mountain");
    _defineProperty(this, "scaleFactors", {
      width: 0,
      height: 0
    });
    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.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;
  }
  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

    // 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
    };
  }
}
exports.Mountain = Mountain;