!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).rextcrpplugin=e()}(this,function(){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function t(t,e,i){return e&&n(t.prototype,e),i&&n(t,i),t}function o(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}}),e&&i(t,e)}function u(t){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function i(t,e){return(i=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function c(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function h(n){var s=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(t){return!1}}();return function(){var t,e=u(n);if(s){var i=u(this).constructor;t=Reflect.construct(e,arguments,i)}else t=e.apply(this,arguments);return c(this,t)}}function a(t,e,i){return(a="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(t,e,i){var n=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=u(t)););return t}(t,e);if(n){var s=Object.getOwnPropertyDescriptor(n,e);return s.get?s.get.call(i):s.value}})(t,e,i||t)}function e(t){return t instanceof s}function l(t){return e(t)?t:t.scene&&e(t.scene)?t.scene:t.parent&&t.parent.scene&&e(t.parent.scene)?t.parent.scene:void 0}var s=Phaser.Scene,f={setEventEmitter:function(t,e){return void 0===e&&(e=Phaser.Events.EventEmitter),this._privateEE=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):[]}},v=Phaser.Utils.Objects.GetValue,d=function(){function i(t,e){r(this,i),this.parent=t,this._isRunning=!1,this.tickingState=!1,this.setEventEmitter(v(e,"eventEmitter",void 0)),this.setTickingMode(v(e,"tickingMode",1))}return t(i,[{key:"boot",value:function(){2!==this.tickingMode||this.tickingState||this.startTicking()}},{key:"shutdown",value:function(){this.destroyEventEmitter(),this.tickingState&&this.stopTicking()}},{key:"setTickingMode",value:function(t){"string"==typeof t&&(t=_[t]),this.tickingMode=t}},{key:"startTicking",value:function(){this.tickingState=!0}},{key:"stopTicking",value:function(){this.tickingState=!1}},{key:"start",value:function(){return this.isRunning=!0,this}},{key:"pause",value:function(){return this.isRunning=!1,this}},{key:"resume",value:function(){return this.isRunning=!0,this}},{key:"stop",value:function(){return this.isRunning=!1,this}},{key:"complete",value:function(){this.isRunning=!1,this.emit("complete",this.parent,this)}},{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()))}}]),i}();Object.assign(d.prototype,f);function p(t){return e(t)?t.events:t.on?t:void 0}function m(t,e){var i=Array.isArray(t);if(void 0===e?e=i?[]:{}:function(t){if(Array.isArray(t))t.length=0;else for(var e in t)delete t[e]}(e),i){e.length=t.length;for(var n=0,s=t.length;n<s;n++)e[n]=t[n]}else for(var r in t)e[r]=t[r];return e}function y(t,e,i,n){void 0===i&&(i=0),void 0===n&&(n=e.length),t.length=n-i;for(var s=0,r=t.length;s<r;s++)t[s]=e[s+i];return t}function k(t){return"string"!=typeof t||(""===t?t=null:x.test(t)?t=parseFloat(t):"false"===screen?t=!1:"true"===t&&(t=!0)),t}function g(t){return"[object Array]"===Object.prototype.toString.call(t)}function E(t,e,i){var n,s=P(i,"reverse",!1);if(g(t[0]))if(s)for(r=(o=t.length)-1;0<=r;r--)n=E(t[r],e,i);else for(var r=0,o=t.length;r<o;r++)n=E(t[r],e,i);else n=T(t,e,i);return n}var _={no:0,lazy:1,always:2},w=Phaser.Utils.Objects.GetValue,R=function(){o(s,d);var n=h(s);function s(t,e){var i;return r(this,s),(i=n.call(this,t,e)).parent=t,i.scene=l(t),i.resetFromJSON(e),i.boot(),i}return t(s,[{key:"resetFromJSON",value:function(t){return this.isRunning=w(t,"isRunning",!1),this.timeScale=w(t,"timeScale",1),this.now=w(t,"now",0),this}},{key:"toJSON",value:function(){return{isRunning:this.isRunning,timeScale:this.timeScale,now:this.now,tickingMode:this.tickingMode}}},{key:"boot",value:function(){a(u(s.prototype),"boot",this).call(this);var t=p(this.parent);t&&t.on("destroy",this.destroy,this)}},{key:"shutdown",value:function(){a(u(s.prototype),"shutdown",this).call(this),this.parent=void 0,this.scene=void 0}},{key:"destroy",value:function(){this.shutdown()}},{key:"startTicking",value:function(){a(u(s.prototype),"startTicking",this).call(this),this.scene.events.on("update",this.update,this)}},{key:"stopTicking",value:function(){a(u(s.prototype),"stopTicking",this).call(this),this.scene&&this.scene.events.off("update",this.update,this)}},{key:"start",value:function(t){return void 0===t&&(t=0),this.now=t,a(u(s.prototype),"start",this).call(this),this}},{key:"seek",value:function(t){return this.now=t,this}},{key:"update",value:function(t,e){return this.isRunning&&0!==this.timeScale&&(this.now+=e*this.timeScale),this}}]),s}(),S=Phaser.Utils.Objects.GetValue,b=function(){function i(t,e){r(this,i),this.parent=t,this.scene=l(t),this.clock=new R(t),this.resetFromJSON(e),this.boot()}return t(i,[{key:"resetFromJSON",value:function(t){var e=S(t,"clock",void 0);return this.clock.resetFromJSON(e),this.commands=S(t,"commands",[]),this}},{key:"toJSON",value:function(){return{clock:this.clock.toJSON(),commands:this.commands}}},{key:"boot",value:function(){var t=p(this.parent);t&&t.on("destroy",this.destroy,this)}},{key:"shutdown",value:function(){this.clock.shutdown(),this.parent=void 0,this.scene=void 0,this.commands=void 0}},{key:"destroy",value:function(){this.shutdown()}},{key:"start",value:function(t){return this.clock.start(t),this.clear(),this}},{key:"pause",value:function(){return this.clock.pause(),this}},{key:"resume",value:function(){return this.clock.resume(),this}},{key:"stop",value:function(){return this.clock.stop(),this}},{key:"seek",value:function(t){return this.clock.seek(t),this}},{key:"addCommand",value:function(t,e){if(!this.isRecording)return this;void 0===e&&(e=0);var i=this.clock.now+e;return this.commands.push([i,t]),this}},{key:"getCommands",value:function(t){return void 0===t&&(t=!1),t?this.commands:m(this.commands)}},{key:"clear",value:function(){this.commands.length=0}},{key:"isRecording",get:function(){return this.clock.isRunning}},{key:"timeScale",get:function(){return this.clock.timeScale},set:function(t){this.clock.timeScale=t}},{key:"now",get:function(){return this.clock.now}}]),i}(),O=function(t,e,i){if(t&&"number"!=typeof t){if(t.hasOwnProperty(e))return t[e];if(-1===e.indexOf("."))return i;for(var n=e.split("."),s=t,r=i,o=0;o<n.length;o++){if(!s.hasOwnProperty(n[o])){r=i;break}r=s[n[o]],s=s[n[o]]}return r}return i},x=/^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,P=Phaser.Utils.Objects.GetValue,T=function(t,e,i){var n,s=P(i,"argsConvert",void 0),r=P(i,"argsConvertScope",void 0),o=t[0];if(M=y(M,t,1),s){!0===s&&(s=k,r=void 0);for(var u=0,c=M.length;u<c;u++)M[u]=r?s.call(r,M[u],t):s(M[u],t)}return"string"==typeof o?null==(n=e[o])&&(n=P(e,o,null)):n=o,n.apply(e,M)},M=[],j=function(){o(s,d);var n=h(s);function s(t,e){var i;return r(this,s),(i=n.call(this,t,e)).parent=t,i.scene=l(t),i.resetFromJSON(e),i.boot(),i}return t(s,[{key:"resetFromJSON",value:function(t){return this.isRunning=O(t,"isRunning",!1),this.state=O(t,"state",0),this.commands=O(t,"commands",[]),this.scope=O(t,"scope",void 0),this.setTimeUnit(O(t,"timeUnit",0)),this.setDtMode(O(t,"dtMode",0)),this.setTimeScale(O(t,"timeScale",1)),this.index=O(t,"index",0),this.nextDt=O(t,"nextDt",0),this.seek(O(t,"now",0)),this.timeScale=O(t,"timeScale",1),this}},{key:"toJSON",value:function(){return{isRunning:this.isRunning,state:this.state,commands:this.commands,scope:this.scope,timeUnit:this.timeUnit,dtMode:this.dtMode,index:this.index,nextDt:this.nextDt,now:this.now,timeScale:this.timeScale,tickingMode:this.tickingMode}}},{key:"boot",value:function(){a(u(s.prototype),"boot",this).call(this);var t=p(this.parent);t&&t.on("destroy",this.destroy,this)}},{key:"shutdown",value:function(){a(u(s.prototype),"shutdown",this).call(this),this.parent=void 0,this.scene=void 0,this.commands=void 0}},{key:"destroy",value:function(){this.shutdown()}},{key:"startTicking",value:function(){a(u(s.prototype),"startTicking",this).call(this),this.scene.events.on("update",this.update,this)}},{key:"stopTicking",value:function(){a(u(s.prototype),"stopTicking",this).call(this),this.scene&&this.scene.events.off("update",this.update,this)}},{key:"load",value:function(t,e,i){this.stop();var n=O(i,"timeUnit",void 0);void 0!==n&&this.setTimeUnit(n);var s=O(i,"dtMode",void 0);return void 0!==s&&this.setDtMode(s),t=t.filter(function(t){var e=t[0];return!isNaN(e)}).map(function(t){return"string"==typeof t[0]&&(t[0]=parseFloat(t[0])),t}),0===this.dtMode&&t.sort(function(t,e){var i=t[0],n=e[0];return n<i?1:i<n?-1:0}),this.commands=t,this.scope=e,this}},{key:"start",value:function(t){return void 0===t&&(t=0),this.stop(),this.index=0,this.isRunning=!0,this.state=1,this.nextDt=this.getNextDt(0),this.seek(t),this.update(),this}},{key:"pause",value:function(){return this.isRunning=!1,this}},{key:"resume",value:function(){return this.isRunning=!0,this}},{key:"stop",value:function(){return this.isRunning=!1,this.state=0,this}},{key:"seek",value:function(t){return this.now=t,this}},{key:"setTimeScale",value:function(t){this.timeScale=t}},{key:"update",value:function(t,e){if(!this.isRunning)return this;if(void 0!==t){if(0===this.timeScale||0===e)return this;this.now+=e*this.timeScale}if(this.nextDt>this.now)return this;for(;;){var i=this.commands[this.index],n=i[1];if(g(n)||(n=y(D,i,1)),E(n,this.scope),this.emit("runcommand",n,this.scope),this.index===this.commands.length-1)return this.complete(),this;if(this.index++,this.nextDt=this.getNextDt(this.nextDt),this.nextDt>this.now)return this}}},{key:"complete",value:function(){a(u(s.prototype),"complete",this).call(this),this.state=2}},{key:"getNextDt",value:function(t){var e=this.commands[this.index][0];return 1===this.timeUnit&&(e*=1e3),1===this.dtMode&&(e+=t),e}},{key:"setDtMode",value:function(t){return"string"==typeof t&&(t=U[t]),this.dtMode=t,this}},{key:"setTimeUnit",value:function(t){return"string"==typeof t&&(t=N[t]),this.timeUnit=t,this}},{key:"isPlaying",get:function(){return this.isRunning}},{key:"completed",get:function(){return 2===this.state}}]),s}(),D=[],N={ms:0,s:1,sec:1},U={abs:0,absolute:0,inc:1,increment:1},J={Recorder:b,Player:j,RunCommands:E},C=J.Recorder,F=J.Player,A=function(){o(i,Phaser.Plugins.BasePlugin);var e=h(i);function i(t){return r(this,i),e.call(this,t)}return t(i,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this)}},{key:"addRecorder",value:function(t,e){return new C(t,e)}},{key:"addPlayer",value:function(t,e){return new F(t,e)}}]),i}(),L={runCommands:J.RunCommands};return Object.assign(A.prototype,L),A});
