UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 8.28 kB
var t,i;t=void 0,i=function(){const t=Math.sqrt(3),i=Math.sqrt(3);var e=0,r=1,n=2,s=3;const h=e,o=r,u=n,a=s;var d={};const g=e,c=r,l=n,f=s;var v={},y={};const x=e,p=r,w=n,W=s,b=4/3,X=2/3;var Y={},z={},T=function(t,i,e,r){var n=t&&("object"==typeof t||"function"==typeof t),s=r&&("object"==typeof r||"function"==typeof r);if(!n&&!s)return e;var h=String(i);if(n&&h in t)return t[h];if(s&&h in r)return r[h];if(-1===h.indexOf("."))return e;var o=h.split(".");if(n){var u=m(t,o,e);if(u.found)return u.value}if(s){var a=m(r,o,e);if(a.found)return a.value}return e},m=function(t,i,e){for(var r=t,n=e,s=0,h=i.length;s<h;s++){var o=i[s];if(!r||"object"!=typeof r&&"function"!=typeof r||!(o in r))return _.found=!1,_;r=n=r[o]}return _.found=!0,_.value=n,_},_={};const O=e,M=r,H=n,S=s;class P{constructor(t){this.resetFromJSON(t)}resetFromJSON(t){this.setType(T(t,"staggeraxis",1),T(t,"staggerindex",1)),this.setDirectionMode(),this.setOriginPosition(T(t,"x",0),T(t,"y",0));var i=T(t,"size",void 0);void 0!==i?this.setCellRadius(i):this.setCellSize(T(t,"cellWidth",0),T(t,"cellHeight",0))}setType(t,i){return"string"==typeof t&&(t=C[t]),"string"==typeof i&&(i=j[i]),this.staggeraxis=t,this.staggerindex=i,this.mode=0===t?0===i?S:H:0===i?M:O,this}setDirectionMode(){return this.directions=6,this}setOriginPosition(t,i){return this.x=t,this.y=i,this}get width(){return this._width}set width(t){this._width=t,this._halfWidth=t/2}get height(){return this._height}set height(t){this._height=t,this._halfHeight=t/2}setCellSize(t,i){return this.width=t,this.height=i,this}setCellRadius(e){this.size=e;var r={size:this.size,type:this.staggeraxis},n=function(i){return 0===i.type?2*i.size:t*i.size}(r),s=function(t){return 0===t.type?i*t.size:2*t.size}(r);return this.setCellSize(n,s),this}get cellWidth(){return this.width}set cellWidth(t){this.width=t}get cellHeight(){return this.height}set cellHeight(t){this.height=t}}var k={getWorldXY:function(t,i,e){void 0===e?e={}:!0===e&&(e=d);var r=t*this.width,n=i*this.height;switch(this.mode){case h:1&i&&(r+=this._halfWidth),n*=.75;break;case o:1&i&&(r-=this._halfWidth),n*=.75;break;case u:r*=.75,1&t&&(n+=this._halfHeight);break;case a:r*=.75,1&t&&(n-=this._halfHeight)}return r+=this.x,n+=this.y,e.x=r,e.y=n,e},getWorldX:function(t,i){return this.getWorldXY(t,i,!0).x},getWorldY:function(t,i){return this.getWorldXY(t,i,!0).y},getTileXY:function(t,i,e){var r,n;switch(void 0===e?e={}:!0===e&&(e=z),t-=this.x,i-=this.y,this.mode){case x:case p:n=i*b/this.height,r=t/this.width-X*(i/this.height);break;case w:case W:n=i/this.height-X*(t/this.width),r=t*b/this.width}var s=function(t,i,e){return void 0===e?e={}:!0===e&&(e=v),e.x=t,e.y=-t-i,e.z=i,e}(r,n,Y);return function(t,i,e,r){"number"!=typeof t&&(t=(r=t).x,i=r.y,e=r.z),void 0===r?r={}:!0===r&&(r=v);var n=Math.round(t),s=Math.round(i),h=Math.round(e),o=Math.abs(n-t),u=Math.abs(s-i),a=Math.abs(h-e);o>u&&o>a?n=-s-h:u>a?s=-n-h:h=-n-s,r.x=n,r.y=s,r.z=h}(s),function(t,i,e,r,n){switch(void 0===n?n={}:!0===n&&(n=y),t){case g:n.x=i+(r-(1&r))/2,n.y=r;break;case c:n.x=i+(r+(1&r))/2,n.y=r;break;case l:n.x=i,n.y=r+(i-(1&i))/2;break;case f:n.x=i,n.y=r+(i+(1&i))/2}}(this.mode,s.x,s.y,s.z,e),e},getTileX:function(t,i){return this.getTileXY(t,i,!0).x},getTileY:function(t,i){return this.getTileXY(t,i,!0).y}};Object.assign(P.prototype,k);const C={y:0,x:1},j={even:0,odd:1};var D=function(){},F=new Phaser.GameObjects.Zone({sys:{queueDepthSort:D,events:{once:D}}},0,0,1,1);F.setOrigin(0);var N=0,q=1,E=2,J=4,R=6,G=8,A=10,U=12,V=function(t){return void 0!==t.displayWidth?t.displayWidth:t.width},B=function(t){return void 0!==t.displayHeight?t.displayHeight:t.height},Z=function(t){var i=B(t);return t.y+i-i*t.originY},I=function(t){var i=V(t);return t.x-i*t.originX+.5*i},K=function(t,i){var e=B(t);return t.y=i-e+e*t.originY,t},L=function(t,i){var e=V(t),r=e*t.originX;return t.x=i+r-.5*e,t},Q=function(t){var i=V(t);return t.x-i*t.originX},$=function(t,i){var e=V(t);return t.x=i+e*t.originX,t},tt=function(t){var i=V(t);return t.x+i-i*t.originX},it=function(t,i){var e=V(t);return t.x=i-e+e*t.originX,t},et=function(t,i){var e=B(t),r=e*t.originY;return t.y=i+r-.5*e,t},rt=function(t){var i=B(t);return t.y-i*t.originY+.5*i},nt=function(t){var i=B(t);return t.y-i*t.originY},st=function(t,i){var e=B(t);return t.y=i+e*t.originY,t},ht=[];ht[11]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),L(t,I(i)+e),K(t,Z(i)+r),t},ht[A]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),$(t,Q(i)-e),K(t,Z(i)+r),t},ht[U]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),it(t,tt(i)+e),K(t,Z(i)+r),t},ht[R]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),function(t,i,e){L(t,i),et(t,e)}(t,I(i)+e,rt(i)+r),t},ht[J]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),$(t,Q(i)-e),et(t,rt(i)+r),t},ht[G]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),it(t,tt(i)+e),et(t,rt(i)+r),t},ht[q]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),L(t,I(i)+e),st(t,nt(i)-r),t},ht[N]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),$(t,Q(i)-e),st(t,nt(i)-r),t},ht[E]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),it(t,tt(i)+e),st(t,nt(i)-r),t};var ot=function(t,i,e,r,n){return ht[e](t,i,r,n)};const ut=Phaser.Utils.Objects.GetFastValue;var at=new P,dt={},gt={};class ct{constructor(t){this.resetFromJSON(t)}resetFromJSON(t){this.setType(T(t,"type",0)),this.setDirectionMode(T(t,"dir",4)),this.setOriginPosition(T(t,"x",0),T(t,"y",0)),this.setCellSize(T(t,"cellWidth",0),T(t,"cellHeight",0))}setType(t){return"string"==typeof t&&(t=ft[t]),this.mode=t,this}setDirectionMode(t){return"string"==typeof t&&(t=vt[t]),this.directions=t,this}setOriginPosition(t,i){return this.x=t,this.y=i,this}get width(){return this._width}set width(t){this._width=t,this._halfWidth=t/2}get height(){return this._height}set height(t){this._height=t,this._halfHeight=t/2}setCellSize(t,i){return this.width=t,this.height=i,this}get cellWidth(){return this.width}set cellWidth(t){this.width=t}get cellHeight(){return this.height}set cellHeight(t){this.height=t}}var lt={getWorldXY:function(t,i,e){var r,n;switch(void 0===e?e={}:!0===e&&(e=dt),this.mode){case 0:r=t*this.width,n=i*this.height;break;case 1:r=(t-i)*this._halfWidth,n=(t+i)*this._halfHeight}return r+=this.x,n+=this.y,e.x=r,e.y=n,e},getWorldX:function(t,i){return this.getWorldXY(t,i,!0).x},getWorldY:function(t,i){return this.getWorldXY(t,i,!0).y},getTileXY:function(t,i,e){void 0===e?e={}:!0===e&&(e=gt),t-=this.x,i-=this.y;var r=t/this.width,n=i/this.height;switch(this.mode){case 0:e.x=Math.round(r),e.y=Math.round(n);break;case 1:e.x=Math.round(+r+n),e.y=Math.round(-r+n)}return e},getTileX:function(t,i){return this.getTileXY(t,i,!0).x},getTileY:function(t,i){return this.getTileXY(t,i,!0).y}};Object.assign(ct.prototype,lt);const ft={orthogonal:0,isometric:1,staggered:2},vt={"4dir":4,"8dir":8},yt=Phaser.Utils.Objects.GetFastValue;var xt=new ct;class pt extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}hexagon(t,i){return function(t,i){void 0===i&&(i={});var e=ut(i,"width",-1),r=ut(i,"height",-1),n=ut(i,"cellWidth",1),s=ut(i,"cellHeight",n),h=ut(i,"staggeraxis",1),o=ut(i,"staggerindex",1),u=ut(i,"position",Phaser.Display.Align.CENTER),a=ut(i,"x",0),d=ut(i,"y",0);at.setOriginPosition(a,d).setCellSize(n,s).setType(h,o),F.setSize(n,s);for(var g=r-1,c=e-1,l=0,f=0,v=0,y=t.length;v<y;v++)if(at.getWorldXY(f,l,F),ot(t[v],F,u),-1===e)l++;else if(-1===r)f++;else if(f===c){if(l===g)break;f=0,l++}else f++;return t}(t,i)}quad(t,i){return function(t,i){void 0===i&&(i={});var e=yt(i,"width",-1),r=yt(i,"height",-1),n=yt(i,"cellWidth",1),s=yt(i,"cellHeight",n),h=yt(i,"type",0),o=yt(i,"position",Phaser.Display.Align.CENTER),u=yt(i,"x",0),a=yt(i,"y",0);xt.setOriginPosition(u,a).setCellSize(n,s).setType(h),F.setSize(n,s);for(var d=r-1,g=e-1,c=0,l=0,f=0,v=t.length;f<v;f++)if(xt.getWorldXY(l,c,F),ot(t[f],F,o),-1===e)c++;else if(-1===r)l++;else if(l===g){if(c===d)break;l=0,c++}else l++;return t}(t,i)}}return pt},"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).rexgridalignplugin=i();