package { public class Globals { public function Globals() { } ///Converts Degrees to radiants public static function degToRad($degrees):Number { return $degrees * (Math.PI / 180); } ///Input the x or y pixel-coords and recieve the grid coords public static function posToCoords($pos:Number) { return Math.floor($pos / 32); } ///Rounds off a number to a set ammount of decimals public static function round($v:Number, $decimals:int):Number { var mod:int = Math.pow(10, $decimals); return Math.round($v * mod) / mod; } ///Generates a random number between start and (end - 1) public static function random($start:Number, $end:Number):Number { if ($start >= $end) { var temp:Number = $start; $start = $end; $end = temp; } return (Math.random() * ($end - $start)) + $start; } ///Shuffles arrays public static function shuffleArray($array:Array) { for (var i:int = 0; i < $array.length; i++) { var randNum:Number = Math.floor(Math.random() * $array.length); var temp1 = $array[i]; var temp2 = $array[randNum]; $array[i] = temp2; $array[randNum] = temp1; } } //finds the quickest direction to turn, give negative results if the quickest dir is counter clock-wise public static function turningDir($targetX:int, $targetY:int, $thisX:int, $thisY:int, $thisRotation:int):Number { var degrees = Math.atan2($targetX - $thisX, $targetY - $thisY) * (180 / Math.PI); degrees = -degrees + 180; degrees = Math.round(degrees); //0 speed = 1, full speed = 0.5 //TorqueRate controls how fast the ship can turn, at 0% speed it turns at full rate, at 100% speed it turns at 50% of TorqueSpeed //converts values above 180 to between -1 and -180 if (degrees > 180) degrees = -(360 - degrees); //turns this ship the correct way towards the target if ((degrees < 0 && $thisRotation < 0) || (degrees >= 0 && $thisRotation >= 0))//target is on the same half-circle-side of the ships angle { return degrees - $thisRotation; } else//target is on the opposite right/left half-circle-side of what this ship is currently pointing at { var distCW:Number; var distCCW:Number; //calculate wether clock-wise or counter-clockwise or the shortest way to turn if ($thisRotation < 0)//left to right { distCW = Math.abs($thisRotation - degrees); distCCW = (180 - Math.abs($thisRotation)) + (180 - degrees); } else//right to left { distCW = (180 - $thisRotation) + (180 - Math.abs(degrees)); distCCW = Math.abs($thisRotation - degrees); } if (distCW > distCCW) return -distCCW;//ccw values are negative to indicate that they are ccw and not cw else return distCW;//cw values are positive to indicate that they are cw and not ccw } } //calculates distance in pixels public static function calcDist($targetX:int, $targetY:int, $thisX:int, $thisY:int):Number { var distX:Number = Math.abs($targetX - $thisX); var distY:Number = Math.abs($targetY - $thisY); return Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2)); } } }