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.rexviewportcoordinateplugin = factory());
  })(this, (function () { 'use strict';
  
    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);
      Object.defineProperty(Constructor, "prototype", {
        writable: false
      });
      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
        }
      });
      Object.defineProperty(subClass, "prototype", {
        writable: false
      });
      if (superClass) _setPrototypeOf(subClass, superClass);
    }
  
    function _getPrototypeOf(o) {
      _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
      };
      return _getPrototypeOf(o);
    }
  
    function _setPrototypeOf(o, p) {
      _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : 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);
      };
    }
  
    var EventEmitter = Phaser.Events.EventEmitter;
  
    var MonitorViewport = function MonitorViewport(viewport) {
      if (viewport.events) {
        return viewport;
      }
  
      var events = new EventEmitter();
      var x = viewport.x;
      Object.defineProperty(viewport, 'x', {
        get: function get() {
          return x;
        },
        set: function set(value) {
          if (x !== value) {
            x = value;
            events.emit('update', viewport);
          }
        }
      });
      var y = viewport.y;
      Object.defineProperty(viewport, 'y', {
        get: function get() {
          return y;
        },
        set: function set(value) {
          if (y !== value) {
            y = value;
            events.emit('update', viewport);
          }
        }
      });
      var width = viewport.width;
      Object.defineProperty(viewport, 'width', {
        get: function get() {
          return width;
        },
        set: function set(value) {
          if (width !== value) {
            width = value;
            events.emit('update', viewport);
          }
        }
      });
      var height = viewport.height;
      Object.defineProperty(viewport, 'height', {
        get: function get() {
          return height;
        },
        set: function set(value) {
          if (height !== value) {
            height = value;
            events.emit('update', viewport);
          }
        }
      });
      viewport.events = events;
      return viewport;
    };
  
    var AddViewportCoordinateProperties = function AddViewportCoordinateProperties(gameObject, viewport, vpx, vpy, transformCallback) {
      // Don't attach properties again
      if (gameObject.hasOwnProperty('vp')) {
        return gameObject;
      }
  
      if (vpx === undefined) {
        vpx = 0.5;
      }
  
      if (vpy === undefined) {
        vpy = 0.5;
      }
  
      if (transformCallback === undefined) {
        transformCallback = DefaultTransformCallback;
      }
  
      MonitorViewport(viewport);
      var events = viewport.events;
      gameObject.vp = viewport; // Set position of game object when view-port changed.
  
      var Transform = function Transform() {
        transformCallback(gameObject, viewport, vpx, vpy);
      };
  
      events.on('update', Transform);
      gameObject.once('destroy', function () {
        events.off('update', Transform);
        gameObject.vp = undefined;
      });
      Object.defineProperty(gameObject, 'vpx', {
        get: function get() {
          return vpx;
        },
        set: function set(value) {
          if (vpx !== value) {
            vpx = value;
            Transform();
          }
        }
      });
      Object.defineProperty(gameObject, 'vpy', {
        get: function get() {
          return vpy;
        },
        set: function set(value) {
          if (vpy !== value) {
            vpy = value;
            Transform();
          }
        }
      });
      Transform();
    };
  
    var DefaultTransformCallback = function DefaultTransformCallback(gameObject, viewport, vpx, vpy) {
      gameObject.x = viewport.x + viewport.width * vpx;
      gameObject.y = viewport.y + viewport.height * vpy;
    };
  
    var ViewportCoordinatePlugin = /*#__PURE__*/function (_Phaser$Plugins$BaseP) {
      _inherits(ViewportCoordinatePlugin, _Phaser$Plugins$BaseP);
  
      var _super = _createSuper(ViewportCoordinatePlugin);
  
      function ViewportCoordinatePlugin(pluginManager) {
        _classCallCheck(this, ViewportCoordinatePlugin);
  
        return _super.call(this, pluginManager);
      }
  
      _createClass(ViewportCoordinatePlugin, [{
        key: "start",
        value: function start() {
          var eventEmitter = this.game.events;
          eventEmitter.on('destroy', this.destroy, this);
        }
      }, {
        key: "add",
        value: function add(gameObject, viewport, vpx, vpy, transformCallback) {
          return AddViewportCoordinateProperties(gameObject, viewport, vpx, vpy, transformCallback);
        }
      }]);
  
      return ViewportCoordinatePlugin;
    }(Phaser.Plugins.BasePlugin);
  
    return ViewportCoordinatePlugin;
  
  }));
  