phaser3-rex-plugins
Version:
560 lines (541 loc) • 18.9 kB
JavaScript
(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.rextouchhelperplugin = factory());
})(this, (function () { 'use strict';
function _typeof(obj) {
"@babel/helpers - typeof";
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
}, _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, _toPropertyKey(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);
};
}
function _superPropBase(object, property) {
while (!Object.prototype.hasOwnProperty.call(object, property)) {
object = _getPrototypeOf(object);
if (object === null) break;
}
return object;
}
function _get() {
if (typeof Reflect !== "undefined" && Reflect.get) {
_get = Reflect.get.bind();
} 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(arguments.length < 3 ? target : receiver);
}
return desc.value;
};
}
return _get.apply(this, arguments);
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _toPrimitive(input, hint) {
if (typeof input !== "object" || input === null) return input;
var prim = input[Symbol.toPrimitive];
if (prim !== undefined) {
var res = prim.call(input, hint || "default");
if (typeof res !== "object") return res;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return (hint === "string" ? String : Number)(input);
}
function _toPropertyKey(arg) {
var key = _toPrimitive(arg, "string");
return typeof key === "symbol" ? key : String(key);
}
var HitTest$1 = function HitTest(scene, topOnly, gameObjects, pointers, out) {
var inputPlugin = scene.input;
var inputManager = inputPlugin.manager;
if (topOnly === undefined) {
topOnly = inputPlugin.topOnly;
}
if (out === undefined) {
out = [];
}
if (gameObjects === undefined) {
gameObjects = inputPlugin._list;
}
var pointersTotal, pointer;
if (pointers === undefined) {
pointers = inputManager.pointers;
pointersTotal = inputManager.pointersTotal;
}
if (pointersTotal === undefined) {
pointersTotal = pointers.length;
}
var cameras;
for (var pointerIdx = 0; pointerIdx < pointersTotal; pointerIdx++) {
pointer = pointers[pointerIdx];
cameras = inputPlugin.cameras.getCamerasBelowPointer(pointer);
for (var cameraIdx = 0, camerasTotal = cameras.length; cameraIdx < camerasTotal; cameraIdx++) {
var _out;
(_out = out).push.apply(_out, _toConsumableArray(inputManager.hitTest(pointer, gameObjects, cameras[cameraIdx], HitTestResult$1)));
}
}
inputPlugin.sortGameObjects(out);
if (topOnly) {
if (out.length) {
out.splice(1);
}
}
HitTestResult$1.length = 0;
return out;
};
var HitTestResult$1 = [];
var IsPointerInHitArea = function IsPointerInHitArea(gameObject, pointer, preTest, postTest) {
if (pointer) {
if (preTest && !preTest(gameObject, pointer)) {
return false;
}
if (!HitTest(gameObject, pointer)) {
return false;
}
if (postTest && !postTest(gameObject, pointer)) {
return false;
}
return true;
} else {
var inputManager = gameObject.scene.input.manager;
var pointersTotal = inputManager.pointersTotal;
var pointers = inputManager.pointers,
pointer;
for (var i = 0; i < pointersTotal; i++) {
pointer = pointers[i];
if (preTest && !preTest(gameObject, pointer)) {
continue;
}
if (!HitTest(gameObject, pointer)) {
continue;
}
if (postTest && !postTest(gameObject, pointer)) {
continue;
}
return true;
}
return false;
}
};
var HitTest = function HitTest(gameObject, pointer) {
var scene = gameObject.scene;
var cameras = scene.input.cameras.getCamerasBelowPointer(pointer);
var inputManager = scene.input.manager;
var gameObjects = [gameObject];
for (var i = 0, len = cameras.length; i < len; i++) {
inputManager.hitTest(pointer, gameObjects, cameras[i], HitTestResult);
if (HitTestResult.length > 0) {
HitTestResult.length = 0;
return true;
}
}
HitTestResult.length = 0;
return false;
};
var HitTestResult = [];
var GetDisplayWidth = function GetDisplayWidth(gameObject) {
if (gameObject.displayWidth !== undefined) {
return gameObject.displayWidth;
} else {
return gameObject.width;
}
};
var GetDisplayHeight = function GetDisplayHeight(gameObject) {
if (gameObject.displayHeight !== undefined) {
return gameObject.displayHeight;
} else {
return gameObject.height;
}
};
var Rectangle = Phaser.Geom.Rectangle;
var Vector2 = Phaser.Math.Vector2;
var RotateAround = Phaser.Math.RotateAround;
var GetBounds = function GetBounds(gameObject, output) {
if (output === undefined) {
output = new Rectangle();
} else if (output === true) {
if (GlobRect === undefined) {
GlobRect = new Rectangle();
}
output = GlobRect;
}
if (gameObject.getBounds) {
return gameObject.getBounds(output);
}
// We can use the output object to temporarily store the x/y coords in:
var TLx, TLy, TRx, TRy, BLx, BLy, BRx, BRy;
// Instead of doing a check if parent container is
// defined per corner we only do it once.
if (gameObject.parentContainer) {
var parentMatrix = gameObject.parentContainer.getBoundsTransformMatrix();
GetTopLeft(gameObject, output);
parentMatrix.transformPoint(output.x, output.y, output);
TLx = output.x;
TLy = output.y;
GetTopRight(gameObject, output);
parentMatrix.transformPoint(output.x, output.y, output);
TRx = output.x;
TRy = output.y;
GetBottomLeft(gameObject, output);
parentMatrix.transformPoint(output.x, output.y, output);
BLx = output.x;
BLy = output.y;
GetBottomRight(gameObject, output);
parentMatrix.transformPoint(output.x, output.y, output);
BRx = output.x;
BRy = output.y;
} else {
GetTopLeft(gameObject, output);
TLx = output.x;
TLy = output.y;
GetTopRight(gameObject, output);
TRx = output.x;
TRy = output.y;
GetBottomLeft(gameObject, output);
BLx = output.x;
BLy = output.y;
GetBottomRight(gameObject, output);
BRx = output.x;
BRy = output.y;
}
output.x = Math.min(TLx, TRx, BLx, BRx);
output.y = Math.min(TLy, TRy, BLy, BRy);
output.width = Math.max(TLx, TRx, BLx, BRx) - output.x;
output.height = Math.max(TLy, TRy, BLy, BRy) - output.y;
return output;
};
var GlobRect = undefined;
var GetTopLeft = function GetTopLeft(gameObject, output, includeParent) {
if (output === undefined) {
output = new Vector2();
} else if (output === true) {
if (GlobVector === undefined) {
GlobVector = new Vector2();
}
output = GlobVector;
}
if (gameObject.getTopLeft) {
return gameObject.getTopLeft(output);
}
output.x = gameObject.x - GetDisplayWidth(gameObject) * gameObject.originX;
output.y = gameObject.y - GetDisplayHeight(gameObject) * gameObject.originY;
return PrepareBoundsOutput(gameObject, output, includeParent);
};
var GetTopRight = function GetTopRight(gameObject, output, includeParent) {
if (output === undefined) {
output = new Vector2();
} else if (output === true) {
if (GlobVector === undefined) {
GlobVector = new Vector2();
}
output = GlobVector;
}
if (gameObject.getTopRight) {
return gameObject.getTopRight(output);
}
output.x = gameObject.x - GetDisplayWidth(gameObject) * gameObject.originX + GetDisplayWidth(gameObject);
output.y = gameObject.y - GetDisplayHeight(gameObject) * gameObject.originY;
return PrepareBoundsOutput(gameObject, output, includeParent);
};
var GetBottomLeft = function GetBottomLeft(gameObject, output, includeParent) {
if (output === undefined) {
output = new Vector2();
} else if (output === true) {
if (GlobVector === undefined) {
GlobVector = new Vector2();
}
output = GlobVector;
}
if (gameObject.getBottomLeft) {
return gameObject.getBottomLeft(output);
}
output.x = gameObject.x - GetDisplayWidth(gameObject) * gameObject.originX;
output.y = gameObject.y - GetDisplayHeight(gameObject) * gameObject.originY + GetDisplayHeight(gameObject);
return PrepareBoundsOutput(gameObject, output, includeParent);
};
var GetBottomRight = function GetBottomRight(gameObject, output, includeParent) {
if (output === undefined) {
output = new Vector2();
} else if (output === true) {
if (GlobVector === undefined) {
GlobVector = new Vector2();
}
output = GlobVector;
}
if (gameObject.getBottomRight) {
return gameObject.getBottomRight(output);
}
output.x = gameObject.x - GetDisplayWidth(gameObject) * gameObject.originX + GetDisplayWidth(gameObject);
output.y = gameObject.y - GetDisplayHeight(gameObject) * gameObject.originY + GetDisplayHeight(gameObject);
return PrepareBoundsOutput(gameObject, output, includeParent);
};
var GlobVector = undefined;
var PrepareBoundsOutput = function PrepareBoundsOutput(gameObject, output, includeParent) {
if (includeParent === undefined) {
includeParent = false;
}
if (gameObject.rotation !== 0) {
RotateAround(output, gameObject.x, gameObject.y, gameObject.rotation);
}
if (includeParent && gameObject.parentContainer) {
var parentMatrix = gameObject.parentContainer.getBoundsTransformMatrix();
parentMatrix.transformPoint(output.x, output.y, output);
}
return output;
};
var IsPointInBounds = function IsPointInBounds(gameObject, x, y, preTest, postTest) {
// Can't get bounds
if (!gameObject) {
return false;
}
if (preTest && !preTest(gameObject, x, y)) {
return false;
}
var boundsRect = GetBounds(gameObject, true);
if (!boundsRect.contains(x, y)) {
return false;
}
if (postTest && !postTest(gameObject, x, y)) {
return false;
}
return true;
};
var IsPointerInBounds = function IsPointerInBounds(gameObject, pointer, preTest, postTest) {
if (pointer) {
return IsPointInBounds(gameObject, pointer.worldX, pointer.worldY, preTest, postTest);
} else {
var inputManager = gameObject.scene.input.manager;
var pointersTotal = inputManager.pointersTotal;
var pointers = inputManager.pointers;
for (var i = 0; i < pointersTotal; i++) {
pointer = pointers[i];
if (IsPointInBounds(gameObject, pointer.worldX, pointer.worldY, preTest, postTest)) {
return true;
}
}
return false;
}
};
var SceneClass = Phaser.Scene;
var IsSceneObject = function IsSceneObject(object) {
return object instanceof SceneClass;
};
var Clear = function Clear(obj) {
if (_typeof(obj) !== 'object' || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
obj.length = 0;
} else {
for (var key in obj) {
delete obj[key];
}
}
return obj;
};
var TouchGroup = /*#__PURE__*/function () {
function TouchGroup(game) {
_classCallCheck(this, TouchGroup);
if (IsSceneObject(game)) {
game = game.game;
}
this.ticker = game.loop;
this.topObjects = {};
}
_createClass(TouchGroup, [{
key: "destroy",
value: function destroy() {
this.ticker = undefined;
this.topObjects = undefined;
}
}, {
key: "isAtTop",
value: function isAtTop(groupName, key) {
var result;
var tick = this.ticker.frame;
var item = this.topObjects[groupName];
if (item) {
if (item.tick < tick) {
result = true;
} else if (item.tick === tick) {
result = key !== undefined && item.key === key;
} else {
result = false;
}
if (result) {
item.tick = tick;
item.key = key;
}
} else {
this.topObjects[groupName] = {
tick: tick,
key: key
};
result = true;
}
return result;
}
}, {
key: "clear",
value: function clear() {
Clear(this.topObjects);
return this;
}
}]);
return TouchGroup;
}();
var TouchHelperPlugin = /*#__PURE__*/function (_Phaser$Plugins$BaseP) {
_inherits(TouchHelperPlugin, _Phaser$Plugins$BaseP);
var _super = _createSuper(TouchHelperPlugin);
function TouchHelperPlugin(pluginManager) {
_classCallCheck(this, TouchHelperPlugin);
return _super.call(this, pluginManager);
}
_createClass(TouchHelperPlugin, [{
key: "start",
value: function start() {
var eventEmitter = this.game.events;
eventEmitter.on('destroy', this.destroy, this);
this.touchGroup = new TouchGroup(this.game);
}
}, {
key: "destroy",
value: function destroy() {
this.touchGroup.destroy();
_get(_getPrototypeOf(TouchHelperPlugin.prototype), "destroy", this).call(this);
}
}, {
key: "isAtTop",
value: function isAtTop(groupName, key) {
return this.touchGroup.isAtTop(groupName, key);
}
}]);
return TouchHelperPlugin;
}(Phaser.Plugins.BasePlugin);
var methods = {
hitTest: HitTest$1,
isPointerInHitArea: IsPointerInHitArea,
isPointerInBounds: IsPointerInBounds
};
// mixin
Object.assign(TouchHelperPlugin.prototype, methods);
return TouchHelperPlugin;
}));