UNPKG

boid

Version:
2 lines (1 loc) 7.34 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Boid=e()}(this,function(){"use strict";function t(t,e){return t&&"undefined"!=typeof t[e]?t[e]:f[e]}function e(n){function i(t,e,n,i){return S.width=t,S.height=e,S.x=n||0,S.y=i||0,b}function r(){var t=S.x+M,e=S.x+S.width-M;B.x>e?(B.x=e,w.x*=-1):B.x<t&&(B.x=t,w.x*=-1);var n=S.y+M,i=S.y+S.height-M;B.y>i?(B.y=i,w.y*=-1):B.y<n&&(B.y=n,w.y*=-1)}function o(){var t=S.x-M,e=S.x+S.width+M;B.x>e?B.x=t:B.x<t&&(B.x=e);var n=S.y-M,i=S.y+S.height+M;B.y>i?B.y=n:B.y<n&&(B.y=i)}function s(t){var e=t.clone().subtract(B);e.normalize(),e.scaleBy(z);var n=e.subtract(w);return D.add(n),n.dispose(),b}function u(t){var e=t.clone().subtract(B);e.normalize(),e.scaleBy(z);var n=e.subtract(w);return D.subtract(n),n.dispose(),b}function a(t){var e=t.clone().subtract(B);e.normalize();var n=B.distanceSq(t);if(n>O)e.scaleBy(z);else{var i=z*n/O;e.scaleBy(i)}var r=e.subtract(w);return D.add(r),r.dispose(),b}function c(t){var e=B.distanceSq(t.position)/P,n=t.velocity.clone().scaleBy(e),i=t.position.clone().add(n);return s(i),n.dispose(),i.dispose(),b}function y(t){var e=B.distanceSq(t.position)/P,n=t.velocity.clone().scaleBy(e),i=t.position.clone().add(n);return u(i),n.dispose(),i.dispose(),b}function l(){var t=w.clone().normalize().scaleBy(T),e=d.get();e.set(j,k),j+=Math.random()*G-.5*G;var n=t.add(e);return D.add(n),e.dispose(),n.dispose(),b}function p(t){for(var e=0;e<t.length;e++){var n=t[e],i=w.clone().normalize(),r=n.position.clone().subtract(B),o=r.dotProduct(i);if(o>0){var s=i.clone().scaleBy(_),u=i.clone().scaleBy(o),a=u.subtract(r),c=a.length;if(c<(n.radius||0)+A&&u.length<s.length){var d=i.clone().scaleBy(z);d.angle+=r.sign(w)*h;var f=u.length/s.length;d.scaleBy(1-f),D.add(d),w.scaleBy(f),d.dispose()}s.dispose(),u.dispose(),a.dispose()}i.dispose(),r.dispose()}return b}function g(t,e){e=!!e;var n=t[C];return n?(B.distanceSq(n)<I&&(C>=t.length-1?e&&(C=0):C++),C>=t.length-1&&!e?a(n):s(n),b):(C=0,b)}function x(t){if(B.distanceSq(t.position)>W)return!1;var e=w.clone().normalize(),n=t.position.clone().subtract(B),i=n.dotProduct(e);return e.dispose(),n.dispose(),i>=0}function v(t){for(var e=w.clone(),n=d.get(),i=0,r=0;r<t.length;r++){var o=t[r];o!==b&&x(o)&&(e.add(o.velocity),n.add(o.position),B.distanceSq(o.position)<H&&u(o.position),i++)}return i>0&&(e.divideBy(i),n.divideBy(i),s(n),D.add(e.subtract(w))),e.dispose(),n.dispose(),b}function m(){return D.truncate(R),1!==E&&D.divideBy(E),w.x+=D.x,w.y+=D.y,D.x=0,D.y=0,w.truncate(z),B.x+=w.x,B.y+=w.y,q===e.EDGE_BOUNCE?r():q===e.EDGE_WRAP&&o(),b}var b=null,B=d.get(),w=d.get(),D=d.get(),S=Object.assign({},f.bounds,n&&n.bounds),q=t(n,"edgeBehavior"),E=t(n,"mass"),z=t(n,"maxSpeed"),P=z*z,R=t(n,"maxForce"),M=t(n,"radius"),N=t(n,"arriveThreshold"),O=N*N,T=t(n,"wanderDistance"),k=t(n,"wanderRadius"),j=t(n,"wanderAngle"),G=t(n,"wanderRange"),_=t(n,"avoidDistance"),A=t(n,"avoidBuffer"),C=0,F=t(n,"pathThreshold"),I=F*F,U=t(n,"maxDistance"),W=U*U,V=t(n,"minDistance"),H=V*V;return b={bounds:S,setBounds:i,update:m,pursue:c,evade:y,wander:l,avoid:p,followPath:g,flock:v,arrive:a,seek:s,flee:u,position:B,velocity:w,userData:{}},Object.defineProperties(b,{edgeBehavior:{get:function(){return q},set:function(t){q=t}},mass:{get:function(){return E},set:function(t){E=t}},maxSpeed:{get:function(){return z},set:function(t){z=t,P=t*t}},maxForce:{get:function(){return R},set:function(t){R=t}},radius:{get:function(){return M},set:function(t){M=t}},arriveThreshold:{get:function(){return N},set:function(t){N=t,O=t*t}},wanderDistance:{get:function(){return T},set:function(t){T=t}},wanderRadius:{get:function(){return k},set:function(t){k=t}},wanderRange:{get:function(){return G},set:function(t){G=t}},avoidDistance:{get:function(){return _},set:function(t){_=t}},avoidBuffer:{get:function(){return A},set:function(t){A=t}},pathIndex:{get:function(){return C},set:function(t){C=t}},pathThreshold:{get:function(){return F},set:function(t){F=t,I=t*t}},maxDistance:{get:function(){return U},set:function(t){U=t,W=t*t}},minDistance:{get:function(){return V},set:function(t){V=t,H=t*t}}}),Object.freeze(b)}var n=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},i=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),r=Math.acos,o=Math.atan2,s=Math.cos,u=Math.sin,a=Math.sqrt,c=[],d=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;n(this,t),this.x=e,this.y=i}return t.prototype.add=function(t){return this.x=this.x+t.x,this.y=this.y+t.y,this},t.prototype.subtract=function(t){return this.x=this.x-t.x,this.y=this.y-t.y,this},t.prototype.normalize=function(){var t=this.lengthSquared;if(0===t)return this.x=1,this;if(1===t)return this;var e=a(t);return this.x/=e,this.y/=e,this},t.prototype.isNormalized=function(){return 1===this.lengthSquared},t.prototype.truncate=function(t){return this.lengthSquared>t*t&&(this.length=t),this},t.prototype.scaleBy=function(t){return this.x*=t,this.y*=t,this},t.prototype.divideBy=function(t){return this.x/=t,this.y/=t,this},t.prototype.equals=function(t){return this.x===t.x&&this.y===t.y},t.prototype.negate=function(){return this.x=-this.x,this.y=-this.y,this},t.prototype.dotProduct=function(t){return this.x*t.x+this.y*t.y},t.prototype.crossProduct=function(t){return this.x*t.y-this.y*t.x},t.prototype.distanceSq=function(t){var e=t.x-this.x,n=t.y-this.y;return e*e+n*n},t.prototype.distance=function(t){return a(this.distanceSq(t))},t.prototype.clone=function(){return t.get(this.x,this.y)},t.prototype.reset=function(){return this.x=0,this.y=0,this},t.prototype.copy=function(t){return this.x=t.x,this.y=t.y,this},t.prototype.perpendicular=function(){return t.get(-this.y,this.x)},t.prototype.sign=function(t){var e=this.perpendicular(),n=e.dotProduct(t)<0?-1:1;return e.dispose(),n},t.prototype.set=function(t,e){return this.x=s(t)*e,this.y=u(t)*e,this},t.prototype.dispose=function(){this.x=0,this.y=0,c.push(this)},t.get=function(e,n){var i=c.length>0?c.pop():new t;return i.x=e||0,i.y=n||0,i},t.fill=function(e){for(;c.length<e;)c.push(new t)},t.angleBetween=function(t,e){return t.isNormalized()||(t=t.clone().normalize()),e.isNormalized()||(e=e.clone().normalize()),r(t.dotProduct(e))},i(t,[{key:"lengthSquared",get:function(){return this.x*this.x+this.y*this.y}},{key:"length",get:function(){return a(this.lengthSquared)},set:function(t){var e=this.angle;this.x=s(e)*t,this.y=u(e)*t}},{key:"angle",get:function(){return o(this.y,this.x)},set:function(t){var e=this.length;this.x=s(t)*e,this.y=u(t)*e}}]),t}(),h=Math.PI/2,f={bounds:{x:0,y:0,width:640,height:480},edgeBehavior:"bounce",mass:1,maxSpeed:10,maxForce:1,radius:0,arriveThreshold:50,wanderDistance:10,wanderRadius:5,wanderAngle:0,wanderRange:1,avoidDistance:300,avoidBuffer:20,pathThreshold:20,maxDistance:300,minDistance:60};return e.EDGE_NONE="none",e.EDGE_BOUNCE="bounce",e.EDGE_WRAP="wrap",e.Vec2=d,e.vec2=function(t,e){return d.get(t,e)},e.obstacle=function(t,e,n){return{radius:t,position:d.get(e,n)}},e});