UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 11.6 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexloopinticksplugin=e();}(undefined,(function(){function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,(s=i.key,r=void 0,"symbol"==typeof(r=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,e||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(s,"string"))?r:String(r)),i);}var s,r;}function i(t,e,i){return e&&n(t.prototype,e),i&&n(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&o(t,e);}function r(t){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},r(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function u(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function h(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return !1}}();return function(){var n,i=r(t);if(e){var s=r(this).constructor;n=Reflect.construct(i,arguments,s);}else n=i.apply(this,arguments);return u(this,n)}}function c(){return c="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var i=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=r(t)););return t}(t,e);if(i){var s=Object.getOwnPropertyDescriptor(i,e);return s.get?s.get.call(arguments.length<3?t:n):s.value}},c.apply(this,arguments)}var a={setEventEmitter:function(t,e){return void 0===e&&(e=Phaser.Events.EventEmitter),this._privateEE=!0===t||void 0===t,this._eventEmitter=this._privateEE?new e:t,this},destroyEventEmitter:function(){return this._eventEmitter&&this._privateEE&&this._eventEmitter.shutdown(),this},getEventEmitter:function(){return this._eventEmitter},on:function(){return this._eventEmitter&&this._eventEmitter.on.apply(this._eventEmitter,arguments),this},once:function(){return this._eventEmitter&&this._eventEmitter.once.apply(this._eventEmitter,arguments),this},off:function(){return this._eventEmitter&&this._eventEmitter.off.apply(this._eventEmitter,arguments),this},emit:function(t){return this._eventEmitter&&t&&this._eventEmitter.emit.apply(this._eventEmitter,arguments),this},addListener:function(){return this._eventEmitter&&this._eventEmitter.addListener.apply(this._eventEmitter,arguments),this},removeListener:function(){return this._eventEmitter&&this._eventEmitter.removeListener.apply(this._eventEmitter,arguments),this},removeAllListeners:function(){return this._eventEmitter&&this._eventEmitter.removeAllListeners.apply(this._eventEmitter,arguments),this},listenerCount:function(){return this._eventEmitter?this._eventEmitter.listenerCount.apply(this._eventEmitter,arguments):0},listeners:function(){return this._eventEmitter?this._eventEmitter.listeners.apply(this._eventEmitter,arguments):[]},eventNames:function(){return this._eventEmitter?this._eventEmitter.eventNames.apply(this._eventEmitter,arguments):[]}},l=Phaser.Scene,f=function(t){return t instanceof l},p=Phaser.Game,d=function(t){return t instanceof p},v=Phaser.Utils.Objects.GetValue,y=function(){function n(t,i){e(this,n),this.setParent(t),this.isShutdown=!1,this.setEventEmitter(v(i,"eventEmitter",!0)),this.parent&&(this.parent===this.scene?this.scene.sys.events.once("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.once("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.once("destroy",this.onParentDestroy,this));}return i(n,[{key:"shutdown",value:function(t){this.isShutdown||(this.parent&&(this.parent===this.scene?this.scene.sys.events.off("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.off("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.off("destroy",this.onParentDestroy,this)),this.destroyEventEmitter(),this.parent=void 0,this.scene=void 0,this.game=void 0,this.isShutdown=!0);}},{key:"destroy",value:function(t){this.shutdown(t);}},{key:"onEnvDestroy",value:function(){this.destroy(!0);}},{key:"onParentDestroy",value:function(t,e){this.destroy(e);}},{key:"setParent",value:function(e){var n;return this.parent=e,this.scene=null==(n=e)||"object"!==t(n)?null:f(n)?n:n.scene&&f(n.scene)?n.scene:n.parent&&n.parent.scene&&f(n.parent.scene)?n.parent.scene:null,this.game=function(e){return null==e||"object"!==t(e)?null:d(e)?e:d(e.game)?e.game:f(e)?e.sys.game:f(e.scene)?e.scene.sys.game:void 0}(e),this}}]),n}();Object.assign(y.prototype,a);var g=Phaser.Utils.Objects.GetValue,m=function(t){s(o,t);var n=h(o);function o(t,i){var s;return e(this,o),(s=n.call(this,t,i))._isRunning=!1,s.isPaused=!1,s.tickingState=!1,s.setTickingMode(g(i,"tickingMode",1)),s}return i(o,[{key:"boot",value:function(){2!==this.tickingMode||this.tickingState||this.startTicking();}},{key:"shutdown",value:function(t){this.isShutdown||(this.stop(),this.tickingState&&this.stopTicking(),c(r(o.prototype),"shutdown",this).call(this,t));}},{key:"setTickingMode",value:function(t){"string"==typeof t&&(t=k[t]),this.tickingMode=t;}},{key:"startTicking",value:function(){this.tickingState=!0;}},{key:"stopTicking",value:function(){this.tickingState=!1;}},{key:"isRunning",get:function(){return this._isRunning},set:function(t){this._isRunning!==t&&(this._isRunning=t,1===this.tickingMode&&t!=this.tickingState&&(t?this.startTicking():this.stopTicking()));}},{key:"start",value:function(){return this.isPaused=!1,this.isRunning=!0,this}},{key:"pause",value:function(){return this.isRunning&&(this.isPaused=!0,this.isRunning=!1),this}},{key:"resume",value:function(){return this.isPaused&&(this.isRunning=!0),this}},{key:"stop",value:function(){return this.isPaused=!1,this.isRunning=!1,this}},{key:"complete",value:function(){this.isPaused=!1,this.isRunning=!1,this.emit("complete",this.parent,this);}}]),o}(y),k={no:0,lazy:1,always:2},E=function(){function t(n,i,s,r,o){e(this,t),this.key=n,this.start=i,this.end=s,this.step=r,this.items=o,this._current=i;}return i(t,[{key:"reset",value:function(){this._current=this.start;}},{key:"isEnd",get:function(){return this.step>=0?this._current>=this.end:this._current<=this.end}},{key:"length",get:function(){return this.step>=0&&this.start>this.end||this.step<0&&this.start<this.end?0:Math.floor(this.end-this.start)+1}},{key:"next",value:function(){return this.isEnd?this._current=this.start:this._current+=this.step,this}},{key:"current",get:function(){return this.items?this.items[this._current]:this._current}}]),t}(),b=function(){function t(){e(this,t),this.indexes=[],this.length=0,this.reset();}return i(t,[{key:"reset",value:function(){for(var t=0,e=this.indexes.length;t<e;t++)this.indexes[t].reset();return this.firstPass=!0,this.currentCount=0,this}},{key:"addNumberLoop",value:function(t,e,n,i){return void 0===i&&(i=n>=e?1:-1),this.indexes.push(new E(t,e,n,i)),this.length=this._getLength(),this}},{key:"addItemsLoop",value:function(t,e,n){void 0===n&&(n=!1);var i=e.length-1,s=n?i:0,r=n?0:i,o=n?-1:1;return this.indexes.push(new E(t,s,r,o,e)),this.length=this._getLength(),this}},{key:"addLoop",value:function(t){return this.indexes.push(new E(t.key,t.start,t.end,t.step,t.items)),this.length=this._getLength(),this}},{key:"removeLoops",value:function(){return this.indexes.length=0,this.length=0,this}},{key:"_getLength",value:function(){for(var t=void 0,e=0,n=this.indexes.length;e<n;e++)void 0===t?t=this.indexes[e].length:t*=this.indexes[e].length;return void 0===t?0:t}},{key:"progress",get:function(){return this.currentCount/this.length}},{key:"isEnd",get:function(){for(var t=this.indexes.length-1;t>=0;t--)if(!this.indexes[t].isEnd)return !1;return !0}},{key:"next",value:function(){for(var t,e,n=this.indexes.length-1;n>=0&&(e=(t=this.indexes[n]).isEnd,t.next(),e);n--);return this}},{key:"getCurrent",value:function(t){var e;void 0===t&&(t={});for(var n=this.indexes.length-1;n>=0;n--)t[(e=this.indexes[n]).key]=e.current;return t}},{key:"getNext",value:function(t){return this.firstPass?this.firstPass=!1:this.next(),this.getCurrent(t),this.currentCount++,t}}]),t}(),_=Phaser.Utils.Objects.GetValue,P=function(n){s(u,n);var o=h(u);function u(t,n){var i;return e(this,u),(i=o.call(this,t,n)).deltaPeriod=1e3/t.game.loop.targetFps,i.deltaPercentage=1,i.loopIndexGenerator=new b,i.currentIndexes={},i.resetFromJSON(n),i.boot(),i}return i(u,[{key:"resetFromJSON",value:function(e){return this.setCallback(_(e,"callback",this.callback),_(e,"scope",this.scope)),this.setDeltaPercentage(_(e,"deltaPercentage",this.deltaPercentage)),this.loopIndexGenerator.reset(),function(e){if("object"!==t(e)||null===e)return e;if(Array.isArray(e))e.length=0;else for(var n in e)delete e[n];}(this.currentIndexes),this}},{key:"startTicking",value:function(){c(r(u.prototype),"startTicking",this).call(this),this.scene.sys.events.on("preupdate",this.preupdate,this);}},{key:"stopTicking",value:function(){c(r(u.prototype),"stopTicking",this).call(this),this.scene&&this.scene.sys.events.off("preupdate",this.preupdate,this);}},{key:"setCallback",value:function(t,e){return this.callback=t,this.scope=e,this}},{key:"setDeltaPercentage",value:function(t){return this.deltaPercentage=t,this}},{key:"addNumberLoop",value:function(t,e,n,i){return this.loopIndexGenerator.addNumberLoop(t,e,n,i),this}},{key:"addItemsLoop",value:function(t,e,n){return this.loopIndexGenerator.addItemsLoop(t,e,n),this}},{key:"addLoop",value:function(t){return this.loopIndexGenerator.addLoop(t),this}},{key:"curTime",get:function(){return (new Date).getTime()}},{key:"progress",get:function(){return this.loopIndexGenerator.progress}},{key:"preupdate",value:function(t,e){if(this.isRunning&&this.callback){var n,i=this.curTime,s=this.deltaPeriod*this.deltaPercentage;this.emit("tickstart",this);do{if(this.loopIndexGenerator.isEnd)return void this.complete();this.currentIndexes=this.loopIndexGenerator.getNext(this.currentIndexes),this.scope?this.callback.call(this.scope,this.currentIndexes,this):this.callback(this.currentIndexes,this),n=this.curTime-i>=s;}while(!n);this.emit("tickend",this);}}}]),u}(m);return function(t){s(r,Phaser.Plugins.BasePlugin);var n=h(r);function r(t){return e(this,r),n.call(this,t)}return i(r,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"add",value:function(t,e){return new P(t,e)}}]),r}()}));