phaser3-rex-plugins
Version:
2 lines (1 loc) • 12.7 kB
JavaScript
!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).rexshatterimageplugin=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 r(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,(i=n.key,s=void 0,"symbol"==typeof(s=function(t,e){if("object"!=typeof t||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(i,"string"))?s:String(s)),n);}var i,s;}function n(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),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 a(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 u(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 r,n=s(t);if(e){var i=s(this).constructor;r=Reflect.construct(n,arguments,i);}else r=n.apply(this,arguments);return a(this,r)}}function h(){return h="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,r){var n=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=s(t)););return t}(t,e);if(n){var i=Object.getOwnPropertyDescriptor(n,e);return i.get?i.get.call(arguments.length<3?t:r):i.value}},h.apply(this,arguments)}var c=function(t){i(s,t);var r=u(s);function s(){return e(this,s),r.apply(this,arguments)}return n(s,[{key:"tint",get:function(){return 0===this.vertices.length?16777215:this.vertices[0].color}},{key:"setInteractive",value:function(){var t=function(t,e,r){for(var n=this.faces,i=0;i<n.length;i++){if(n[i].contains(e,r))return !0}return !1}.bind(this);return this.scene.sys.input.enable(this,t),this}},{key:"forceUpdate",value:function(){return this.dirtyCache[10]=1,this}}]),s}(Phaser.GameObjects.Mesh);function l(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var f={exports:{}};!function(t){var e;!function(){var r=1/1048576;function n(t,e,n,i){var s,o,a,u,h,c,l,f,v,y,p=t[e][0],g=t[e][1],d=t[n][0],m=t[n][1],b=t[i][0],P=t[i][1],w=Math.abs(g-m),x=Math.abs(m-P);if(w<r&&x<r)throw new Error("Eek! Coincident points!");return w<r?o=(u=-(b-d)/(P-m))*((s=(d+p)/2)-(c=(d+b)/2))+(f=(m+P)/2):x<r?o=(a=-(d-p)/(m-g))*((s=(b+d)/2)-(h=(p+d)/2))+(l=(g+m)/2):(s=((a=-(d-p)/(m-g))*(h=(p+d)/2)-(u=-(b-d)/(P-m))*(c=(d+b)/2)+(f=(m+P)/2)-(l=(g+m)/2))/(a-u),o=w>x?a*(s-h)+l:u*(s-c)+f),{i:e,j:n,k:i,x:s,y:o,r:(v=d-s)*v+(y=m-o)*y}}function i(t){var e,r,n,i,s,o;for(r=t.length;r;)for(i=t[--r],n=t[--r],e=r;e;)if(o=t[--e],n===(s=t[--e])&&i===o||n===o&&i===s){t.splice(r,2),t.splice(e,2);break}}e={triangulate:function(t,e){var s,o,a,u,h,c,l,f,v,y,p,g,d=t.length;if(d<3)return [];if(t=t.slice(0),e)for(s=d;s--;)t[s]=t[s][e];for(a=new Array(d),s=d;s--;)a[s]=s;for(a.sort((function(e,r){return t[r][0]-t[e][0]})),u=function(t){var e,r,n,i,s,o,a=Number.POSITIVE_INFINITY,u=Number.POSITIVE_INFINITY,h=Number.NEGATIVE_INFINITY,c=Number.NEGATIVE_INFINITY;for(e=t.length;e--;)t[e][0]<a&&(a=t[e][0]),t[e][0]>h&&(h=t[e][0]),t[e][1]<u&&(u=t[e][1]),t[e][1]>c&&(c=t[e][1]);return n=c-u,[[(s=a+.5*(r=h-a))-20*(i=Math.max(r,n)),(o=u+.5*n)-i],[s,o+20*i],[s+20*i,o-i]]}(t),t.push(u[0],u[1],u[2]),h=[n(t,d+0,d+1,d+2)],c=[],l=[],s=a.length;s--;l.length=0){for(g=a[s],o=h.length;o--;)(f=t[g][0]-h[o].x)>0&&f*f>h[o].r?(c.push(h[o]),h.splice(o,1)):f*f+(v=t[g][1]-h[o].y)*v-h[o].r>r||(l.push(h[o].i,h[o].j,h[o].j,h[o].k,h[o].k,h[o].i),h.splice(o,1));for(i(l),o=l.length;o;)p=l[--o],y=l[--o],h.push(n(t,y,p,g));}for(s=h.length;s--;)c.push(h[s]);for(h.length=0,s=c.length;s--;)c[s].i<d&&c[s].j<d&&c[s].k<d&&h.push(c[s].i,c[s].j,c[s].k);return h},contains:function(t,e){if(e[0]<t[0][0]&&e[0]<t[1][0]&&e[0]<t[2][0]||e[0]>t[0][0]&&e[0]>t[1][0]&&e[0]>t[2][0]||e[1]<t[0][1]&&e[1]<t[1][1]&&e[1]<t[2][1]||e[1]>t[0][1]&&e[1]>t[1][1]&&e[1]>t[2][1])return null;var r=t[1][0]-t[0][0],n=t[2][0]-t[0][0],i=t[1][1]-t[0][1],s=t[2][1]-t[0][1],o=r*s-n*i;if(0===o)return null;var a=(s*(e[0]-t[0][0])-n*(e[1]-t[0][1]))/o,u=(r*(e[1]-t[0][1])-i*(e[0]-t[0][0]))/o;return a<0||u<0||a+u>1?null:[a,u]}},t.exports=e;}();}(f);var v=l(f.exports),y=Phaser.Geom.Triangle,p=function(t,e){void 0===e&&(e=!0);var r=v.triangulate(t);if(e){for(var n=[],i=0,s=r.length;i<s;i+=3){var o=t[r[i+0]],a=t[r[i+1]],u=t[r[i+2]],h=new y(o[0],o[1],a[0],a[1],u[0],u[1]);n.push(h);}return n}return {vertices:t,indices:r}},g=Phaser.Utils.Objects.GetValue,d=Phaser.Math.Clamp,m=[1/27,3/27,9/27],b=function(t,e,r,n,i,s,o,a,u,h,c,l){var f=[];f.push([t,e]);for(var v=Math.min(r,n),y=0,p=i.length;y<p;y++)w(f,t,e,v*i[y],s,o,a,u,h,c,l);v=2*Math.max(r,n);return w(f,t,e,v,s,o,a,u,h,c,l),f},P=2*Math.PI,w=function(t,e,r,n,i,s,o,a,u,h,c){for(var l=0;l<i;l++){var f=l/i*P,v=e+Math.cos(f)*n*x(s,o),y=r+Math.sin(f)*n*x(s,o);v=d(v,a,u),y=d(y,h,c),t.push([v,y]);}return t},x=function(t,e){return t===e?t:t+(e-t)*Math.random()},O=Phaser.Geom.Mesh.Face,j=Phaser.Math.DegToRad,R=Phaser.Math.RadToDeg,k=Phaser.Geom.Mesh.RotateFace,I=function(t){i(s,t);var r=u(s);function s(t,n,i){var o;return e(this,s),(o=r.call(this,t,n,i))._rotation=0,o}return n(s,[{key:"rotation",get:function(){return this._rotation},set:function(t){k(this,t-this._rotation),this._rotation=t;}},{key:"setRotation",value:function(t){return this.rotation=t,this}},{key:"angle",get:function(){return R(this.rotation)},set:function(t){this.rotation=j(t);}},{key:"setAngle",value:function(t){return this.angle=t,this}},{key:"setAlpha",value:function(t){return this.alpha=t,this}},{key:"tint",get:function(){var t=this.vertex1.color,e=this.vertex2.color,r=this.vertex3.color;return (((t>>0&255)+(e>>0&255)+(r>>0&255))/3<<0)+(((t>>8&255)+(e>>8&255)+(r>>8&255))/3<<8)+(((t>>16&255)+(e>>16&255)+(r>>16&255))/3<<16)},set:function(t){this.vertex1.color=t,this.vertex2.color=t,this.vertex3.color=t;}},{key:"setTint",value:function(t){return this.tint=t,this}}]),s}(O),G=Phaser.Math.RotateAround,T={x:0,y:0},S=Phaser.Utils.Objects.IsPlainObject,M=Phaser.Utils.Objects.GetValue,_=Phaser.Geom.Mesh.GenerateGridVerts,N=Phaser.Geom.Mesh.Vertex,C=Phaser.Math.Distance.Squared,E=[1/27,3/27,9/27],V=function(t){i(s,t);var r=u(s);function s(t,n,i,o,a,u){var h;return e(this,s),S(n)&&(n=M(u=n,"x",0),i=M(u,"y",0),o=M(u,"key",null),a=M(u,"frame",null)),(h=r.call(this,t,n,i,o,a)).type="rexShatterImage",h.hideCCW=!1,h.resetImage(),h.shatterCenter={x:null,y:null},h.setRingRadiusList(M(u,"ringRadiusList",E)),h.setSamplesPerRing(M(u,"samplesPerRing",12)),h.setVariation(M(u,"variation",.25)),h}return n(s,[{key:"setRingRadiusList",value:function(t){return this.ringRadiusList=t,this}},{key:"setSamplesPerRing",value:function(t){return this.samplesPerRing=t,this}},{key:"setVariation",value:function(t){return this.variation=t,this}},{key:"resetImage",value:function(){return this.setSizeToFrame(),this.clear(),this.dirtyCache[9]=-1,_({mesh:this,width:this.frame.cutWidth/this.height,height:this.frame.cutHeight/this.height}),this.setOrtho(this.width/this.height,1),this}},{key:"shatter",value:function(t,e,r){if(S(t)&&(r=t,t=void 0,e=void 0),S(r)&&(r.hasOwnProperty("centerX")&&(t=r.centerX),r.hasOwnProperty("centerY")&&(e=r.centerY),r.hasOwnProperty("ringRadiusList")&&this.setRingRadiusList(r.ringRadiusList),r.hasOwnProperty("samplesPerRing")&&this.setSamplesPerRing(r.samplesPerRing),r.hasOwnProperty("variation")&&this.setVariation(r.variation)),void 0===t)t=this.width/2,e=this.height/2;else {var n=(s=t,o=e,a=(i=this).width/2,u=i.height/2,T.x=s-i.x,T.y=o-i.y,T.x/=i.scaleX,T.y/=i.scaleY,G(T,0,0,-i.rotation),T.x+=a,T.y+=u,T);t=n.x,e=n.y;}var i,s,o,a,u;if(this.shatterCenter.x=t,this.shatterCenter.y=e,this.clear(),this.dirtyCache[9]=-1,0===this.width||0===this.height)return this;for(var h=function(t){var e,r,n,i,s,o,a=t.rectangle;a?(e=a.x,n=a.y,s=a.width,o=a.height):(e=0,n=0,s=t.width,o=t.height),r=e+s,i=n+o;var u,h,c=t.center;void 0===c?(u=(e+r)/2,h=(n+i)/2):(u=d(c.x,e,r),h=d(c.y,n,i));var l,f=g(t,"ringRadiusList",m),v=g(t,"samplesPerRing",12),y=g(t,"variation",.25),P=g(t,"triangleOutput",!0);(l=f)&&"function"==typeof l&&(f=f(s,o));for(var w=1-y,x=1+y,O=0;O<10;O++)try{var j=b(u,h,s,o,f,v,w,x,e,r,n,i);return p(j,P)}catch(t){}throw new Error("Generate triangles fail")}({width:this.width,height:this.height,center:this.shatterCenter,triangleOutput:!1,ringRadiusList:this.ringRadiusList,variation:this.variation,samplesPerRing:this.samplesPerRing}),c=h.vertices,l=h.indices,f=[],v=this.width,y=this.height,P=this.frame.cutWidth/y/2,w=this.frame.cutHeight/y/2,x=this.frame.u0,O=this.frame.u1,j=this.frame.v0,R=O-x,k=this.frame.v1-j,M=0,_=c.length;M<_;M++){var E=c[M],V=E[0],L=E[1];f.push({g:C(t,e,V,L),x:V/y-P,y:L/y-w,u:x+R*(V/v),v:j+k*(L/y)});}for(M=0,_=l.length;M<_;M+=3){var U=f[l[M+0]],D=f[l[M+1]],A=f[l[M+2]],F=new N(U.x,-U.y,0,U.u,U.v),Y=new N(D.x,-D.y,0,D.u,D.v),B=new N(A.x,-A.y,0,A.u,A.v),W=new I(F,Y,B);this.vertices.push(F,Y,B),this.faces.push(W),W.g=Math.min(U.g,D.g,A.g);}return this.faces.sort((function(t,e){return t.g-e.g})),this}},{key:"startUpdate",value:function(){return this.ignoreDirtyCache=!0,this}},{key:"stopUpdate",value:function(){return this.ignoreDirtyCache=!1,this}}]),s}(c);function L(t,e,r,n,i){var s=new V(this.scene,t,e,r,n,i);return this.scene.add.existing(s),s}var U=Phaser.Utils.Objects.GetAdvancedValue,D=Phaser.GameObjects.BuildGameObject;function A(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var r=U(t,"key",null),n=U(t,"frame",null),i=new V(this.scene,0,0,r,n,t);return D(this.scene,i,t),i}var F=Phaser.Textures.DynamicTexture,Y=Phaser.Utils.Objects.IsPlainObject,B=Phaser.Utils.Objects.GetValue,W=function(t){i(o,t);var r=u(o);function o(t,n,i,s,a,u){var h;e(this,o),Y(n)&&(n=B(u=n,"x",0),i=B(u,"y",0),s=B(u,"width",32),a=B(u,"height",32));var c=function(t,e,r){return void 0===e&&(e=2),void 0===r&&(r=2),new F(t.sys.textures,null,e,r)}(t,s,a);return (h=r.call(this,t,n,i,c,null,u)).type="rexShatterRenderTexture",h.rt=h.texture,h}return n(o,[{key:"destroy",value:function(t){this.scene&&!this.ignoreDestroy&&(h(s(o.prototype),"destroy",this).call(this,t),this.rt.destroy(),this.rt=null);}}]),o}(V);function X(t,e,r,n,i){var s=new W(this.scene,t,e,r,n,i);return this.scene.add.existing(s),s}var H=Phaser.Utils.Objects.GetAdvancedValue,q=Phaser.GameObjects.BuildGameObject;function z(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var r=H(t,"x",0),n=H(t,"y",0),i=H(t,"width",32),s=H(t,"height",32),o=new W(this.scene,r,n,i,s,t);return q(this.scene,o,t),o}var J=function(t){return null==t||""===t||0===t.length},K=function(e,r,n,i){if(void 0===i&&(i="."),"object"===t(e)){if(J(r)){if(null==n)return;"object"===t(n)&&(e=n);}else {"string"==typeof r&&(r=r.split(i));var s=r.pop(),o=function(e,r,n){var i=e;if(J(r));else {var s;"string"==typeof r&&(r=r.split("."));for(var o=0,a=r.length;o<a;o++){var u;null!=i[s=r[o]]&&"object"===t(i[s])||(u=o===a-1?void 0===n?{}:n:{},i[s]=u),i=i[s];}}return i}(e,r);o[s]=n;}return e}},Q=function(t){i(s,Phaser.Plugins.BasePlugin);var r=u(s);function s(t){var n;return e(this,s),n=r.call(this,t),t.registerGameObject("rexShatterImage",L,A),t.registerGameObject("rexShatterRenderTexture",X,z),n}return n(s,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}}]),s}();return K(window,"RexPlugins.GameObjects.ShatterImage",V),K(window,"RexPlugins.GameObjects.ShatterRenderTexture",W),Q}));