UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 8 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).rexlocalmaskplugin=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 r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(i=r.key,s=void 0,"symbol"==typeof(s=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(i,"string"))?s:String(s)),r);}var i,s;}function r(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function i(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 s(t){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},s(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){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function a(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,r=s(t);if(e){var i=s(this).constructor;n=Reflect.construct(r,arguments,i);}else n=r.apply(this,arguments);return function(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 u(t)}(this,n)}}function c(){return c="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var r=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=s(t)););return t}(t,e);if(r){var i=Object.getOwnPropertyDescriptor(r,e);return i.get?i.get.call(arguments.length<3?t:n):i.value}},c.apply(this,arguments)}var l={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):[]}},h=Phaser.Scene,f=function(t){return t instanceof h},p=Phaser.Game,v=function(t){return t instanceof p},m=Phaser.Utils.Objects.GetValue,y=function(){function n(t,r){e(this,n),this.setParent(t),this.isShutdown=!1,this.setEventEmitter(m(r,"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 r(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:v(e)?e:v(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,l);var d="localMask",E=function(t){i(o,Phaser.Renderer.WebGL.Pipelines.PreFXPipeline);var n=a(o);function o(t){return e(this,o),n.call(this,{game:t,fragShader:"\nprecision mediump float;\nuniform sampler2D uMainSampler;\nuniform sampler2D uMaskSampler;\nvarying vec2 outTexCoord;\n\nvoid main ()\n{\n vec4 color = texture2D(uMainSampler, outTexCoord);\n vec4 maskColor = texture2D(uMaskSampler, outTexCoord);\n gl_FragColor = vec4(color.rgb * maskColor.a, color.a * maskColor.a);\n}\n"})}return r(o,[{key:"onDraw",value:function(t){var e=this.tempSprite[d].maskGLTexture;this.set1i("uMainSampler",0),this.set1i("uMaskSampler",1),this.bindTexture(e,1),c(s(o.prototype),"onDraw",this).call(this,t);}}],[{key:"setControllerKey",value:function(t){d=t;}}]),o}(),b=Phaser.Utils.Objects.GetValue,_="RexLocalMaskFx",g="rexLocalMask",k=function(t){i(o,t);var n=a(o);function o(t,r){var i;e(this,o);var s=(i=n.call(this,t,{eventEmitter:!1})).scene,a=s.sys.renderer.pipelines,c=a.get(_);return c||(g=b(r,"controllerKey",g),E.setControllerKey(g),c=a.add(_,new E(s.game))),i.pipelineInstance=c,i.textures=s.sys.textures,i.parent[g]=u(i),i.setMaskTexture(b(r,"key"),b(r,"frame")),i.setEnable(b(r,"enable",!0)),i}return r(o,[{key:"shutdown",value:function(t){this.pipelineInstance=void 0,this.textures=void 0,this.maskFrame=void 0,this.maskGLTexture=void 0,c(s(o.prototype),"shutdown",this).call(this,t);}},{key:"controllerKey",get:function(){return g}},{key:"enable",get:function(){return this._enable},set:function(t){if(t!==this._enable){this._enable=t;var e=this.parent,n=e.pipeline;t?n!==this.pipelineInstance&&e.setPipeline(this.pipelineInstance):n===this.pipelineInstance&&e.resetPipeline();}}},{key:"setEnable",value:function(t){return void 0===t&&(t=!0),this.enable=t,this}},{key:"setMaskTexture",value:function(t,e){return this.maskKey=t,this.maskFrameName=e,this.maskFrame=this.textures.getFrame(t,e),this.maskGLTexture=this.maskFrame.glTexture,this}}]),o}(y);return function(t){i(s,Phaser.Plugins.BasePlugin);var n=a(s);function s(t){return e(this,s),n.call(this,t)}return r(s,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"add",value:function(t,e){return new k(t,e)}}]),s}()}));