"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _CogManager = require("../../utils/CogManager");
class SimplePlatformerControls extends Phaser.Plugins.ScenePlugin {
  constructor(scene, pluginManager, pluginKey) {
    super(scene, pluginManager, pluginKey);
    this.cursors = undefined;
    this.xbox = Phaser.Input.Gamepad.Configs.XBOX_360;
    this.up = false;
    this.right = false;
    this.down = false;
    this.left = false;
    this.aDown = false;
    this.xDown = false;
    this.events = new Phaser.Events.EventEmitter();
    this.input = undefined;
    this.lastMobDir = 'idle-right';
    this.prefInputMethod = 'touch';
  }
  _reset(cursors) {
    this.up = false;
    this.right = false;
    this.down = false;
    this.left = false;
    this.aDown = false;
    this.xDown = false;
    if (cursors) {
      cursors.up.isDown = false;
      cursors.right.isDown = false;
      cursors.down.isDown = false;
      cursors.left.isDown = false;
    }
  }
  uregisterCogEvents(cog) {
    const connectedCog = cog ? cog : (0, _CogManager.getConnectedCog)();
    if (connectedCog) {
      connectedCog.publishedDataAnalyser.removeAllListeners();
    }
  }
  registerCogEvents(cog) {
    this.uregisterCogEvents(cog);
    const connectedCog = cog ? cog : (0, _CogManager.getConnectedCog)();
    if (connectedCog) {
      const eventsMap = connectedCog.publishedDataAnalyser.eventsMap;
      connectedCog.publishedDataAnalyser.on(eventsMap.tilt.backward, () => {
        this._reset();
        this[this.selectedDownBlock] = true;
      });
      connectedCog.publishedDataAnalyser.on(eventsMap.tilt.forward, () => {
        this._reset();
        this[this.selectedUpBlock] = true;
      });
      connectedCog.publishedDataAnalyser.on(eventsMap.tilt.left, () => {
        this._reset();
        this[this.selectedLeftBlock] = true;
      });
      connectedCog.publishedDataAnalyser.on(eventsMap.tilt.right, () => {
        this._reset();
        this[this.selectedRightBlock] = true;
      });
      connectedCog.publishedDataAnalyser.on(eventsMap.tilt.none, () => {
        this._reset();
      });
    }
  }
  start() {
    var _this$input, _this$input2, _this$input3, _this$scene2, _this$scene3;
    if (!this.input) {
      var _this$scene;
      this.input = (_this$scene = this.scene) === null || _this$scene === void 0 ? void 0 : _this$scene.input;
    }
    this.prefInputMethod = 'touch';
    this.cursors = (_this$input = this.input) === null || _this$input === void 0 || (_this$input = _this$input.keyboard) === null || _this$input === void 0 ? void 0 : _this$input.createCursorKeys();
    if (!this.cursors) {
      return;
    }

    // reset!
    this._reset(this.cursors);
    (_this$input2 = this.input) === null || _this$input2 === void 0 || (_this$input2 = _this$input2.keyboard) === null || _this$input2 === void 0 || _this$input2.on('keydown', e => {
      switch (e.code) {
        case 'Space':
          this.aDown = true;
          break;
        case 'KeyD':
        case 'KeyX':
          this.xDown = true;
          break;
      }
      this.prefInputMethod = 'keyboard';
    });
    (_this$input3 = this.input) === null || _this$input3 === void 0 || (_this$input3 = _this$input3.keyboard) === null || _this$input3 === void 0 || _this$input3.on('keyup', e => {
      this.events.emit('anyup');
      switch (e.code) {
        case 'Space':
          this.aDown = false;
          this.events.emit('aup');
          break;
        case 'KeyD':
        case 'KeyX':
          this.xDown = false;
          this.events.emit('xup');
          break;
      }
    });
    this.registerCogEvents();
    (_this$scene2 = this.scene) === null || _this$scene2 === void 0 || _this$scene2.events.on('preupdate', this.preUpdate, this);
    (_this$scene3 = this.scene) === null || _this$scene3 === void 0 || _this$scene3.events.on('shutdown', this.shutdown, this);
  }
  preUpdate() {
    const connectedCog = (0, _CogManager.getConnectedCog)();
    // if (!connectedCog) {
    if (false) {
      var _this$cursors, _this$cursors2, _this$cursors3, _this$cursors4;
      // comment in the above to use the arrow keys instead of the cog
      this.up = !!((_this$cursors = this.cursors) !== null && _this$cursors !== void 0 && _this$cursors.up.isDown);
      this.right = !!((_this$cursors2 = this.cursors) !== null && _this$cursors2 !== void 0 && _this$cursors2.right.isDown);
      this.down = !!((_this$cursors3 = this.cursors) !== null && _this$cursors3 !== void 0 && _this$cursors3.down.isDown);
      this.left = !!((_this$cursors4 = this.cursors) !== null && _this$cursors4 !== void 0 && _this$cursors4.left.isDown);
    }
    // @ts-ignore - justUp is not in the Pointer class for some reason
    // if (this.input?.activePointer.justUp) {
    //     this.prefInputMethod = 'touch';
    // }

    // if (this.prefInputMethod == 'touch') {

    //     // @ts-ignore - justUp is not in the Pointer class for some reason
    //     if (this.input?.activePointer.justUp) {
    //         switch (this.lastMobDir) {
    //             case 'idle-right':
    //                 this.lastMobDir = 'run-right';
    //                 break;
    //             case 'idle-left':
    //                 this.lastMobDir = 'run-left';
    //                 break;
    //             case 'run-right':
    //                 this.lastMobDir = 'run-left';
    //                 break;
    //             case 'run-left':
    //                 this.lastMobDir = 'run-right';
    //                 break;
    //         }
    //     }

    //     switch (this.lastMobDir) {
    //         case 'idle-right':
    //         case 'idle-left':
    //             this.left = false;
    //             this.right = false;
    //             break;
    //         case 'run-right':
    //             this.right = true;
    //             this.left = false;
    //             break;
    //         case 'run-left':
    //             this.right = false;
    //             this.left = true;
    //             break;
    //     }

    // if (this.input?.activePointer.isDown) {
    //     this.aDown = true;
    //     this.up = true;
    //     this.down = false;
    // } else {
    //     this.aDown = false;
    //     this.up = false;
    //     this.down = true;
    // }
    // }
  }
  shutdown() {
    var _this$scene4, _this$scene5;
    (_this$scene4 = this.scene) === null || _this$scene4 === void 0 || _this$scene4.events.off('preupdate', this.preUpdate);
    (_this$scene5 = this.scene) === null || _this$scene5 === void 0 || _this$scene5.events.off('shutdown', this.shutdown);
    this.uregisterCogEvents();
  }
}
var _default = exports.default = SimplePlatformerControls;