phaser4-rex-plugins
Version:
2 lines (1 loc) • 7.98 kB
JavaScript
var t,i;t=void 0,i=function(){const t=Math.sqrt(3),i=Math.sqrt(3);var e=0,r=1,s=2,n=3;const h=e,o=r,u=s,a=n;var d={};const g=e,l=r,c=s,f=n;var v={},y={};const x=e,p=r,w=s,W=n,b=4/3,X=2/3;var Y={},z={},T=function(t,i,e){if(!t||"number"==typeof t)return e;if("string"==typeof i){if(t.hasOwnProperty(i))return t[i];if(-1===i.indexOf("."))return e;i=i.split(".")}for(var r=i,s=t,n=e,h=0;h<r.length;h++){if(i=r[h],!s.hasOwnProperty(i)){n=e;break}s=n=s[i]}return n};const m=e,O=r,_=s,M=n;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=S[t]),"string"==typeof i&&(i=k[i]),this.staggeraxis=t,this.staggerindex=i,this.mode=0===t?0===i?M:_:0===i?O:m,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},s=function(i){return 0===i.type?2*i.size:t*i.size}(r),n=function(t){return 0===t.type?i*t.size:2*t.size}(r);return this.setCellSize(s,n),this}get cellWidth(){return this.width}set cellWidth(t){this.width=t}get cellHeight(){return this.height}set cellHeight(t){this.height=t}}var H={getWorldXY:function(t,i,e){void 0===e?e={}:!0===e&&(e=d);var r=t*this.width,s=i*this.height;switch(this.mode){case h:1&i&&(r+=this._halfWidth),s*=.75;break;case o:1&i&&(r-=this._halfWidth),s*=.75;break;case u:r*=.75,1&t&&(s+=this._halfHeight);break;case a:r*=.75,1&t&&(s-=this._halfHeight)}return r+=this.x,s+=this.y,e.x=r,e.y=s,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,s;switch(void 0===e?e={}:!0===e&&(e=z),t-=this.x,i-=this.y,this.mode){case x:case p:s=i*b/this.height,r=t/this.width-X*(i/this.height);break;case w:case W:s=i/this.height-X*(t/this.width),r=t*b/this.width}var n=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,s,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 s=Math.round(t),n=Math.round(i),h=Math.round(e),o=Math.abs(s-t),u=Math.abs(n-i),a=Math.abs(h-e);o>u&&o>a?s=-n-h:u>a?n=-s-h:h=-s-n,r.x=s,r.y=n,r.z=h}(n),function(t,i,e,r,s){switch(void 0===s?s={}:!0===s&&(s=y),t){case g:s.x=i+(r-(1&r))/2,s.y=r;break;case l:s.x=i+(r+(1&r))/2,s.y=r;break;case c:s.x=i,s.y=r+(i-(1&i))/2;break;case f:s.x=i,s.y=r+(i+(1&i))/2}}(this.mode,n.x,n.y,n.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,H);const S={y:0,x:1},k={even:0,odd:1};var C=function(){},D=new Phaser.GameObjects.Zone({sys:{queueDepthSort:C,events:{once:C}}},0,0,1,1);D.setOrigin(0);var j=0,F=1,N=2,q=4,E=6,J=8,R=10,G=12,A=function(t){return void 0!==t.displayWidth?t.displayWidth:t.width},U=function(t){return void 0!==t.displayHeight?t.displayHeight:t.height},V=function(t){var i=U(t);return t.y+i-i*t.originY},B=function(t){var i=A(t);return t.x-i*t.originX+.5*i},Z=function(t,i){var e=U(t);return t.y=i-e+e*t.originY,t},I=function(t,i){var e=A(t),r=e*t.originX;return t.x=i+r-.5*e,t},K=function(t){var i=A(t);return t.x-i*t.originX},L=function(t,i){var e=A(t);return t.x=i+e*t.originX,t},Q=function(t){var i=A(t);return t.x+i-i*t.originX},$=function(t,i){var e=A(t);return t.x=i-e+e*t.originX,t},tt=function(t,i){var e=U(t),r=e*t.originY;return t.y=i+r-.5*e,t},it=function(t){var i=U(t);return t.y-i*t.originY+.5*i},et=function(t){var i=U(t);return t.y-i*t.originY},rt=function(t,i){var e=U(t);return t.y=i+e*t.originY,t},st=[];st[11]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),I(t,B(i)+e),Z(t,V(i)+r),t},st[R]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),L(t,K(i)-e),Z(t,V(i)+r),t},st[G]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),$(t,Q(i)+e),Z(t,V(i)+r),t},st[E]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),function(t,i,e){I(t,i),tt(t,e)}(t,B(i)+e,it(i)+r),t},st[q]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),L(t,K(i)-e),tt(t,it(i)+r),t},st[J]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),$(t,Q(i)+e),tt(t,it(i)+r),t},st[F]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),I(t,B(i)+e),rt(t,et(i)-r),t},st[j]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),L(t,K(i)-e),rt(t,et(i)-r),t},st[N]=function(t,i,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),$(t,Q(i)+e),rt(t,et(i)-r),t};var nt=function(t,i,e,r,s){return st[e](t,i,r,s)};const ht=Phaser.Utils.Objects.GetFastValue;var ot=new P,ut={},at={};class dt{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=lt[t]),this.mode=t,this}setDirectionMode(t){return"string"==typeof t&&(t=ct[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 gt={getWorldXY:function(t,i,e){var r,s;switch(void 0===e?e={}:!0===e&&(e=ut),this.mode){case 0:r=t*this.width,s=i*this.height;break;case 1:r=(t-i)*this._halfWidth,s=(t+i)*this._halfHeight}return r+=this.x,s+=this.y,e.x=r,e.y=s,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=at),t-=this.x,i-=this.y;var r=t/this.width,s=i/this.height;switch(this.mode){case 0:e.x=Math.round(r),e.y=Math.round(s);break;case 1:e.x=Math.round(+r+s),e.y=Math.round(-r+s)}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(dt.prototype,gt);const lt={orthogonal:0,isometric:1,staggered:2},ct={"4dir":4,"8dir":8},ft=Phaser.Utils.Objects.GetFastValue;var vt=new dt;class yt 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=ht(i,"width",-1),r=ht(i,"height",-1),s=ht(i,"cellWidth",1),n=ht(i,"cellHeight",s),h=ht(i,"staggeraxis",1),o=ht(i,"staggerindex",1),u=ht(i,"position",Phaser.Display.Align.CENTER),a=ht(i,"x",0),d=ht(i,"y",0);ot.setOriginPosition(a,d).setCellSize(s,n).setType(h,o),D.setSize(s,n);for(var g=r-1,l=e-1,c=0,f=0,v=0,y=t.length;v<y;v++)if(ot.getWorldXY(f,c,D),nt(t[v],D,u),-1===e)c++;else if(-1===r)f++;else if(f===l){if(c===g)break;f=0,c++}else f++;return t}(t,i)}quad(t,i){return function(t,i){void 0===i&&(i={});var e=ft(i,"width",-1),r=ft(i,"height",-1),s=ft(i,"cellWidth",1),n=ft(i,"cellHeight",s),h=ft(i,"type",0),o=ft(i,"position",Phaser.Display.Align.CENTER),u=ft(i,"x",0),a=ft(i,"y",0);vt.setOriginPosition(u,a).setCellSize(s,n).setType(h),D.setSize(s,n);for(var d=r-1,g=e-1,l=0,c=0,f=0,v=t.length;f<v;f++)if(vt.getWorldXY(c,l,D),nt(t[f],D,o),-1===e)l++;else if(-1===r)c++;else if(c===g){if(l===d)break;c=0,l++}else c++;return t}(t,i)}}return yt},"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();