phaser3-rex-plugins
Version:
649 lines (618 loc) • 20.5 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.rexcoverplugin = 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 _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 Rectangle = Phaser.GameObjects.Rectangle;
var FullWindowRectangle = /*#__PURE__*/function (_Rectangle) {
_inherits(FullWindowRectangle, _Rectangle);
var _super = _createSuper(FullWindowRectangle);
function FullWindowRectangle(scene, color, alpha) {
var _this;
_classCallCheck(this, FullWindowRectangle);
_this = _super.call(this, scene, 0, 0, 2, 2, color, 1);
_this.setAlpha(alpha);
_this.setScrollFactor(0);
_this.boot();
return _this;
}
_createClass(FullWindowRectangle, [{
key: "boot",
value: function boot() {
var scene = this.scene;
scene.sys.events.on('prerender', this.resize, this);
}
}, {
key: "destroy",
value: function destroy(fromScene) {
// preDestroy method does not have fromScene parameter
// This Game Object has already been destroyed
if (!this.scene || this.ignoreDestroy) {
return;
}
this.scene.sys.events.off('prerender', this.resize, this);
_get(_getPrototypeOf(FullWindowRectangle.prototype), "destroy", this).call(this, fromScene);
}
}, {
key: "tint",
get: function get() {
return this.fillColor;
},
set: function set(value) {
this.setFillStyle(value, this.fillAlpha);
}
}, {
key: "resize",
value: function resize() {
var scene = this.scene;
var gameSize = scene.sys.scale.gameSize;
var camera = scene.sys.cameras.main;
var gameWidth = gameSize.width,
gameHeight = gameSize.height,
scale = 1 / camera.zoom;
var x = gameWidth / 2,
y = gameHeight / 2,
width = gameWidth * scale,
height = gameHeight * scale;
if (this.x !== x || this.y !== y) {
this.setPosition(x, y);
}
if (this.width !== width || this.height !== height) {
this.setSize(width, height).setOrigin(0.5);
}
}
}]);
return FullWindowRectangle;
}(Rectangle);
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;
} else {
return null;
}
};
var GameClass = Phaser.Game;
var IsGame = function IsGame(object) {
return object instanceof GameClass;
};
var GetGame = function GetGame(object) {
if (object == null || _typeof(object) !== 'object') {
return null;
} else if (IsGame(object)) {
return object;
} else if (IsGame(object.game)) {
return object.game;
} else if (IsSceneObject(object)) {
// object = scene object
return object.sys.game;
} else if (IsSceneObject(object.scene)) {
// object = game object
return object.scene.sys.game;
}
};
var GetValue$2 = Phaser.Utils.Objects.GetValue;
var ComponentBase = /*#__PURE__*/function () {
function ComponentBase(parent, config) {
_classCallCheck(this, ComponentBase);
this.setParent(parent); // gameObject, scene, or game
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) {
if (this.parent === this.scene) {
// parent is a scene
this.scene.sys.events.once('shutdown', this.onEnvDestroy, this);
} else if (this.parent === this.game) {
// parent is game
this.game.events.once('shutdown', this.onEnvDestroy, this);
} else if (this.parent.once) {
// parent is game object or something else
this.parent.once('destroy', this.onParentDestroy, this);
}
// bob object does not have event emitter
}
}
_createClass(ComponentBase, [{
key: "shutdown",
value: function shutdown(fromScene) {
// Already shutdown
if (this.isShutdown) {
return;
}
// parent might not be shutdown yet
if (this.parent) {
if (this.parent === this.scene) {
// parent is a scene
this.scene.sys.events.off('shutdown', this.onEnvDestroy, this);
} else if (this.parent === this.game) {
// parent is game
this.game.events.off('shutdown', this.onEnvDestroy, this);
} else if (this.parent.once) {
// parent is game object or something else
this.parent.off('destroy', this.onParentDestroy, this);
}
// bob object does not have event emitter
}
this.destroyEventEmitter();
this.parent = undefined;
this.scene = undefined;
this.game = undefined;
this.isShutdown = true;
}
}, {
key: "destroy",
value: function destroy(fromScene) {
this.shutdown(fromScene);
}
}, {
key: "onEnvDestroy",
value: function onEnvDestroy() {
this.destroy(true);
}
}, {
key: "onParentDestroy",
value: function onParentDestroy(parent, fromScene) {
this.destroy(fromScene);
}
}, {
key: "setParent",
value: function setParent(parent) {
this.parent = parent; // gameObject, scene, or game
this.scene = GetSceneObject(parent);
this.game = GetGame(parent);
return this;
}
}]);
return ComponentBase;
}();
Object.assign(ComponentBase.prototype, EventEmitterMethods);
var GetValue$1 = Phaser.Utils.Objects.GetValue;
var TouchEventStop = /*#__PURE__*/function (_ComponentBase) {
_inherits(TouchEventStop, _ComponentBase);
var _super = _createSuper(TouchEventStop);
function TouchEventStop(gameObject, config) {
var _this;
_classCallCheck(this, TouchEventStop);
_this = _super.call(this, gameObject, {
eventEmitter: false
});
// No event emitter
// this.parent = gameObject;
_this.resetFromJSON(config);
_this.boot();
return _this;
}
_createClass(TouchEventStop, [{
key: "resetFromJSON",
value: function resetFromJSON(o) {
this.setHitAreaMode(GetValue$1(o, 'hitAreaMode', 0));
this.setEnable(GetValue$1(o, 'enable', true));
this.setStopMode(GetValue$1(o, 'stopAllLevels', true));
return this;
}
}, {
key: "boot",
value: function boot() {
this.parent.on('pointerdown', function (pointer, localX, localY, event) {
if (this.stopAllLevels) {
event.stopPropagation();
}
}, this).on('pointerup', function (pointer, localX, localY, event) {
if (this.stopAllLevels) {
event.stopPropagation();
}
}, this).on('pointermove', function (pointer, localX, localY, event) {
if (this.stopAllLevels) {
event.stopPropagation();
}
}, this).on('pointerover', function (pointer, localX, localY, event) {
if (this.stopAllLevels) {
event.stopPropagation();
}
}, this).on('pointerout', function (pointer, event) {
if (this.stopAllLevels) {
event.stopPropagation();
}
}, this);
}
}, {
key: "setHitAreaMode",
value: function setHitAreaMode(mode) {
if (typeof mode === 'string') {
mode = HitAreaMode[mode];
}
var gameObject = this.parent;
if (gameObject.input) {
gameObject.removeInteractive();
}
if (mode === 0) {
gameObject.setInteractive();
} else {
gameObject.setInteractive({
hitArea: {},
hitAreaCallback: function hitAreaCallback() {
return true;
}
});
}
return this;
}
}, {
key: "setEnable",
value: function setEnable(e) {
if (e === undefined) {
e = true;
}
if (e) {
this.parent.setInteractive();
} else {
this.parent.disableInteractive();
}
this.enable = e;
return this;
}
}, {
key: "setStopMode",
value: function setStopMode(allLevels) {
if (allLevels === undefined) {
allLevels = true;
}
this.stopAllLevels = allLevels;
return this;
}
}, {
key: "toggleEnable",
value: function toggleEnable() {
this.setEnable(!this.enable);
return this;
}
}]);
return TouchEventStop;
}(ComponentBase);
var HitAreaMode = {
"default": 0,
fullWindow: 1
};
var GetValue = Phaser.Utils.Objects.GetValue;
var Cover = /*#__PURE__*/function (_FullWindowRectangle) {
_inherits(Cover, _FullWindowRectangle);
var _super = _createSuper(Cover);
function Cover(scene, config) {
var _this;
_classCallCheck(this, Cover);
var fillColor = GetValue(config, 'color', 0x0);
var fillAlpha = GetValue(config, 'alpha', 0.8);
_this = _super.call(this, scene, fillColor, fillAlpha);
_this.touchEventStop = new TouchEventStop(_assertThisInitialized(_this), {
hitAreaMode: 1
});
return _this;
}
return _createClass(Cover);
}(FullWindowRectangle);
function Factory (config) {
var gameObject = new Cover(this.scene, config);
this.scene.add.existing(gameObject);
return gameObject;
}
var BuildGameObject = Phaser.GameObjects.BuildGameObject;
function Creator (config, addToScene) {
if (config === undefined) {
config = {};
}
if (addToScene !== undefined) {
config.add = addToScene;
}
var gameObject = new Cover(this.scene, config);
BuildGameObject(this.scene, gameObject, config);
return gameObject;
}
var IsInValidKey = function IsInValidKey(keys) {
return keys == null || keys === '' || keys.length === 0;
};
var GetEntry = function GetEntry(target, keys, defaultEntry) {
var entry = target;
if (IsInValidKey(keys)) ; else {
if (typeof keys === 'string') {
keys = keys.split('.');
}
var key;
for (var i = 0, cnt = keys.length; i < cnt; i++) {
key = keys[i];
if (entry[key] == null || _typeof(entry[key]) !== 'object') {
var newEntry;
if (i === cnt - 1) {
if (defaultEntry === undefined) {
newEntry = {};
} else {
newEntry = defaultEntry;
}
} else {
newEntry = {};
}
entry[key] = newEntry;
}
entry = entry[key];
}
}
return entry;
};
var SetValue = function SetValue(target, keys, value, delimiter) {
if (delimiter === undefined) {
delimiter = '.';
}
// no object
if (_typeof(target) !== 'object') {
return;
}
// invalid key
else if (IsInValidKey(keys)) {
// don't erase target
if (value == null) {
return;
}
// set target to another object
else if (_typeof(value) === 'object') {
target = value;
}
} else {
if (typeof keys === 'string') {
keys = keys.split(delimiter);
}
var lastKey = keys.pop();
var entry = GetEntry(target, keys);
entry[lastKey] = value;
}
return target;
};
var CoverPlugin = /*#__PURE__*/function (_Phaser$Plugins$BaseP) {
_inherits(CoverPlugin, _Phaser$Plugins$BaseP);
var _super = _createSuper(CoverPlugin);
function CoverPlugin(pluginManager) {
var _this;
_classCallCheck(this, CoverPlugin);
_this = _super.call(this, pluginManager);
// Register our new Game Object type
pluginManager.registerGameObject('rexCover', Factory, Creator);
return _this;
}
_createClass(CoverPlugin, [{
key: "start",
value: function start() {
var eventEmitter = this.game.events;
eventEmitter.on('destroy', this.destroy, this);
}
}]);
return CoverPlugin;
}(Phaser.Plugins.BasePlugin);
SetValue(window, 'RexPlugins.GameObjects.Cover', Cover);
return CoverPlugin;
}));