"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/web.dom-collections.iterator.js");
var _highScoreStore = require("../../utils/high-score-store");
var _Player = require("../entities/Player");
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 Level extends Phaser.Scene {
  constructor(config) {
    super(config ? config : {
      key: 'level'
    });
    _defineProperty(this, "stars", null);
    this.gamepaused = undefined;
    this.player = undefined;
    this.exits = [];
    this.level = 0;
    this.score = 0;
  }
  create() {
    this.cameras.main.setRoundPixels(true);
    // start controls
    this.controls.start();
    this.exits = [];
    this.cameras.main.setBackgroundColor('#00FF00');
    _EventBus.EventBus.emit('current-scene-ready', this);
  }
  postCreate() {
    this.gamepaused = this.add.image(+this.sys.game.config.width / 2, +this.sys.game.config.height / 2, 'gamepaused');
    this.gamepaused.visible = false;
    this.gamepaused.setScrollFactor(0);
    this.gamepaused.setDepth(3);
    this.resizeField(+this.sys.game.config.width, +this.sys.game.config.height);
    this.cameras.main.flash(3000, window.fadeColor.r, window.fadeColor.g, window.fadeColor.b);
  }
  addPlayer() {
    let {
      x = 64,
      y = 64
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    this.player = new _Player.Player(this, x, y, 'player', "0", this.startPosition.facing);
    this.cameras.main.startFollow(this.player, true);
  }
  addExit() {
    var _this$game$config$phy;
    let {
      x = -480,
      y = 0,
      w = 512,
      h = 2048,
      startX = 64,
      startY = 64,
      facing = 'right',
      scene = "",
      level = 0
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    if (!scene) {
      return;
    }
    const exit = {
      rect: new Phaser.Geom.Rectangle(x, y, w, h),
      startX: startX,
      startY: startY,
      facing: facing,
      scene: scene,
      level: level
    };
    this.exits.push(exit);

    // DEBUGGING: paint the exit for debugging
    if ((_this$game$config$phy = this.game.config.physics.arcade) !== null && _this$game$config$phy !== void 0 && _this$game$config$phy.debug) {
      const graphics = this.add.graphics();
      graphics.fillStyle(0x0000ff, 0.5); // sets the fill color and alpha
      graphics.fillRect(x, y, w, h); // draws the rectangle
      graphics.fillPath(); // applies the fill
    }
  }
  checkExits() {
    if (!this.exits || !this.exits.length || !this.player) {
      return;
    }
    for (let exit of this.exits) {
      if (Phaser.Geom.Rectangle.ContainsPoint(exit.rect, this.player)) {
        //console.log('time to go');
        this.leaveThroughExit(exit);
        this.player.disappear();
        this.exits = [];
        break;
      }
    }
  }
  createStars() {
    this.stars = this.physics.add.group();
  }
  handleStarCollisions() {
    if (!this.stars) return;
    if (!this.player) return;
    this.physics.add.overlap(this.stars, this.player, (player, star) => {
      var _this$stars;
      (_this$stars = this.stars) === null || _this$stars === void 0 || _this$stars.remove(star);
      star.destroy();
      this.score += 40;
      const currentHighScore = (0, _highScoreStore.getHighScore)();
      if (this.score > currentHighScore) {
        (0, _highScoreStore.updateHighScore)(this.score);
      }
      _EventBus.EventBus.emit('player-collected-star', this);
    });
  }
  leaveThroughExit(_ref) {
    let {
      startX = 0,
      startY = 0,
      facing = 'right',
      scene = "",
      level = 0
    } = _ref;
    if (level >= 4) {
      this.showGameOver();
      return;
    }
    if (!scene) {
      return;
    }
    this.startPosition.setExit({
      x: startX,
      y: startY,
      facing: facing,
      scene: scene
    });
    if (this.level > level) {
      // going back -- subtract score
      this.score -= this.level * 100;
    } else {
      // going forward -- add score
      this.score += level * 100;
    }
    this.level = level;
    this.cameras.main.once('camerafadeoutcomplete', (camera, effect, thisArg) => {
      this.scene.start(scene);
    }, this);
    window.fadeColor = {
      r: 5,
      g: 4,
      b: 4
    };
    this.cameras.main.fadeOut(3000, window.fadeColor.r, window.fadeColor.g, window.fadeColor.b);
    const currentHighScore = (0, _highScoreStore.getHighScore)();
    if (this.score > currentHighScore) {
      (0, _highScoreStore.updateHighScore)(this.score);
    }
    _EventBus.EventBus.emit('player-went-through-exit', this);
  }
  resizeField(w, h) {
    //console.log(w, h);
    if (this.gamepaused) {
      this.gamepaused.x = w / 2;
      this.gamepaused.y = h / 2;
    }
    if (this.centerMap) {
      this.cameras.main.setBounds((this.centerMap.widthInPixels - w) / 2, (this.centerMap.heightInPixels - h) / 2, w, h);
    }
  }
  onGamePause() {
    if (this.gamepaused) {
      // don't show "paused" message
      // this.gamepaused.visible = true;
    }
  }
  onGameResume() {
    if (this.gamepaused) {
      // don't show "paused" message
      // this.gamepaused.visible = false;
    }
  }
  showGameOver() {
    this.level = 0;
    _EventBus.EventBus.emit('game-finished', this);
    this.cameras.main.fadeOut(5000, 0, 0, 0, () => {
      this.time.delayedCall(5000, () => {
        this.scene.start("boot");
        _EventBus.EventBus.emit('game-finished', this);
      });
    });
  }
}
var _default = exports.default = Level;