UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 12.7 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexcutjigsawimageplugin=t();}(undefined,(function(){function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,(a=r.key,n=void 0,"symbol"==typeof(n=function(e,t){if("object"!=typeof e||null===e)return e;var i=e[Symbol.toPrimitive];if(void 0!==i){var r=i.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===t?String:Number)(e)}(a,"string"))?n:String(n)),r);}var a,n;}function r(e,t,r){return t&&i(e.prototype,t),r&&i(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function a(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&o(e,t);}function n(e){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},n(e)}function o(e,t){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},o(e,t)}function s(e,t){if(t&&("object"==typeof t||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function h(e){var t=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(e){return !1}}();return function(){var i,r=n(e);if(t){var a=n(this).constructor;i=Reflect.construct(r,arguments,a);}else i=r.apply(this,arguments);return s(this,i)}}function c(){return c="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(e,t,i){var r=function(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=n(e)););return e}(e,t);if(r){var a=Object.getOwnPropertyDescriptor(r,t);return a.get?a.get.call(arguments.length<3?e:i):a.value}},c.apply(this,arguments)}var l,u,d=Phaser.Game,f=function(e){return e instanceof d},g=Phaser.Scene,v=function(e){return e instanceof g},m=function(t){return null==t||"object"!==e(t)?null:f(t)?t:f(t.game)?t.game:v(t)?t.sys.game:v(t.scene)?t.scene.sys.game:void 0},y=function(e,t,i,r,a){if(void 0===l){var n=(o=e.manager.game,m(o).textures.getFrame("__WHITE"));l=n.cutWidth,u=n.cutHeight;}var o;return e.stamp("__WHITE",void 0,t,i,{scaleX:r/l,scaleY:a/u,originX:0,originY:0,erase:!0}),e},p=function(e,t,i,r){this.useDynamicTexture?y(this.texture,e,t,i,r):this.context.clearRect(e,t,i,r);},b=function(e,t,i,r,a){var n=this.context;n.save(),n.translate(e,t),a?r.call(a,this.canvas,n,i):r(this.canvas,n,i),n.restore();},w=function(e,t,i,r,a){var n=this.texture;n.camera.setScroll(-e,-t),a?r.call(a,n,i):r(n,i),n.camera.setScroll(0,0);},x={remove:function(e){var t=this.getFrameIndex(e);return -1===t||(this.addFrameName(t,void 0),this.texture.remove(e)),this},clear:function(){for(var e,t=this.frameNames.length;e<t;e++){var i=this.frameNames[e];void 0!==i&&(this.addFrameName(index,void 0),this.texture.remove(i));}return this}},k={draw:function(e,t,i){var r=this.getFrameIndex(e);if(-1===r&&(r=this.getFrameIndex(void 0)),-1===r)return console.warn("Does not have free space."),this;var a=this.getTopLeftPosition(r),n=a.x,o=a.y,s=this.cellPadding,h=n+s,c=o+s;p.call(this,n,o,this.outerCellWidth,this.outerCellHeight);var l={width:this.cellWidth,height:this.cellHeight};return (this.useDynamicTexture?w:b).call(this,h,c,l,t,i),this.texture.add(e,0,h,c,l.width,l.height),this.addFrameName(r,e),this},paste:function(e,t){var i;if(this.useDynamicTexture){var r=function(e){return void 0!==e.displayWidth?e.displayWidth:e.width}(t),a=function(e){return void 0!==e.displayHeight?e.displayHeight:e.height}(t);h=r<=this.cellWidth&&a<=this.cellHeight?1:Math.max(r/this.cellWidth,a/this.cellHeight),i=function(e,i){var n=t.originX,o=t.originY,s=t.scaleX,c=t.scaleY;t.setOrigin(0,0).setScale(h,h),e.draw(t),t.setOrigin(n,o).setScale(s,c),i.width=r/h,i.height=a/h;};}else {var n=t.canvas;if(!n)return console.warn("Can't get canvas of game object."),this;var o,s;r=n.width,a=n.height;if(r<=this.cellWidth&&a<=this.cellHeight)o=r,s=a;else {var h=Math.max(r/this.cellWidth,a/this.cellHeight);o=r/h,s=a/h;}i=function(e,t,i){t.drawImage(n,0,0,o,s),i.width=o,i.height=s;};}return this.draw(e,i),this},addEmptyFrame:function(e,t,i){var r;return void 0===t&&(t=this.cellWidth),void 0===i&&(i=this.cellHeight),r=this.useDynamicTexture?function(e,r){r.width=t,r.height=i;}:function(e,r,a){a.width=t,a.height=i;},this.draw(e,r),this},addToBitmapFont:function(){var e=this.texture.key,t=this.bitmapFontCache.get(e);t||(t={data:{retroFont:!0,font:e,size:this.cellWidth,lineHeight:this.cellHeight,chars:{}},texture:e,frame:null},this.bitmapFontCache.add(e,t));for(var i=t.data.chars,r=this.frameNames,a=0,n=r.length;a<n;a++){var o=r[a];if(void 0!==o){var s=this.texture.get(o),h=s.cutX,c=s.cutY,l=s.cutWidth,u=s.cutHeight;i[o.charCodeAt(0)]={x:h,y:c,width:l,height:u,centerX:h+l/2,centerY:c+u/2,xOffset:0,yOffset:0,xAdvance:l,data:{},kerning:{},u0:s.u0,v0:s.v0,u1:s.u1,v1:s.v1};}}return this}};Object.assign(k,x);var P=Phaser.Utils.Objects.IsPlainObject,C=Phaser.Utils.Objects.GetValue,W=function(){function e(i,r,a,n,o,s,h,c){var l,u,d;if(t(this,e),P(r)){var f=r;r=C(f,"key"),a=C(f,"width"),n=C(f,"height"),o=C(f,"cellWidth"),s=C(f,"cellHeight"),d=C(f,"cellPadding",0),l=C(f,"columns"),u=C(f,"rows"),h=C(f,"fillColor"),c=C(f,"useDynamicTexture");}else "boolean"==typeof h&&(c=h,h=void 0);void 0===o&&(o=64),void 0===s&&(s=64),void 0===d&&(d=0),this.cellWidth=o,this.cellHeight=s,this.cellPadding=d,l?a=this.outerCellWidth*l:(void 0===a&&(a=4096),l=Math.floor(a/this.outerCellWidth)),u?n=this.outerCellWidth*u:(void 0===n&&(n=4096),u=Math.floor(n/this.outerCellWidth)),void 0===c&&(c=!1);var g=m(i);if(this.useDynamicTexture=c,this.texture=function(e,t,i,r,a){void 0===a&&(a=!1);var n=(e=m(e)).textures;return n.exists(t)&&n.remove(t),n[a?"addDynamicTexture":"createCanvas"](t,i,r)}(g,r,a,n,c),this.canvas=c?void 0:this.texture.getCanvas(),this.context=c?void 0:this.texture.getContext(),this.bitmapFontCache=g.cache.bitmapFont,void 0!==h)if(c)this.texture.fill(h);else {var v=this.context;v.fillStyle=h,v.fillRect(0,0,this.canvas.width,this.canvas.height);}this.key=r,this.width=a,this.height=n,this.columns=l,this.rows=u,this.totalCount=this.columns*this.rows,this.frameNames=Array(this.totalCount);for(var y=0,p=this.frameNames.length;y<p;y++)this.frameNames[y]=void 0;}return r(e,[{key:"outerCellWidth",get:function(){return this.cellWidth+2*this.cellPadding}},{key:"outerCellHeight",get:function(){return this.cellHeight+2*this.cellPadding}},{key:"destroy",value:function(){this.texture=void 0,this.canvas=void 0,this.context=void 0,this.frameNames=void 0,this.bitmapFontCache=void 0;}},{key:"getFrameIndex",value:function(e){return this.frameNames.indexOf(e)}},{key:"contains",value:function(e){return -1!==this.getFrameIndex(e)}},{key:"addFrameName",value:function(e,t){return this.frameNames[e]=t,this}},{key:"isFull",get:function(){return -1===this.getFrameIndex(void 0)}},{key:"getTopLeftPosition",value:function(e,t){void 0===t&&(t={});var i=e%this.columns,r=Math.floor(e/this.columns);return t.x=i*(this.cellWidth+2*this.cellPadding),t.y=r*(this.cellHeight+2*this.cellPadding),t}},{key:"updateTexture",value:function(){return this.useDynamicTexture||this.texture.refresh(),this}}]),e}();Object.assign(W.prototype,k);var H=Phaser.Math.DegToRad,O=H(0),T=H(90),S=H(180),j=H(270),F=H(360),M=function(e,t,i,r,a,n){var o=t/2,s=i/2,h=r,c=t-r,l=a,u=i-a;switch(e.clear(),e.beginPath(),e.moveTo(h,l),n.top){case 1:e.lineTo(o-a,l),e.arc(o,l,a,S,F,!1);break;case 2:e.lineTo(o-a,l),e.arc(o,l,a,S,F,!0);}switch(e.lineTo(c,l),n.right){case 1:e.arc(c,s,r,j,T,!1);break;case 2:e.arc(c,s,r,j,T,!0);}switch(e.lineTo(c,u),n.bottom){case 1:e.arc(o,u,a,O,S,!1);break;case 2:e.arc(o,u,a,O,S,!0);}switch(e.lineTo(h,u),n.left){case 1:e.arc(h,s,r,T,j,!1);break;case 2:e.arc(h,s,r,T,j,!0);}e.lineTo(h,l),e.closePath(),e.fillPath();},N=function(e){a(o,Phaser.GameObjects.RenderTexture);var i=h(o);function o(e,r){var a,n=r.width,s=r.height,h=r.edgeWidth,c=r.edgeHeight,l=r.key,u=r.drawShapeCallback,d=void 0===u?M:u;t(this,o),(a=i.call(this,e,0,0,n,s)).setBaseKey(l),a.setDrawShapeCallback(d),void 0===h&&(h=Math.floor(n/7)),a.edgeWidth=h,void 0===c&&(c=Math.floor(s/7)),a.edgeHeight=c;var f=e.make.graphics({add:!1});return a.setMask(f.createGeometryMask()),a.maskGraphics=f,a}return r(o,[{key:"destroy",value:function(e){this.scene&&!this.ignoreDestroy&&(c(n(o.prototype),"destroy",this).call(this,e),this.drawShapeCallback=void 0,this.maskGraphics.destroy(),this.maskGraphics=void 0);}},{key:"setBaseKey",value:function(e){return this.sourceKey=e,this}},{key:"setDrawShapeCallback",value:function(e){return this.drawShapeCallback=e,this}},{key:"drawPiece",value:function(e){var t=e.scrollX,i=e.scrollY,r=e.edgeMode;return "string"==typeof r&&(r={right:(r=r.split("").map((function(e){return parseInt(e)})))[0],bottom:r[1],left:r[2],top:r[3]}),this.clear().fill(3355443),this.camera.setScroll(t,i),this.stamp(this.sourceKey,void 0,0,0,{originX:0,originY:0}),this.camera.setScroll(0,0),this.drawShapeCallback(this.maskGraphics,this.width,this.height,this.edgeWidth,this.edgeHeight,r),this}}]),o}(),D=function(e,t){return "".concat(e,",").concat(t)},R=function(e,t){var i=t.sourceKey,r=t.destinationKey,a=t.columns,n=t.rows,o=t.framePadding,s=void 0===o?1:o,h=t.edgeWidth,c=t.edgeHeight,l=t.edges,u=t.drawShapeCallback,d=t.getFrameNameCallback,f=void 0===d?D:d,g=e.sys.textures,v=g.getFrame(i,"__BASE"),m=v.cutWidth,y=v.height;void 0===h&&(h=Math.floor(m/a/7)),void 0===c&&(c=Math.floor(y/n/7)),void 0===l&&(l=function(e,t){for(var i,r,a,n,o=[],s=0;s<e;s++)o.push(new Array(t));for(var h=0;h<t;h++)for(s=0;s<e;s++)i=0===s?0:1===o[s-1][h].right?2:1,a=0===h?0:1===o[s][h-1].bottom?2:1,r=s===e-1?0:Math.random()>.5?2:1,n=h===t-1?0:Math.random()>.5?2:1,o[s][h]={left:i,right:r,top:a,bottom:n};return o}(a,n)),void 0===r&&(r="".concat(i,"_pieces"));var p=m/a+2*h,b=y/n+2*c;g.exists(r)&&g.remove(r);for(var w=new W(e,{key:r,cellWidth:p,cellHeight:b,cellPadding:s,columns:a,rows:n,useDynamicTexture:!0,fillColor:8947848}),x=new N(e,{width:p,height:b,edgeWidth:h,edgeHeight:c,key:i,drawShapeCallback:u}),k=-h,P=k,C=-c,H=0;H<n;H++){for(var O=0;O<a;O++)x.drawPiece({scrollX:P,scrollY:C,edgeMode:l[O][H]}),w.paste(f(O,H),x),P+=p-2*h;P=k,C+=b-2*c;}return x.destroy(),w.destroy(),{sourceKey:i,destinationKey:r,columns:a,rows:n,sourceFrameWidth:m,sourceFrameHeight:y,frameWidth:p,frameHeight:b,edgeWidth:h,edgeHeight:c,getFrameNameCallback:f}},_=Phaser.GameObjects.Image,I=Phaser.Math.RotateAround;return function(e){a(n,Phaser.Plugins.BasePlugin);var i=h(n);function n(e){return t(this,n),i.call(this,e)}return r(n,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"gridCut",value:function(e,t){return function(e,t){var i=t.piecesKey,r=t.columns,a=t.rows,n=t.edgeWidth,o=t.edgeHeight,s=t.drawShapeCallback,h=t.edges,c=t.createImageCallback,l=t.ImageClass,u=void 0===l?_:l,d=t.objectPool,f=t.add,g=void 0===f||f,v=t.align,m=void 0===v?g:v,y=t.originX,p=void 0===y?.5:y,b=t.originY,w=void 0===b?.5:b,x=e.scene,k=e.texture.key,P=e.getTopLeft(),C=P.x,W=P.y,H=e.scaleX,O=e.scaleY,T=e.rotation,S=R(x,{sourceKey:k,destinationKey:i,columns:r,rows:a,edgeWidth:n,edgeHeight:o,edges:h,drawShapeCallback:s});i=S.destinationKey;var j=S.getFrameNameCallback,F=S.frameWidth,M=S.frameHeight,N=(F-2*n)*H,D=(M-2*o)*O,K=p*F*H,X=w*M*O;c||(c=function(e,t,i){return new u(e,0,0,t,i)});for(var Y=[],E=C-=n,G=W-=o,B=0;B<a;B++){for(var A=0;A<r;A++){var L,U=j(A,B);if(L=d&&d.length>0?d.pop().setTexture(i,U):c(x,i,U),g&&x.add.existing(L),m){var z=E+K,V=G+X;L.setOrigin(p,w).setPosition(z,V).setScale(H,O).setRotation(T),I(L,C,W,T);}E+=N,Y.push(L);}E=C,G+=D;}return Y}(e,t)}}]),n}()}));