phaser4-rex-plugins
Version:
2 lines (1 loc) • 5.47 kB
JavaScript
var e,t;e=void 0,t=function(){const e=Phaser.GameObjects.GameObject,t=Phaser.GameObjects.Layer;var s=function(e){return void 0!==e.displayWidth?e.displayWidth:e.width},r=function(e){return void 0!==e.displayHeight?e.displayHeight:e.height};Phaser.Geom.Rectangle;const n=Phaser.Math.Vector2,o=Phaser.Math.RotateAround;Phaser.GameObjects.Container;var i=void 0,a=function(e,t,s){return void 0===s&&(s=!1),0!==e.rotation&&o(t,e.x,e.y,e.rotation),s&&e.parentContainer&&e.parentContainer.getBoundsTransformMatrix().transformPoint(t.x,t.y,t),t};const h=Phaser.Geom.Polygon;var l=function(e,t){void 0===t&&(t=new h);var o=function(e,t,o){return void 0===t?t=new n:!0===t&&(void 0===i&&(i=new n),t=i),e.getTopLeft?e.getTopLeft(t,o):(t.x=e.x-s(e)*e.originX,t.y=e.y-r(e)*e.originY,a(e,t,o))}(e),l=function(e,t,o){return void 0===t?t=new n:!0===t&&(void 0===i&&(i=new n),t=i),e.getTopRight?e.getTopRight(t,o):(t.x=e.x-s(e)*e.originX+s(e),t.y=e.y-r(e)*e.originY,a(e,t,o))}(e),c=function(e,t,o){return void 0===t?t=new n:!0===t&&(void 0===i&&(i=new n),t=i),e.getBottomRight?e.getBottomRight(t,o):(t.x=e.x-s(e)*e.originX+s(e),t.y=e.y-r(e)*e.originY+r(e),a(e,t,o))}(e),y=function(e,t,o){return void 0===t?t=new n:!0===t&&(void 0===i&&(i=new n),t=i),e.getBottomLeft?e.getBottomLeft(t,o):(t.x=e.x-s(e)*e.originX,t.y=e.y-r(e)*e.originY+r(e),a(e,t,o))}(e);return t.setTo([o,l,c,y,o]),t};const c=Phaser.Geom.Polygon,y=Phaser.Utils.Array.SpliceOne;class g{constructor(){this.gameObjects=[],this.polygons=[]}contains(e){return-1!==this.gameObjects.indexOf(e)}get(e){return u.gameObject=this.gameObjects[e],u.polygon=this.polygons[e],u}addDestroyCallback(e){if(Array.isArray(e)){for(var t=e,s=0,r=t.length;s<r;s++)this.addDestroyCallback(t[s]);return this}return e.on&&e.once("destroy",this.onChildDestroy,this),this}removeDestroyCallback(e){if(Array.isArray(e)){for(var t=e,s=0,r=t.length;s<r;s++)this.removeDestroyCallback(t[s]);return this}return e.off&&e.off("destroy",this.onChildDestroy,this),this}clear(){return this.removeDestroyCallback(this.gameObjects),this.gameObjects.length=0,this.polygons.length=0,this}add(s,r){return this.contains(s)||((n=s)instanceof e||n instanceof t?void 0===r&&(r=l(s)):s instanceof c&&(r=s),this.gameObjects.push(s),this.polygons.push(r),this.addDestroyCallback(s)),this;var n}remove(e){var t=this.gameObjects.indexOf(e);return-1===t||(y(this.gameObjects,t),y(this.polygons,t),this.removeDestroyCallback(e)),this}onChildDestroy(e,t){this.remove(e)}update(e,t){var s=this.gameObjects.indexOf(e);return-1===s||(void 0===t&&(t=l(e,this.polygons[s])),this.polygons[s]=t),this}}var u={};const d=Phaser.Geom.Intersects.GetLineToLine,f=Phaser.Geom.Intersects.PointToLine;var v=function(e,t,s){void 0===s?s={}:!0===s&&(s=m);var r=!1;b.setTo(e.x1,e.y1),s.d=1/0,x.set();for(var n=t[0],o=1;o<t.length;o++){var i=t[o];p.setTo(n.x,n.y,i.x,i.y),n=i,f(b,p)||d(e,p,!1,x)&&x.z<s.d&&(s.x=x.x,s.y=x.y,s.d=x.z,s.segIndex=o-1,r=!0)}return r?s:null},m={},b=new Phaser.Geom.Point,p=new Phaser.Geom.Line,x=new Phaser.Math.Vector3,P=function(e,t){var s=Array.isArray(e);if(void 0===t?t=s?[]:{}:function(e){if("object"!=typeof e||null===e)return e;if(Array.isArray(e))e.length=0;else for(var t in e)delete e[t]}(t),s){t.length=e.length;for(var r=0,n=e.length;r<n;r++)t[r]=e[r]}else for(var o in e)t[o]=e[o];return t};const O=Phaser.Geom.Polygon.GetAABB,A=Phaser.Geom.Intersects.LineToRectangle;var j={},G=new Phaser.Geom.Rectangle;const w=Phaser.Utils.Objects.GetValue,L=Phaser.Geom.Line,T=Phaser.Geom.Line.SetToAngle,R=Phaser.Geom.Line.ReflectAngle;class C{constructor(e){this.obstacles=new g,this.ray=new L,this.setMaxRayLength(w(e,"maxRayLength",1e4)),this.result={hit:!1,x:0,y:0,segment:new L,polygon:null,gameObject:null,reflectAngle:0}}destroy(){this.obstacles.clear(),this.obstacles=null,this.ray=null,this.result=null}setMaxRayLength(e){return this.maxRayLength=e,this}addObstacle(e,t){if(Array.isArray(e))for(var s=e,r=0,n=s.length;r<n;r++)this.obstacles.add(s[r]);else this.obstacles.add(e,t);return this}clearObstacle(){return this.obstacles.clear(),this}removeObstacle(e){if(Array.isArray(e))for(var t=e,s=0,r=t.length;s<r;s++)this.obstacles.remove(t[s]);else this.obstacles.remove(e);return this}updateObstacle(e,t){if(Array.isArray(e))for(var s=e,r=0,n=s.length;r<n;r++)this.obstacles.update(s[r]);else this.obstacles.update(e,t);return this}hitTest(){var e=function(e,t,s){void 0===s?s={}:!0===s&&(s=j),Array.isArray(t)||(t=[t]);var r=!1;s.d=1/0;for(var n=0;n<t.length;n++){var o=t[n];if(!(o.points.length>9)||A(e,O(o,G))){var i=v(e,o.points,!0);i&&i.d<s.d&&(P(i,s),s.shapeIndex=n,r=!0)}}return r?s:null}(this.ray,this.obstacles.polygons,!0);if(e){this.ray.x2=e.x,this.ray.y2=e.y,this.result.hit=!0,this.result.x=e.x,this.result.y=e.y;var t=this.obstacles.get(e.shapeIndex);this.result.polygon=t.polygon,this.result.gameObject=t.gameObject;var s=this.result.polygon.points,r=e.segIndex,n=s[r],o=s[r+1],i=this.result.segment;i.setTo(n.x,n.y,o.x,o.y),this.result.reflectAngle=R(this.ray,i)}else this.result.hit=!1;return!!e&&this.result}rayToward(e,t,s){return T(this.ray,e,t,s,this.maxRayLength),this.hitTest()}}class D extends Phaser.Plugins.BasePlugin{constructor(e){super(e)}start(){this.game.events.on("destroy",this.destroy,this)}add(e){return new C(e)}}return D},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexraycasterplugin=t();