text: (function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
    typeof define === 'function' && define.amd ? define(factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.rexeightdirectionplugin = factory());
  }(this, (function () { 'use strict';
  
    function _typeof(obj) {
      "@babel/helpers - typeof";
  
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function (obj) {
          return typeof obj;
        };
      } else {
        _typeof = function (obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }
  
      return _typeof(obj);
    }
  
    function _classCallCheck(instance, Constructor) {
      if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
      }
    }
  
    function _defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }
  
    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);
      if (staticProps) _defineProperties(Constructor, staticProps);
      return Constructor;
    }
  
    function _inherits(subClass, superClass) {
      if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function");
      }
  
      subClass.prototype = Object.create(superClass && superClass.prototype, {
        constructor: {
          value: subClass,
          writable: true,
          configurable: true
        }
      });
      if (superClass) _setPrototypeOf(subClass, superClass);
    }
  
    function _getPrototypeOf(o) {
      _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
      };
      return _getPrototypeOf(o);
    }
  
    function _setPrototypeOf(o, p) {
      _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
        o.__proto__ = p;
        return o;
      };
  
      return _setPrototypeOf(o, p);
    }
  
    function _isNativeReflectConstruct() {
      if (typeof Reflect === "undefined" || !Reflect.construct) return false;
      if (Reflect.construct.sham) return false;
      if (typeof Proxy === "function") return true;
  
      try {
        Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
        return true;
      } catch (e) {
        return false;
      }
    }
  
    function _assertThisInitialized(self) {
      if (self === void 0) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
      }
  
      return self;
    }
  
    function _possibleConstructorReturn(self, call) {
      if (call && (typeof call === "object" || typeof call === "function")) {
        return call;
      } else if (call !== void 0) {
        throw new TypeError("Derived constructors may only return object or undefined");
      }
  
      return _assertThisInitialized(self);
    }
  
    function _createSuper(Derived) {
      var hasNativeReflectConstruct = _isNativeReflectConstruct();
  
      return function _createSuperInternal() {
        var Super = _getPrototypeOf(Derived),
            result;
  
        if (hasNativeReflectConstruct) {
          var NewTarget = _getPrototypeOf(this).constructor;
  
          result = Reflect.construct(Super, arguments, NewTarget);
        } else {
          result = Super.apply(this, arguments);
        }
  
        return _possibleConstructorReturn(this, result);
      };
    }
  
    function _superPropBase(object, property) {
      while (!Object.prototype.hasOwnProperty.call(object, property)) {
        object = _getPrototypeOf(object);
        if (object === null) break;
      }
  
      return object;
    }
  
    function _get(target, property, receiver) {
      if (typeof Reflect !== "undefined" && Reflect.get) {
        _get = Reflect.get;
      } else {
        _get = function _get(target, property, receiver) {
          var base = _superPropBase(target, property);
  
          if (!base) return;
          var desc = Object.getOwnPropertyDescriptor(base, property);
  
          if (desc.get) {
            return desc.get.call(receiver);
          }
  
          return desc.value;
        };
      }
  
      return _get(target, property, receiver || target);
    }
  
    var EventEmitterMethods = {
      setEventEmitter: function setEventEmitter(eventEmitter, EventEmitterClass) {
        if (EventEmitterClass === undefined) {
          EventEmitterClass = Phaser.Events.EventEmitter; // Use built-in EventEmitter class by default
        }
  
        this._privateEE = eventEmitter === true || eventEmitter === undefined;
        this._eventEmitter = this._privateEE ? new EventEmitterClass() : eventEmitter;
        return this;
      },
      destroyEventEmitter: function destroyEventEmitter() {
        if (this._eventEmitter && this._privateEE) {
          this._eventEmitter.shutdown();
        }
  
        return this;
      },
      getEventEmitter: function getEventEmitter() {
        return this._eventEmitter;
      },
      on: function on() {
        if (this._eventEmitter) {
          this._eventEmitter.on.apply(this._eventEmitter, arguments);
        }
  
        return this;
      },
      once: function once() {
        if (this._eventEmitter) {
          this._eventEmitter.once.apply(this._eventEmitter, arguments);
        }
  
        return this;
      },
      off: function off() {
        if (this._eventEmitter) {
          this._eventEmitter.off.apply(this._eventEmitter, arguments);
        }
  
        return this;
      },
      emit: function emit(event) {
        if (this._eventEmitter && event) {
          this._eventEmitter.emit.apply(this._eventEmitter, arguments);
        }
  
        return this;
      },
      addListener: function addListener() {
        if (this._eventEmitter) {
          this._eventEmitter.addListener.apply(this._eventEmitter, arguments);
        }
  
        return this;
      },
      removeListener: function removeListener() {
        if (this._eventEmitter) {
          this._eventEmitter.removeListener.apply(this._eventEmitter, arguments);
        }
  
        return this;
      },
      removeAllListeners: function removeAllListeners() {
        if (this._eventEmitter) {
          this._eventEmitter.removeAllListeners.apply(this._eventEmitter, arguments);
        }
  
        return this;
      },
      listenerCount: function listenerCount() {
        if (this._eventEmitter) {
          return this._eventEmitter.listenerCount.apply(this._eventEmitter, arguments);
        }
  
        return 0;
      },
      listeners: function listeners() {
        if (this._eventEmitter) {
          return this._eventEmitter.listeners.apply(this._eventEmitter, arguments);
        }
  
        return [];
      },
      eventNames: function eventNames() {
        if (this._eventEmitter) {
          return this._eventEmitter.eventNames.apply(this._eventEmitter, arguments);
        }
  
        return [];
      }
    };
  
    var SceneClass = Phaser.Scene;
  
    var IsSceneObject = function IsSceneObject(object) {
      return object instanceof SceneClass;
    };
  
    var GetSceneObject = function GetSceneObject(object) {
      if (object == null || _typeof(object) !== 'object') {
        return null;
      } else if (IsSceneObject(object)) {
        // object = scene
        return object;
      } else if (object.scene && IsSceneObject(object.scene)) {
        // object = game object
        return object.scene;
      } else if (object.parent && object.parent.scene && IsSceneObject(object.parent.scene)) {
        // parent = bob object
        return object.parent.scene;
      }
    };
  
    var GetValue$2 = Phaser.Utils.Objects.GetValue;
  
    var ComponentBase = /*#__PURE__*/function () {
      function ComponentBase(parent, config) {
        _classCallCheck(this, ComponentBase);
  
        this.parent = parent; // gameObject or scene
  
        this.scene = GetSceneObject(parent);
        this.isShutdown = false; // Event emitter, default is private event emitter
  
        this.setEventEmitter(GetValue$2(config, 'eventEmitter', true)); // Register callback of parent destroy event, also see `shutdown` method
  
        if (this.parent && this.parent === this.scene) {
          // parent is a scene
          this.scene.events.once('shutdown', this.onSceneDestroy, this);
        } else if (this.parent && this.parent.once) {
          // bob object does not have event emitter
          this.parent.once('destroy', this.onParentDestroy, this);
        }
      }
  
      _createClass(ComponentBase, [{
        key: "shutdown",
        value: function shutdown(fromScene) {
          // Already shutdown
          if (this.isShutdown) {
            return;
          } // parent might not be shutdown yet
  
  
          if (this.parent && this.parent === this.scene) {
            // parent is a scene
            this.scene.events.off('shutdown', this.onSceneDestroy, this);
          } else if (this.parent && this.parent.once) {
            // bob object does not have event emitter
            this.parent.off('destroy', this.onParentDestroy, this);
          }
  
          this.destroyEventEmitter();
          this.parent = undefined;
          this.scene = undefined;
          this.isShutdown = true;
        }
      }, {
        key: "destroy",
        value: function destroy(fromScene) {
          this.shutdown(fromScene);
        }
      }, {
        key: "onSceneDestroy",
        value: function onSceneDestroy() {
          this.destroy(true);
        }
      }, {
        key: "onParentDestroy",
        value: function onParentDestroy(parent, fromScene) {
          this.destroy(fromScene);
        }
      }]);
  
      return ComponentBase;
    }();
    Object.assign(ComponentBase.prototype, EventEmitterMethods);
  
    var GetValue$1 = Phaser.Utils.Objects.GetValue;
  
    var TickTask = /*#__PURE__*/function (_ComponentBase) {
      _inherits(TickTask, _ComponentBase);
  
      var _super = _createSuper(TickTask);
  
      function TickTask(parent, config) {
        var _this;
  
        _classCallCheck(this, TickTask);
  
        _this = _super.call(this, parent, config);
        _this._isRunning = false;
        _this.isPaused = false;
        _this.tickingState = false;
  
        _this.setTickingMode(GetValue$1(config, 'tickingMode', 1)); // boot() later
  
  
        return _this;
      } // override
  
  
      _createClass(TickTask, [{
        key: "boot",
        value: function boot() {
          if (this.tickingMode === 2 && !this.tickingState) {
            this.startTicking();
          }
        } // override
  
      }, {
        key: "shutdown",
        value: function shutdown(fromScene) {
          // Already shutdown
          if (this.isShutdown) {
            return;
          }
  
          this.stop();
  
          if (this.tickingState) {
            this.stopTicking();
          }
  
          _get(_getPrototypeOf(TickTask.prototype), "shutdown", this).call(this, fromScene);
        }
      }, {
        key: "setTickingMode",
        value: function setTickingMode(mode) {
          if (typeof mode === 'string') {
            mode = TICKINGMODE[mode];
          }
  
          this.tickingMode = mode;
        } // override
  
      }, {
        key: "startTicking",
        value: function startTicking() {
          this.tickingState = true;
        } // override
  
      }, {
        key: "stopTicking",
        value: function stopTicking() {
          this.tickingState = false;
        }
      }, {
        key: "isRunning",
        get: function get() {
          return this._isRunning;
        },
        set: function set(value) {
          if (this._isRunning === value) {
            return;
          }
  
          this._isRunning = value;
  
          if (this.tickingMode === 1 && value != this.tickingState) {
            if (value) {
              this.startTicking();
            } else {
              this.stopTicking();
            }
          }
        }
      }, {
        key: "start",
        value: function start() {
          this.isPaused = false;
          this.isRunning = true;
          return this;
        }
      }, {
        key: "pause",
        value: function pause() {
          // Only can ba paused in running state
          if (this.isRunning) {
            this.isPaused = true;
            this.isRunning = false;
          }
  
          return this;
        }
      }, {
        key: "resume",
        value: function resume() {
          // Only can ba resumed in paused state (paused from running state)
          if (this.isPaused) {
            this.isRunning = true;
          }
  
          return this;
        }
      }, {
        key: "stop",
        value: function stop() {
          this.isPaused = false;
          this.isRunning = false;
          return this;
        }
      }, {
        key: "complete",
        value: function complete() {
          this.isPaused = false;
          this.isRunning = false;
          this.emit('complete', this.parent, this);
        }
      }]);
  
      return TickTask;
    }(ComponentBase);
  
    var TICKINGMODE = {
      'no': 0,
      'lazy': 1,
      'always': 2
    };
  
    var SceneUpdateTickTask = /*#__PURE__*/function (_TickTask) {
      _inherits(SceneUpdateTickTask, _TickTask);
  
      var _super = _createSuper(SceneUpdateTickTask);
  
      function SceneUpdateTickTask() {
        _classCallCheck(this, SceneUpdateTickTask);
  
        return _super.apply(this, arguments);
      }
  
      _createClass(SceneUpdateTickTask, [{
        key: "startTicking",
        value: function startTicking() {
          _get(_getPrototypeOf(SceneUpdateTickTask.prototype), "startTicking", this).call(this);
  
          this.scene.events.on('update', this.update, this);
        }
      }, {
        key: "stopTicking",
        value: function stopTicking() {
          _get(_getPrototypeOf(SceneUpdateTickTask.prototype), "stopTicking", this).call(this);
  
          if (this.scene) {
            // Scene might be destoryed
            this.scene.events.off('update', this.update, this);
          }
        } // update(time, delta) {
        //     
        // }
  
      }]);
  
      return SceneUpdateTickTask;
    }(TickTask);
  
    var SetVelocity = function SetVelocity(gameObject, vx, vy, onChange) {
      var body = gameObject.body;
      var preVx = body.velocity.x,
          preVy = body.velocity.y;
  
      if (vx === preVx && vy === preVy) {
        return;
      }
  
      body.setVelocity(vx, vy);
  
      if (onChange) {
        onChange(vx, vy, preVx, preVy);
      }
    };
  
    /**
     * @author       Richard Davey <rich@photonstorm.com>
     * @copyright    2018 Photon Storm Ltd.
     * @license      {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
     */
    var DEG_TO_RAD = Math.PI / 180;
    /**
     * Convert the given angle from degrees, to the equivalent angle in radians.
     *
     * @function Phaser.Math.DegToRad
     * @since 3.0.0
     *
     * @param {integer} degrees - The angle (in degrees) to convert to radians.
     *
     * @return {number} The given angle converted to radians.
     */
  
    var DegToRad = function DegToRad(degrees) {
      return degrees * DEG_TO_RAD;
    };
  
    var GetValue = Phaser.Utils.Objects.GetValue;
  
    var EightDirection = /*#__PURE__*/function (_TickTask) {
      _inherits(EightDirection, _TickTask);
  
      var _super = _createSuper(EightDirection);
  
      function EightDirection(gameObject, config) {
        var _this;
  
        _classCallCheck(this, EightDirection);
  
        _this = _super.call(this, gameObject, config); // this.parent = gameObject;
  
        _this.resetFromJSON(config);
  
        _this.boot();
  
        return _this;
      }
  
      _createClass(EightDirection, [{
        key: "resetFromJSON",
        value: function resetFromJSON(o) {
          if (!this.parent.body) {
            this.scene.physics.add.existing(this.parent, false);
          }
  
          this.setEnable(GetValue(o, 'enable', true));
          this.setDirMode(GetValue(o, 'dir', '8dir'));
          this.setSpeed(GetValue(o, 'speed', 200));
          this.setRotateToDirection(GetValue(o, 'rotateToDirection', false));
          this.setWrapMode(GetValue(o, 'wrap', false), GetValue(o, 'padding', 0));
          this.setCursorKeys(GetValue(o, 'cursorKeys', undefined));
          return this;
        }
      }, {
        key: "enable",
        get: function get() {
          return this.isRunning;
        },
        set: function set(value) {
          this.isRunning = value;
  
          if (!value) {
            SetVelocity(this, 0, 0);
          }
        }
      }, {
        key: "setEnable",
        value: function setEnable(e) {
          if (e == undefined) {
            e = true;
          }
  
          this.enable = e;
  
          if (e && this.body === undefined) {
            this.scene.physics.add.existing(this.parent, false);
          }
  
          return this;
        }
      }, {
        key: "setDirMode",
        value: function setDirMode(m) {
          if (typeof m === 'string') {
            m = DIRMODE[m];
          }
  
          this.dirMode = m;
          return this;
        }
      }, {
        key: "setSpeed",
        value: function setSpeed(speed) {
          this.speed = speed;
          return this;
        }
      }, {
        key: "setRotateToDirection",
        value: function setRotateToDirection(rotateToDirection) {
          this.rotateToDirection = rotateToDirection;
          return this;
        }
      }, {
        key: "setWrapMode",
        value: function setWrapMode(wrap, padding) {
          if (wrap === undefined) {
            wrap = true;
          }
  
          this.wrap = wrap;
          this.padding = padding;
          return this;
        }
      }, {
        key: "setCursorKeys",
        value: function setCursorKeys(cursorKeys) {
          if (cursorKeys === undefined) {
            cursorKeys = this.scene.input.keyboard.createCursorKeys();
          }
  
          this.cursorKeys = cursorKeys;
          return this;
        }
      }, {
        key: "isLeft",
        get: function get() {
          return this.enable ? this.cursorKeys.left.isDown : false;
        }
      }, {
        key: "isRight",
        get: function get() {
          return this.enable ? this.cursorKeys.right.isDown : false;
        }
      }, {
        key: "isUp",
        get: function get() {
          return this.enable ? this.cursorKeys.up.isDown : false;
        }
      }, {
        key: "isDown",
        get: function get() {
          return this.enable ? this.cursorKeys.down.isDown : false;
        }
      }, {
        key: "update",
        value: function update(time, delta) {
          var gameObject = this.parent;
  
          if (!this.enable) {
            SetVelocity(gameObject, 0, 0);
            return this;
          }
  
          if (!gameObject.active) {
            return this;
          }
  
          var dy = (this.isUp ? -1 : 0) + (this.isDown ? 1 : 0),
              dx = (this.isLeft ? -1 : 0) + (this.isRight ? 1 : 0);
  
          if (dx === 0 && dy === 0) {
            SetVelocity(gameObject, 0, 0);
            return this;
          }
  
          switch (this.dirMode) {
            case 0:
              // up&down
              dx = 0;
              break;
  
            case 1:
              // left&right
              dy = 0;
              break;
  
            case 2:
              // 4dir
              if (dy !== 0) {
                dx = 0;
              }
  
              break;
          }
  
          var rotation, vx, vy;
  
          if (dy === 0) {
            // dx !== 0
            vx = this.speed * dx;
            vy = 0;
            rotation = dx === 1 ? RAD0 : RAD180;
          } else if (dx === 0) {
            // dy !== 0
            vx = 0;
            vy = this.speed * dy;
            rotation = dy === 1 ? RAD90 : RAD270;
          } else {
            // (dx !== 0) && (dy !== 0)
            rotation = Math.atan2(dy, dx);
            vx = this.speed * Math.cos(rotation);
            vy = this.speed * Math.sin(rotation);
          }
  
          SetVelocity(gameObject, vx, vy);
  
          if (this.rotateToDirection && rotation !== undefined) {
            gameObject.rotation = rotation;
          }
  
          if (this.wrap) {
            gameObject.body.world.wrap(gameObject, this.padding);
          }
  
          return this;
        }
      }]);
  
      return EightDirection;
    }(SceneUpdateTickTask);
  
    var DIRMODE = {
      'up&down': 0,
      'left&right': 1,
      '4dir': 2,
      '8dir': 3
    };
    var RAD0 = DegToRad(0);
    var RAD90 = DegToRad(90);
    var RAD180 = DegToRad(180);
    var RAD270 = DegToRad(270);
  
    var EightDirectionPlugin = /*#__PURE__*/function (_Phaser$Plugins$BaseP) {
      _inherits(EightDirectionPlugin, _Phaser$Plugins$BaseP);
  
      var _super = _createSuper(EightDirectionPlugin);
  
      function EightDirectionPlugin(pluginManager) {
        _classCallCheck(this, EightDirectionPlugin);
  
        return _super.call(this, pluginManager);
      }
  
      _createClass(EightDirectionPlugin, [{
        key: "start",
        value: function start() {
          var eventEmitter = this.game.events;
          eventEmitter.on('destroy', this.destroy, this);
        }
      }, {
        key: "add",
        value: function add(gameObject, config) {
          return new EightDirection(gameObject, config);
        }
      }]);
  
      return EightDirectionPlugin;
    }(Phaser.Plugins.BasePlugin);
  
    return EightDirectionPlugin;
  
  })));
  