-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcrash.min.js
More file actions
1 lines (1 loc) · 5.94 KB
/
Copy pathcrash.min.js
File metadata and controls
1 lines (1 loc) · 5.94 KB
1
!function(t){"use strict";"function"==typeof define&&define.amd?define(["RBush","SAT"],t):"object"==typeof exports?module.exports=t(require("rbush"),require("sat")):window.Crash=t(window.rbush,window.SAT)}(function(e,p){"use strict";var s={},i=[],r=function(t){t||(t={}),this.__options={maxEntries:t.maxEntries||9,maxChecks:"number"==typeof t.maxChecks?t.maxChecks:100,overlapLimit:"number"==typeof t.overlapLimit||!1===t.overlapLimit?t.overlapLimit:.5},this.rbush=new e(this.__options.maxEntries,[".aabb.x1",".aabb.y1",".aabb.x2",".aabb.y2"]),this.MAX_CHECKS=this.__options.maxChecks,this.OVERLAP_LIMIT=this.__options.overlapLimit,this.RESPONSE=new p.Response,this.BREAK=!1,this.__moved=[],this.__listeners=[],this.createColliders(this)};return r.RBush=r.prototype.RBush=e,r.SAT=r.prototype.SAT=p,r.Vector=r.prototype.Vector=p.Vector,r.V=r.prototype.V=p.Vector,r.Response=r.prototype.Response=p.Response,r.extend=r.prototype.extend=function(t,e){t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}})},r.getTestString=r.prototype.getTestString=function(t,e){return"circle"===t?"circle"===e?"testCircleCircle":"testCirclePolygon":"circle"===e?"testPolygonCircle":"testPolygonPolygon"},r.prototype.reset=function(){return this.clear(),this.__listeners=[],this.BREAK=!1,this.MAX_CHECKS=this.__options.maxChecks,this.OVERLAP_LIMIT=this.__options.overlapLimit,this.RESPONSE.clear(),this},r.prototype.onCollision=function(t){return this.__listeners.push(t),this},r.prototype.offCollision=function(t){var e=this.__listeners.indexOf(t);return-1<e&&this.__listeners.splice(e,1),this},r.prototype.__onCollision=function(t,e,o){for(var s=0,i=this.__listeners.length;s<i;s++)this.__listeners[s].call(this,t,e,o,this.cancel);return this},r.prototype.cancel=function(){return!(this.BREAK=!0)},r.prototype.insert=function(t){return this.rbush.insert(t),this},r.prototype.remove=function(t){return this.rbush.remove(t),this},r.prototype.all=function(){return this.rbush.all()},r.prototype.search=function(t){s.minX=t.aabb.x1,s.minY=t.aabb.y1,s.maxX=t.aabb.x2,s.maxY=t.aabb.y2;for(var e=this.rbush.search(s),o=0;o<e.length;o++)e[o]===t&&e.splice(o,1);return e},r.prototype.clear=function(){return this.rbush.clear(),this.__moved=[],this},r.prototype.addToMoved=function(t){return-1===this.__moved.indexOf(t)&&this.__moved.push(t),this},r.prototype.update=function(t){return this.remove(t),this.updateAABB(t),this.insert(t),this},r.prototype.moved=function(t){return this.update(t),this.addToMoved(t),this},r.updateAABB=r.prototype.updateAABB=function(t){switch(t.type){case"polygon":return this.updateAABBPolygon(t);case"box":return this.updateAABBBox(t);case"circle":return this.updateAABBCircle(t);case"point":return this.updateAABBPoint(t)}},r.updateAABBPolygon=r.prototype.updateAABBPolygon=function(t){for(var e=t.aabb,o=t.sat.pos,s=t.sat.calcPoints,i=s.length,r=s[0].x,n=s[0].y,a=s[0].x,p=s[0].y,h=1;h<i;h++){var u=s[h];u.x<r?r=u.x:u.x>a&&(a=u.x),u.y<n?n=u.y:u.y>p&&(p=u.y)}e.x1=o.x+r,e.y1=o.y+n,e.x2=o.x+a,e.y2=o.y+p},r.updateAABBCircle=r.prototype.updateAABBCircle=function(t){var e=t.aabb,o=t.sat.r,s=t.sat.pos;e.x1=s.x-o,e.y1=s.y-o,e.x2=s.x+o,e.y2=s.y+o},r.updateAABBPoint=r.prototype.updateAABBPoint=function(t){var e=t.aabb,o=t.sat.pos;e.x1=e.x2=o.x,e.y1=e.y2=o.x},r.updateAABBBox=r.prototype.updateAABBBox=function(t){var e=t.sat.calcPoints,o=t.aabb,s=t.sat.pos;o.x1=s.x+e[0].x,o.y1=s.y+e[0].y,o.x2=s.x+e[2].x,o.y2=s.y+e[2].y},r.test=r.prototype.test=function(t,e,o){o=o||this.RESPONSE;var s=this.getTestString(t.type,e.type);return o.clear(),p[s](t.sat,e.sat,o)},r.prototype.testAll=function(t,e){e=e||this.RESPONSE;var o=this.search(t);t:for(var s=0,i=o.length;s<i;s++){var r=o[s],n=this.getTestString(t.type,r.type);if(e.clear(),p[n](t.sat,r.sat,e)&&(!this.OVERLAP_LIMIT||Math.abs(e.overlap)>this.OVERLAP_LIMIT)&&(this.__onCollision(t,r,e),this.BREAK))break t}t.lastPos.copy(t.pos);var a=this.BREAK;return this.BREAK=!1,!a},r.prototype.check=function(t){for(var e=0;this.__moved.length&&e<this.MAX_CHECKS;){var o=this.__moved.pop();-1===i.indexOf(o)&&i.push(o),this.testAll(o,t),e++}e=0;for(var s=i.length;e<s;e++)i[e].lastCheckedPos.copy(i[e].pos);return i.length=0,this},r.prototype.checkAll=function(t){for(var e=this.all(),o=0,s=e.length;o<s;o++)this.testAll(e[o],t);return this.check(t),this},r.createColliders=r.prototype.createColliders=function(i){var n=i.Collider=function(t,e,o,s){return this.type=t,this.sat=e,this.data=s,this.pos=this.sat.pos,this.lastPos=this.pos.clone(),this.lastCheckedPos=this.pos.clone(),this.aabb={},i.updateAABB(this),o&&i.insert(this),this};n.prototype.insert=function(){return i.insert(this),this},n.prototype.remove=function(){return i.remove(this),this},n.prototype.update=function(){return i.update(this),this},n.prototype.updateAABB=function(){return i.updateAABB(this),this},n.prototype.moved=function(){return i.moved(this),this},n.prototype.search=function(){return i.search(this)},n.prototype.setData=function(t){return this.data=t,this},n.prototype.getData=function(){return this.data},n.prototype.moveTo=function(t,e){return this.sat.pos.x=t,this.sat.pos.y=e,this.moved(),this},n.prototype.moveBy=n.prototype.move=function(t,e){return this.sat.pos.x+=t,this.sat.pos.y+=e,this.moved(),this};var t=i.Polygon=function(t,e,o,s){var i=new p.Polygon(t,e);return n.call(this,"polygon",i,o,s),this};r.extend(t,n),t.prototype.setPoints=function(t){return this.sat.setPoints(t),this.moved(),this},t.prototype.setAngle=function(t){return this.sat.setAngle(t),this.moved(),this},t.prototype.setOffset=function(t){return this.sat.setOffset(t),this.moved(),this},t.prototype.rotate=function(t){return this.sat.rotate(t),this.moved(),this};var e=i.Circle=function(t,e,o,s){var i=new p.Circle(t,e);return n.call(this,"circle",i,o,s),this};r.extend(e,n);var o=i.Point=function(t,e,o){var s=new p.Box(t,1,1).toPolygon();return n.call(this,"point",s,e,o),this};r.extend(o,n);var s=i.Box=function(t,e,o,s,i){var r=new p.Box(t,e,o).toPolygon();return n.call(this,"box",r,s,i),this};r.extend(s,n)},r});