"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.regexp.flags.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.for-each.js");
var _Player = require("../entities/Player");
var _GameScene = require("./GameScene");
var _EventBus = require("../EventBus");
var _Mountain = require("../entities/Mountain");
var _Tree = require("../entities/Tree");
var _Flags = require("../entities/Flags");
var _RightWall = require("../entities/RightWall");
var _PlayButton = require("../entities/PlayButton");
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, "mountain", null);
    _defineProperty(this, "rightWall", null);
    _defineProperty(this, "trees", null);
    _defineProperty(this, "flags", null);
    _defineProperty(this, "flagsBadAreas", null);
    _defineProperty(this, "treeFactory", new _Tree.TreeFactory(this));
    _defineProperty(this, "flagsFactory", new _Flags.FlagsFactory(this));
    _defineProperty(this, "score", 0);
    _defineProperty(this, "collectedFlags", 0);
    _defineProperty(this, "scoreInterval", 100);
    _defineProperty(this, "scoreDeltaTime", 0);
    _defineProperty(this, "treeSpawnInterval", 3000);
    _defineProperty(this, "treeSpawnTime", 0);
    _defineProperty(this, "flagsSpawnInterval", 5000);
    _defineProperty(this, "flagsSpawnTime", 0);
    _defineProperty(this, "gameSpeed", 2);
    _defineProperty(this, "gameSpeedModifier", 1);
    _defineProperty(this, "lastTimeFlagCollected", 0);
    _defineProperty(this, "flagCollectedInterval", 500);
  }
  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.createMountain();
    this.createPlayer();
    this.createRightWall();
    this.createTrees();
    this.handleTreeCollisions();
    this.handleRightWallCollision();
    this.createFlags();
    this.handleFlagsCollisions();

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

    this.createPlayButton();
    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);
    // this.spawnTree();
    // this.spawnFlags();

    if (this.scale.width < 600) {
      this.gameSpeed = 3;
    } else if (this.scale.width < 800) {
      this.gameSpeed = 3;
    } else if (this.scale.width < 1200) {
      this.gameSpeed = 4;
    } else if (this.scale.width < 1600) {
      this.gameSpeed = 4;
    } else if (this.scale.width < 2000) {
      this.gameSpeed = 4;
    } else if (this.scale.width < 2500) {
      this.gameSpeed = 5;
    } else {
      this.gameSpeed = 6;
    }
  }
  update(time, delta) {
    var _this$trees, _this$flags, _this$flagsBadAreas, _this$trees2, _this$flags2, _this$flagsBadAreas2;
    if (!this.isGameRunning) return;
    _EventBus.EventBus.emit('play-scene-updated', this);
    if (this.player && this.rightWall) {
      const isPlayerTouchingGround = this.physics.overlap(this.player, this.rightWall);
      this.player.canGoRight = !isPlayerTouchingGround;
    }
    this.treeSpawnTime += delta;
    this.flagsSpawnTime += 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.treeSpawnTime >= this.treeSpawnInterval) {
      this.spawnTree();
      this.treeSpawnTime = 0;
    }
    if (this.flagsSpawnTime >= this.flagsSpawnInterval) {
      this.spawnFlags();
      this.flagsSpawnTime = 0;
    }
    Phaser.Actions.IncY(((_this$trees = this.trees) === null || _this$trees === void 0 ? void 0 : _this$trees.getChildren()) || [], -this.gameSpeed * this.gameSpeedModifier);
    Phaser.Actions.IncY(((_this$flags = this.flags) === null || _this$flags === void 0 ? void 0 : _this$flags.getChildren()) || [], -this.gameSpeed * this.gameSpeedModifier);
    Phaser.Actions.IncY(((_this$flagsBadAreas = this.flagsBadAreas) === null || _this$flagsBadAreas === void 0 ? void 0 : _this$flagsBadAreas.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$trees2 = this.trees) === null || _this$trees2 === void 0 || _this$trees2.getChildren().forEach(tree => {
      if (tree.getBounds().right < 0) {
        var _this$trees3;
        (_this$trees3 = this.trees) === null || _this$trees3 === void 0 || _this$trees3.remove(tree);
      }
    });
    (_this$flags2 = this.flags) === null || _this$flags2 === void 0 || _this$flags2.getChildren().forEach(flags => {
      if (flags.getBounds().right < 0) {
        var _this$flags3;
        (_this$flags3 = this.flags) === null || _this$flags3 === void 0 || _this$flags3.remove(flags);
      }
    });
    (_this$flagsBadAreas2 = this.flagsBadAreas) === null || _this$flagsBadAreas2 === void 0 || _this$flagsBadAreas2.getChildren().forEach(badArea => {
      if (badArea.getBounds().right < 0) {
        var _this$flagsBadAreas3;
        (_this$flagsBadAreas3 = this.flagsBadAreas) === null || _this$flagsBadAreas3 === void 0 || _this$flagsBadAreas3.remove(badArea);
      }
    });
  }
  createPlayer() {
    this.player = new _Player.Player(this);
  }
  createMountain() {
    this.mountain = new _Mountain.Mountain(this);
  }
  createRightWall() {
    this.rightWall = new _RightWall.RightWall(this);
  }
  createTrees() {
    this.trees = this.physics.add.group();
  }
  createFlags() {
    this.flags = this.physics.add.group();
    this.flagsBadAreas = this.physics.add.group();
  }
  createPlayButton() {
    this.playButton = new _PlayButton.PlayButton(this);
    this.playButton_ = this.playButton.playButton;
  }
  spawnTree() {
    if (!this.trees) return;
    this.treeFactory.create(this.trees);
  }
  spawnFlags() {
    var _this$flagsBadAreas4, _this$flagsBadAreas5;
    if (!this.flags) return;
    const createdFlags = this.flagsFactory.create(this.flags);
    createdFlags.badAreaLeft && ((_this$flagsBadAreas4 = this.flagsBadAreas) === null || _this$flagsBadAreas4 === void 0 ? void 0 : _this$flagsBadAreas4.add(createdFlags.badAreaLeft));
    createdFlags.badAreaRight && ((_this$flagsBadAreas5 = this.flagsBadAreas) === null || _this$flagsBadAreas5 === void 0 ? void 0 : _this$flagsBadAreas5.add(createdFlags.badAreaRight));
  }
  handleGameStart() {
    var _this$playButton;
    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();
        (_this$player2 = this.player) === null || _this$player2 === void 0 || _this$player2.setVelocityX(0);
        // this.scoreText.setAlpha(1);
        this.isGameRunning = true;
      }
    });
    // this.playButtonContainer?.setAlpha(0);
    (_this$playButton = this.playButton) === null || _this$playButton === void 0 || _this$playButton.onGameStart();
  }
  handleGameRestartSetup() {
    var _this$restartText, _this$playButton2;
    (_this$restartText = this.restartText) === null || _this$restartText === void 0 || _this$restartText.on("pointerdown", () => {
      this.handleGameRestart();
    });
    (_this$playButton2 = this.playButton) === null || _this$playButton2 === void 0 || (_this$playButton2 = _this$playButton2.playButton) === null || _this$playButton2 === void 0 || _this$playButton2.on("pointerdown", () => {
      if (this.isGameRunning) {
        this.treeCollision();
      } else {
        this.handleGameRestart();
      }
    });
  }
  handleGameRestart() {
    var _this$player3, _this$trees4, _this$flags4, _this$playButton3, _this$player4;
    this.physics.resume();
    (_this$player3 = this.player) === null || _this$player3 === void 0 || _this$player3.setVelocityY(0);
    (_this$trees4 = this.trees) === null || _this$trees4 === void 0 || _this$trees4.clear(true, true);
    (_this$flags4 = this.flags) === null || _this$flags4 === void 0 || _this$flags4.clear(true, true);
    this.collectedFlags = 0;
    this.score = 0;

    // this.playButtonContainer?.setAlpha(0);
    // this.isPlayButtonVisible = false;
    this.anims.resumeAll();
    this.isGameRunning = true;
    (_this$playButton3 = this.playButton) === null || _this$playButton3 === void 0 || _this$playButton3.onGameStart();

    // this.player?.setTexture("marty-in-plane");
    (_this$player4 = this.player) === null || _this$player4 === void 0 || _this$player4.positionOnMountain();
  }
  handleRightWallCollision() {
    // the player should not be able to move past the right wall
    if (!this.player) return;
    if (!this.rightWall) return;
    this.physics.add.collider(this.player, this.rightWall, () => {
      if (this.player) {
        this.player.canGoRight = false;
      }
    });
  }
  treeCollision() {
    var _this$player5, _this$playButton4;
    this.isGameRunning = false;
    this.physics.pause();
    this.anims.pauseAll();
    (_this$player5 = this.player) === null || _this$player5 === void 0 || _this$player5.die();
    (_this$playButton4 = this.playButton) === null || _this$playButton4 === void 0 || _this$playButton4.onGameOver();
    // this.playButtonContainer?.setAlpha(1);
    // this.isPlayButtonVisible = true;

    this.treeSpawnTime = 0;
    this.score = 0;
    this.collectedFlags = 0;
    this.scoreDeltaTime = 0;
    this.gameSpeedModifier = 1;
    _EventBus.EventBus.emit('die', this);
  }
  handleTreeCollisions() {
    if (!this.trees) return;
    if (!this.player) return;
    this.physics.add.collider(this.trees, this.player, () => {
      this.treeCollision();
    });
  }
  handleFlagsCollisions() {
    if (!this.flags) return;
    if (!this.flagsBadAreas) return;
    if (!this.player) return;
    this.physics.add.collider(this.flags, this.player, (player, flags) => {
      var _this$progressSound;
      if (flags.getData('collected')) return;
      if (this.time.now - this.lastTimeFlagCollected < this.flagCollectedInterval) return;
      this.lastTimeFlagCollected = this.time.now;
      // flags.destroy();
      flags.setData('collected', true);
      // this.score += 40;
      this.collectedFlags++;
      _EventBus.EventBus.emit('flags-collected', this);
      if (this.collectedFlags === 10) {
        var _this$playButton5;
        _EventBus.EventBus.emit('target-achieved', this);
        this.isGameRunning = false;
        (_this$playButton5 = this.playButton) === null || _this$playButton5 === void 0 || _this$playButton5.onFlagsCollected();
        // this.playButtonContainer?.setAlpha(1);
        // this.isPlayButtonVisible = true;
        this.physics.pause();
        this.anims.pauseAll();
      }
      (_this$progressSound = this.progressSound) === null || _this$progressSound === void 0 || _this$progressSound.play();
    });
    this.physics.add.collider(this.flagsBadAreas, this.player, (player, badArea) => {
      var _this$player6;
      if (badArea.getData('collected')) return;
      badArea.setData('collected', true);
      if (this.time.now - this.lastTimeFlagCollected < this.flagCollectedInterval) return;
      this.lastTimeFlagCollected = this.time.now;
      if (this.collectedFlags === 0) return;
      this.collectedFlags--;
      _EventBus.EventBus.emit('flags-lost', this);
      (_this$player6 = this.player) === null || _this$player6 === void 0 || (_this$player6 = _this$player6.hitSound) === null || _this$player6 === void 0 || _this$player6.play();
    });
  }
}
var _default = exports.default = PlayScene;