mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 23:20:59 +00:00
1 line
144 KiB
JavaScript
1 line
144 KiB
JavaScript
|
var GameMath=(function(){function GameMath(game){this.globalSeed=Math.random();this._game=game}GameMath.PI=3.141592653589793;GameMath.PI_2=1.5707963267948966;GameMath.PI_4=0.7853981633974483;GameMath.PI_8=0.39269908169872414;GameMath.PI_16=0.19634954084936207;GameMath.TWO_PI=6.283185307179586;GameMath.THREE_PI_2=4.71238898038469;GameMath.E=2.71828182845905;GameMath.LN10=2.302585092994046;GameMath.LN2=0.6931471805599453;GameMath.LOG10E=0.4342944819032518;GameMath.LOG2E=1.4426950408889634;GameMath.SQRT1_2=0.7071067811865476;GameMath.SQRT2=1.4142135623730951;GameMath.DEG_TO_RAD=0.017453292519943295;GameMath.RAD_TO_DEG=57.29577951308232;GameMath.B_16=65536;GameMath.B_31=2147483648;GameMath.B_32=4294967296;GameMath.B_48=281474976710656;GameMath.B_53=9007199254740992;GameMath.B_64=18446744073709552000;GameMath.ONE_THIRD=0.3333333333333333;GameMath.TWO_THIRDS=0.6666666666666666;GameMath.ONE_SIXTH=0.16666666666666666;GameMath.COS_PI_3=0.8660254037844386;GameMath.SIN_2PI_3=0.03654595;GameMath.CIRCLE_ALPHA=0.5522847498307935;GameMath.ON=true;GameMath.OFF=false;GameMath.SHORT_EPSILON=0.1;GameMath.PERC_EPSILON=0.001;GameMath.EPSILON=0.0001;GameMath.LONG_EPSILON=1e-8;GameMath.prototype.computeMachineEpsilon=function(){var fourThirds=4/3;var third=fourThirds-1;var one=third+third+third;return Math.abs(1-one)};GameMath.prototype.fuzzyEqual=function(a,b,epsilon){if(typeof epsilon==="undefined"){epsilon=0.0001}return Math.abs(a-b)<epsilon};GameMath.prototype.fuzzyLessThan=function(a,b,epsilon){if(typeof epsilon==="undefined"){epsilon=0.0001}return a<b+epsilon};GameMath.prototype.fuzzyGreaterThan=function(a,b,epsilon){if(typeof epsilon==="undefined"){epsilon=0.0001}return a>b-epsilon};GameMath.prototype.fuzzyCeil=function(val,epsilon){if(typeof epsilon==="undefined"){epsilon=0.0001}return Math.ceil(val-epsilon)};GameMath.prototype.fuzzyFloor=function(val,epsilon){if(typeof epsilon==="undefined"){epsilon=0.0001}return Math.floor(val+epsilon)};GameMath.prototype.average=function(){var args=[];for(var _i=0;_i<(arguments.length-0);_i++){args[_i]=arguments[_i+0]}var avg=0;for(var i=0;i<args.length;i++){avg+=args[i]}return avg/args.length};GameMath.prototype.slam=function(value,target,epsilon){if(typeof epsilon==="undefined"){epsilon=0.0001}return(Math.abs(value-target)<epsilon)?target:value};GameMath.prototype.percentageMinMax=function(val,max,min){if(typeof min==="undefined"){min=0}val-=min;max-=min;if(!max){return 0}else{return val/max}};GameMath.prototype.sign=function(n){if(n){return n/Math.abs(n)}else{return 0}};GameMath.prototype.truncate=function(n){return(n>0)?Math.floor(n):Math.ceil(n)};GameMath.prototype.shear=function(n){return n%1};GameMath.prototype.wrap=function(val,max,min){if(typeof min==="undefined"){min=0}val-=min;max-=min;if(max==0){return min}val%=max;val+=min;while(val<min){val+=max}return val};GameMath.prototype.arithWrap=function(value,max,min){if(typeof min==="undefined"){min=0}max-=min;if(max==0){return min}return value-max*Math.floor((value-min)/max)};GameMath.prototype.clamp=function(input,max,min){if(typeof min==="undefined"){min=0}return Math.max(min,Math.min(max,input))};GameMath.prototype.snapTo=function(input,gap,start){if(typeof start==="undefined"){start=0}if(gap==0){return input}input-=start;input=gap*Math.round(input/gap);return start+input};GameMath.prototype.snapToFloor=function(input,gap,start){if(typeof start==="undefined"){start=0}if(gap==0){return input}input-=start;input=gap*Math.floor(input/gap);return start+input};GameMath.prototype.snapToCeil=function(input,gap,start){if(typeof start==="undefined"){start=0}if(gap==0){return input}input-=start;input=gap*Math.ceil(input/gap);return start+input};GameMath.prototype.snapToInArray=function(input,arr,sort){if(typeof sort==="undefined"){sort=true}if(sort){arr.sort()}if(input<arr[0]){return arr[0]}var i=1;while(arr[i]<input){i++}var low=arr[i-1];var high=(i<arr.length)?arr[i]:Number.POSITIVE_INFINITY;return((high-input)<=(input-low))?high:low};GameMath.prototype.roundTo=function(value,place,base){if(typeof place==="undefined"){place=0}if(typeof base==="und
|