UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 8.11 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).rexraycasterplugin=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 n(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,(o=r.key,i=void 0,"symbol"==typeof(i=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.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)}(o,"string"))?i:String(i)),r);}var o,i;}function r(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function i(e,t){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},i(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 a(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 n,r=o(e);if(t){var i=o(this).constructor;n=Reflect.construct(r,arguments,i);}else n=r.apply(this,arguments);return s(this,n)}}var u=Phaser.GameObjects.GameObject,l=function(e){return void 0!==e.displayWidth?e.displayWidth:e.width},c=function(e){return void 0!==e.displayHeight?e.displayHeight:e.height};Phaser.Geom.Rectangle;var h=Phaser.Math.Vector2,f=Phaser.Math.RotateAround,y=void 0,g=function(e,t,n){(void 0===n&&(n=!1),0!==e.rotation&&f(t,e.x,e.y,e.rotation),n&&e.parentContainer)&&e.parentContainer.getBoundsTransformMatrix().transformPoint(t.x,t.y,t);return t},d=Phaser.Geom.Polygon,v=function(e,t){void 0===t&&(t=new d);var n=function(e,t,n){return void 0===t?t=new h:!0===t&&(void 0===y&&(y=new h),t=y),e.getTopLeft?e.getTopLeft(t):(t.x=e.x-l(e)*e.originX,t.y=e.y-c(e)*e.originY,g(e,t,n))}(e),r=function(e,t,n){return void 0===t?t=new h:!0===t&&(void 0===y&&(y=new h),t=y),e.getTopRight?e.getTopRight(t):(t.x=e.x-l(e)*e.originX+l(e),t.y=e.y-c(e)*e.originY,g(e,t,n))}(e),o=function(e,t,n){return void 0===t?t=new h:!0===t&&(void 0===y&&(y=new h),t=y),e.getBottomRight?e.getBottomRight(t):(t.x=e.x-l(e)*e.originX+l(e),t.y=e.y-c(e)*e.originY+c(e),g(e,t,n))}(e),i=function(e,t,n){return void 0===t?t=new h:!0===t&&(void 0===y&&(y=new h),t=y),e.getBottomLeft?e.getBottomLeft(t):(t.x=e.x-l(e)*e.originX,t.y=e.y-c(e)*e.originY+c(e),g(e,t,n))}(e);return t.setTo([n,r,o,i,n]),t},p=Phaser.Geom.Polygon,b=Phaser.Utils.Array.SpliceOne,m=function(){function e(){t(this,e),this.gameObjects=[],this.polygons=[];}return r(e,[{key:"contains",value:function(e){return -1!==this.gameObjects.indexOf(e)}},{key:"get",value:function(e){return x.gameObject=this.gameObjects[e],x.polygon=this.polygons[e],x}},{key:"addDestroyCallback",value:function(e){if(Array.isArray(e)){for(var t=e,n=0,r=t.length;n<r;n++)this.addDestroyCallback(t[n]);return this}return e.on&&e.once("destroy",this.onChildDestroy,this),this}},{key:"removeDestroyCallback",value:function(e){if(Array.isArray(e)){for(var t=e,n=0,r=t.length;n<r;n++)this.removeDestroyCallback(t[n]);return this}return e.off&&e.off("destroy",this.onChildDestroy,this),this}},{key:"clear",value:function(){return this.removeDestroyCallback(this.gameObjects),this.gameObjects.length=0,this.polygons.length=0,this}},{key:"add",value:function(e,t){return this.contains(e)||(e instanceof u?void 0===t&&(t=v(e)):e instanceof p&&(t=e),this.gameObjects.push(e),this.polygons.push(t),this.addDestroyCallback(e)),this}},{key:"remove",value:function(e){var t=this.gameObjects.indexOf(e);return -1===t||(b(this.gameObjects,t),b(this.polygons,t),this.removeDestroyCallback(e)),this}},{key:"onChildDestroy",value:function(e,t){this.remove(e);}},{key:"update",value:function(e,t){var n=this.gameObjects.indexOf(e);return -1===n||(void 0===t&&(t=v(e,this.polygons[n])),this.polygons[n]=t),this}}]),e}(),x={},O=Phaser.Geom.Intersects.GetLineToLine,P=Phaser.Geom.Intersects.PointToLine,w=function(e,t,n){void 0===n?n={}:!0===n&&(n=j);var r=!1;k.setTo(e.x1,e.y1),n.d=1/0,T.set();for(var o=t[0],i=1;i<t.length;i++){var s=t[i];A.setTo(o.x,o.y,s.x,s.y),o=s,P(k,A)||O(e,A,!1,T)&&T.z<n.d&&(n.x=T.x,n.y=T.y,n.d=T.z,n.segIndex=i-1,r=!0);}return r?n:null},j={},k=new Phaser.Geom.Point,A=new Phaser.Geom.Line,T=new Phaser.Math.Vector3,R=function(t,n){var r=Array.isArray(t);if(void 0===n?n=r?[]:{}:function(t){if("object"!==e(t)||null===t)return t;if(Array.isArray(t))t.length=0;else for(var n in t)delete t[n];}(n),r){n.length=t.length;for(var o=0,i=t.length;o<i;o++)n[o]=t[o];}else for(var s in t)n[s]=t[s];return n},G=Phaser.Geom.Polygon.GetAABB,L=Phaser.Geom.Intersects.LineToRectangle,C={},D=new Phaser.Geom.Rectangle,S=Phaser.Utils.Objects.GetValue,B=Phaser.Geom.Line,_=Phaser.Geom.Line.SetToAngle,I=Phaser.Geom.Line.ReflectAngle,M=function(){function e(n){t(this,e),this.obstacles=new m,this.ray=new B,this.setMaxRayLength(S(n,"maxRayLength",1e4)),this.result={hit:!1,x:0,y:0,segment:new B,polygon:null,gameObject:null,reflectAngle:0};}return r(e,[{key:"destroy",value:function(){this.obstacles.clear(),this.obstacles=null,this.ray=null,this.result=null;}},{key:"setMaxRayLength",value:function(e){return this.maxRayLength=e,this}},{key:"addObstacle",value:function(e,t){if(Array.isArray(e))for(var n=e,r=0,o=n.length;r<o;r++)this.obstacles.add(n[r]);else this.obstacles.add(e,t);return this}},{key:"clearObstacle",value:function(){return this.obstacles.clear(),this}},{key:"removeObstacle",value:function(e){if(Array.isArray(e))for(var t=e,n=0,r=t.length;n<r;n++)this.obstacles.remove(t[n]);else this.obstacles.remove(e);return this}},{key:"updateObstacle",value:function(e,t){if(Array.isArray(e))for(var n=e,r=0,o=n.length;r<o;r++)this.obstacles.update(n[r]);else this.obstacles.update(e,t);return this}},{key:"hitTest",value:function(){var e=function(e,t,n){void 0===n?n={}:!0===n&&(n=C),Array.isArray(t)||(t=[t]);var r=!1;n.d=1/0;for(var o=0;o<t.length;o++){var i=t[o];if(!(i.points.length>9)||L(e,G(i,D))){var s=w(e,i.points,!0);s&&s.d<n.d&&(R(s,n),n.shapeIndex=o,r=!0);}}return r?n: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 n=this.result.polygon.points,r=e.segIndex,o=n[r],i=n[r+1],s=this.result.segment;s.setTo(o.x,o.y,i.x,i.y),this.result.reflectAngle=I(this.ray,s);}else this.result.hit=!1;return !!e&&this.result}},{key:"rayToward",value:function(e,t,n){return _(this.ray,e,t,n,this.maxRayLength),this.hitTest()}}]),e}();return function(e){!function(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&&i(e,t);}(o,Phaser.Plugins.BasePlugin);var n=a(o);function o(e){return t(this,o),n.call(this,e)}return r(o,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"add",value:function(e){return new M(e)}}]),o}()}));