"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _Player = require("../entities/Player");
var _GameScene = require("./GameScene");
var _EventBus = require("../EventBus");
var _Sky = require("../entities/Sky");
var _Ground = require("../entities/Ground");
var _Cog = require("../entities/Cog");
var _Star = require("../entities/Star");
var _ShinyStar = require("../entities/ShinyStar");
var _highScoreStore = require("../../utils/high-score-store");
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 PlayScene extends _GameScene.GameScene {
  constructor() {
    super("PlayScene");
    _defineProperty(this, "player", null);
    _defineProperty(this, "sky", null);
    _defineProperty(this, "ground", null);
    _defineProperty(this, "obstacles", null);
    _defineProperty(this, "stars", null);
    _defineProperty(this, "shinyStars", null);
    _defineProperty(this, "cogFactory", new _Cog.CogFactory(this));
    _defineProperty(this, "starFactory", new _Star.StarFactory(this));
    _defineProperty(this, "shinyStarFactory", new _ShinyStar.ShinyStarFactory(this));
    _defineProperty(this, "score", 0);
    _defineProperty(this, "collectedStars", 0);
    _defineProperty(this, "scoreInterval", 100);
    _defineProperty(this, "scoreDeltaTime", 0);
    _defineProperty(this, "cogSpawnInterval", 3000);
    _defineProperty(this, "cogSpawnTime", 0);
    _defineProperty(this, "starSpawnInterval", 5000);
    _defineProperty(this, "starSpawnTime", 0);
    _defineProperty(this, "starShinyStarInterval", 30000);
    _defineProperty(this, "starShinyStarTime", 0);
    _defineProperty(this, "gameSpeed", 5);
    _defineProperty(this, "gameSpeedModifier", 1);
    _defineProperty(this, "isGameOverVisible", false);
    _defineProperty(this, "isPlayButtonVisible", true);
  }
  create() {
    var _this$input$keyboard;
    this.cursors = (_this$input$keyboard = this.input.keyboard) === null || _this$input$keyboard === void 0 ? void 0 : _this$input$keyboard.createCursorKeys();
    this.createSky();
    this.createPlayer();
    this.createGround();
    this.handleGroundPlayerCollision();
    this.createObstacles();
    this.handleObstacleCollisions();
    this.createStars();
    this.handleStarCollisions();

    // this.createShinyStars();
    // this.handleShinyStarCollisions();

    this.createPlayButtonContainer();
    this.createGameoverContainer();
    this.createScore();
    this.handleGameRestartSetup();
    this.progressSound = this.sound.add("progress", {
      volume: 0.2
    });
    this.progressSoundShiny = this.sound.add("progress", {
      volume: 0.5
    });
    _EventBus.EventBus.emit('current-scene-ready', this);
  }
  handleGroundPlayerCollision() {
    if (!this.ground) return;
    if (!this.player) return;
    this.physics.add.collider(this.ground, this.player, () => {});
  }
  update(time, delta) {
    var _this$obstacles, _this$stars, _this$shinyStars, _this$scoreText, _this$obstacles2, _this$stars2, _this$shinyStars2;
    if (!this.isGameRunning) return;
    _EventBus.EventBus.emit('play-scene-updated', this);
    this.cogSpawnTime += delta;
    this.starSpawnTime += delta;
    this.starShinyStarTime += delta;
    this.scoreDeltaTime += delta;
    if (this.scoreDeltaTime >= this.scoreInterval) {
      this.score++;
      this.scoreDeltaTime = 0;
      if (this.score % 100 === 0) {
        this.gameSpeedModifier += 0.2;
        this.tweens.add({
          targets: this.scoreText,
          duration: 100,
          repeat: 3,
          alpha: 0,
          yoyo: true
        });
      }
    }
    if (this.cogSpawnTime >= this.cogSpawnInterval) {
      this.spawnObstacle();
      this.cogSpawnTime = 0;
    }
    if (this.starSpawnTime >= this.starSpawnInterval) {
      this.spawnStar();
      this.starSpawnTime = 0;
    }
    if (this.starShinyStarTime >= this.starShinyStarInterval) {
      this.spawnShinyStar();
      this.starShinyStarTime = 0;
    }
    Phaser.Actions.IncX(((_this$obstacles = this.obstacles) === null || _this$obstacles === void 0 ? void 0 : _this$obstacles.getChildren()) || [], -this.gameSpeed * this.gameSpeedModifier);
    Phaser.Actions.IncX(((_this$stars = this.stars) === null || _this$stars === void 0 ? void 0 : _this$stars.getChildren()) || [], -this.gameSpeed * this.gameSpeedModifier);
    Phaser.Actions.IncX(((_this$shinyStars = this.shinyStars) === null || _this$shinyStars === void 0 ? void 0 : _this$shinyStars.getChildren()) || [], -this.gameSpeed * this.gameSpeedModifier);
    const score = Array.from(String(this.score), Number);
    for (let i = 0; i < 5 - String(this.score).length; i++) {
      score.unshift(0);
    }
    (_this$scoreText = this.scoreText) === null || _this$scoreText === void 0 || _this$scoreText.setText(score.join(""));
    (_this$obstacles2 = this.obstacles) === null || _this$obstacles2 === void 0 || _this$obstacles2.getChildren().forEach(obstacle => {
      if (obstacle.getBounds().right < 0) {
        var _this$obstacles3;
        (_this$obstacles3 = this.obstacles) === null || _this$obstacles3 === void 0 || _this$obstacles3.remove(obstacle);
      }
    });
    (_this$stars2 = this.stars) === null || _this$stars2 === void 0 || _this$stars2.getChildren().forEach(star => {
      if (star.getBounds().right < 0) {
        var _this$stars3;
        (_this$stars3 = this.stars) === null || _this$stars3 === void 0 || _this$stars3.remove(star);
      }
    });
    (_this$shinyStars2 = this.shinyStars) === null || _this$shinyStars2 === void 0 || _this$shinyStars2.getChildren().forEach(shinyStar => {
      if (shinyStar.getBounds().right < 0) {
        var _this$shinyStars3;
        (_this$shinyStars3 = this.shinyStars) === null || _this$shinyStars3 === void 0 || _this$shinyStars3.remove(shinyStar);
      }
    });
  }
  createPlayer() {
    this.player = new _Player.Player(this);
  }
  createSky() {
    this.sky = new _Sky.Sky(this);
  }
  createGround() {
    this.ground = new _Ground.Ground(this);
  }
  createObstacles() {
    this.obstacles = this.physics.add.group();
  }
  createStars() {
    this.stars = this.physics.add.group();
  }
  createShinyStars() {
    this.shinyStars = this.physics.add.group();
  }
  createPlayButtonContainer() {
    var _this$sky;
    this.playButtonText = this.add.image(0, 0, "play").setInteractive();

    // Use the scale factor from the Sky class to maintain consistency
    const scaleFactor = ((_this$sky = this.sky) === null || _this$sky === void 0 ? void 0 : _this$sky.scaleFactors.height) || 1;
    const width = 185;
    const height = 185;

    // center the play button to the center of the game taking into account the scale factor

    this.playButtonText.setPosition(0, 80);
    console.log("this.scale", this.scale);
    console.log("this.playButtonText.width, this.playButtonText.height", this.playButtonText.width, this.playButtonText.height);
    console.log("this.scale.width", this.scale.width);
    console.log("this.scale.height", this.scale.height);
    console.log("(this.scene.scale.width / 2, this.scene.scale.height / 2)", this.scale.width / 2, this.scale.height / 2);

    // Apply the scale factor to the player
    this.playButtonText.setDisplaySize(width * scaleFactor, height * scaleFactor);

    // Add shadow to the play button
    const shadow = this.add.image(0, 0, "play").setTint(0x000000).setAlpha(0.5);
    shadow.setPosition(this.playButtonText.x + 5, this.playButtonText.y + 5);
    shadow.setDisplaySize(width * scaleFactor, height * scaleFactor);
    this.playButtonContainer = this.add.container(this.scale.width / 2, this.scale.height / 4).add([shadow, this.playButtonText]).setAlpha(1).setDepth(3);

    // Add continuous breathing animation
    this.tweens.add({
      targets: this.playButtonContainer,
      scaleX: 1.2,
      scaleY: 1.2,
      duration: 1300,
      yoyo: true,
      ease: 'Power1',
      repeat: -1 // This makes the animation loop indefinitely
    });
  }
  createGameoverContainer() {
    this.gameOverText = this.add.image(0, 0, "game-over");
    this.restartText = this.add.image(0, 80, "restart").setInteractive();
    this.gameOverContainer = this.add.container(this.scale.width / 2, this.scale.height / 4).add([this.gameOverText, this.restartText]).setAlpha(0).setDepth(3);
  }
  createScore() {
    this.scoreText = this.add.text(this.scale.width, 0, "00000", {
      fontSize: 30,
      fontFamily: "Arial",
      color: "#535353",
      resolution: 5
    }).setOrigin(1, 0).setAlpha(1);
    this.highScoreText = this.add.text(this.scoreText.getBounds().left - 20, 0, "00000", {
      fontSize: 30,
      fontFamily: "Arial",
      color: "#535353",
      resolution: 5
    }).setOrigin(1, 0).setAlpha(1);
  }
  spawnObstacle() {
    if (!this.obstacles) return;
    this.cogFactory.create(this.obstacles);
  }
  spawnStar() {
    if (!this.stars) return;
    this.starFactory.create(this.stars);
  }
  spawnShinyStar() {
    if (!this.shinyStars) return;
    this.shinyStarFactory.create(this.shinyStars);
  }
  handleGameStart() {
    const rollOutEvent = this.time.addEvent({
      delay: 1000 / 60,
      loop: true,
      callback: () => {
        var _this$player, _this$player2;
        (_this$player = this.player) === null || _this$player === void 0 || _this$player.setVelocityX(80);
        rollOutEvent.remove();
        if (this.ground) this.ground.width = this.gameWidth;
        (_this$player2 = this.player) === null || _this$player2 === void 0 || _this$player2.setVelocityX(0);
        // this.scoreText.setAlpha(1);
        this.isGameRunning = true;
      }
    });
  }
  handleGameRestartSetup() {
    var _this$restartText, _this$playButtonText;
    (_this$restartText = this.restartText) === null || _this$restartText === void 0 || _this$restartText.on("pointerdown", () => {
      this.handleGameRestart();
    });
    (_this$playButtonText = this.playButtonText) === null || _this$playButtonText === void 0 || _this$playButtonText.on("pointerdown", () => {
      this.handleGameRestart();
    });
  }
  handleGameRestart() {
    var _this$player3, _this$obstacles4, _this$stars4, _this$shinyStars4, _this$gameOverContain, _this$playButtonConta, _this$player4, _this$player5;
    this.physics.resume();
    (_this$player3 = this.player) === null || _this$player3 === void 0 || _this$player3.setVelocityY(0);
    (_this$obstacles4 = this.obstacles) === null || _this$obstacles4 === void 0 || _this$obstacles4.clear(true, true);
    (_this$stars4 = this.stars) === null || _this$stars4 === void 0 || _this$stars4.clear(true, true);
    (_this$shinyStars4 = this.shinyStars) === null || _this$shinyStars4 === void 0 || _this$shinyStars4.clear(true, true);
    this.collectedStars = 0;
    this.score = 0;
    (_this$gameOverContain = this.gameOverContainer) === null || _this$gameOverContain === void 0 || _this$gameOverContain.setAlpha(0);
    (_this$playButtonConta = this.playButtonContainer) === null || _this$playButtonConta === void 0 || _this$playButtonConta.setAlpha(0);
    this.isGameOverVisible = false;
    this.isPlayButtonVisible = false;
    this.anims.resumeAll();
    this.isGameRunning = true;
    (_this$player4 = this.player) === null || _this$player4 === void 0 || _this$player4.setTexture("marty-waist");
    (_this$player5 = this.player) === null || _this$player5 === void 0 || _this$player5.positionOnSky();
  }
  handleObstacleCollisions() {
    if (!this.obstacles) return;
    if (!this.player) return;
    this.physics.add.collider(this.obstacles, this.player, () => {
      var _this$player6, _this$gameOverContain2, _this$highScoreText, _this$scoreText2, _this$scoreText3, _this$highScoreText2, _this$highScoreText3;
      this.isGameRunning = false;
      this.physics.pause();
      this.anims.pauseAll();
      (_this$player6 = this.player) === null || _this$player6 === void 0 || _this$player6.die();
      (_this$gameOverContain2 = this.gameOverContainer) === null || _this$gameOverContain2 === void 0 || _this$gameOverContain2.setAlpha(1);
      this.isGameOverVisible = true;
      const newHighScore = (_this$highScoreText = this.highScoreText) === null || _this$highScoreText === void 0 ? void 0 : _this$highScoreText.text.substring(this.highScoreText.text.length - 5);
      const newScore = Number((_this$scoreText2 = this.scoreText) === null || _this$scoreText2 === void 0 ? void 0 : _this$scoreText2.text) > Number(newHighScore) ? (_this$scoreText3 = this.scoreText) === null || _this$scoreText3 === void 0 ? void 0 : _this$scoreText3.text : newHighScore;
      (0, _highScoreStore.updateHighScore)(Number(newScore));
      (_this$highScoreText2 = this.highScoreText) === null || _this$highScoreText2 === void 0 || _this$highScoreText2.setText("HI " + newScore);
      (_this$highScoreText3 = this.highScoreText) === null || _this$highScoreText3 === void 0 || _this$highScoreText3.setAlpha(1);
      this.cogSpawnTime = 0;
      this.score = 0;
      this.collectedStars = 0;
      this.scoreDeltaTime = 0;
      this.gameSpeedModifier = 1;
      _EventBus.EventBus.emit('die', this);
    });
  }
  handleStarCollisions() {
    if (!this.stars) return;
    if (!this.player) return;
    this.physics.add.overlap(this.stars, this.player, (player, star) => {
      var _this$stars5, _this$progressSound;
      (_this$stars5 = this.stars) === null || _this$stars5 === void 0 || _this$stars5.remove(star);
      star.destroy();
      // this.score += 40;
      this.collectedStars++;
      _EventBus.EventBus.emit('star-collected', this);
      if (this.collectedStars === 10) {
        var _this$playButtonConta2;
        _EventBus.EventBus.emit('target-achieved', this);
        this.isGameRunning = false;
        (_this$playButtonConta2 = this.playButtonContainer) === null || _this$playButtonConta2 === void 0 || _this$playButtonConta2.setAlpha(1);
        this.isPlayButtonVisible = true;
        this.physics.pause();
        this.anims.pauseAll();
      }
      (_this$progressSound = this.progressSound) === null || _this$progressSound === void 0 || _this$progressSound.play();
    });
  }
  handleShinyStarCollisions() {
    if (!this.shinyStars) return;
    if (!this.player) return;
    this.physics.add.overlap(this.shinyStars, this.player, (player, star) => {
      var _this$shinyStars5, _this$progressSoundSh;
      (_this$shinyStars5 = this.shinyStars) === null || _this$shinyStars5 === void 0 || _this$shinyStars5.remove(star);
      star.destroy();
      // this.score += 50;
      this.collectedStars++;
      (_this$progressSoundSh = this.progressSoundShiny) === null || _this$progressSoundSh === void 0 || _this$progressSoundSh.play();
    });
  }
}
var _default = exports.default = PlayScene;