PageRenderTime 145ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/net/flashpunk/FP.as

https://github.com/jakevsrobots/Infinite-Friends
ActionScript | 822 lines | 464 code | 64 blank | 294 comment | 91 complexity | 544f419b82fb7f477c5e3aae3c5ff4b2 MD5 | raw file
  1. package net.flashpunk
  2. {
  3. import flash.display.BitmapData;
  4. import flash.display.Sprite;
  5. import flash.display.Stage;
  6. import flash.geom.Matrix;
  7. import flash.geom.Point;
  8. import flash.geom.Rectangle;
  9. import flash.media.SoundMixer;
  10. import flash.media.SoundTransform;
  11. import flash.system.System;
  12. import flash.utils.ByteArray;
  13. import flash.utils.getTimer;
  14. import net.flashpunk.*;
  15. import net.flashpunk.debug.Console;
  16. import net.flashpunk.tweens.misc.MultiVarTween;
  17. /**
  18. * Static catch-all class used to access global properties and functions.
  19. */
  20. public class FP
  21. {
  22. /**
  23. * The FlashPunk major version.
  24. */
  25. public static const VERSION:String = "1.4";
  26. /**
  27. * Width of the game.
  28. */
  29. public static var width:uint;
  30. /**
  31. * Height of the game.
  32. */
  33. public static var height:uint;
  34. /**
  35. * If the game is running at a fixed framerate.
  36. */
  37. public static var fixed:Boolean;
  38. /**
  39. * The framerate assigned to the stage.
  40. */
  41. public static var frameRate:Number;
  42. /**
  43. * The framerate assigned to the stage.
  44. */
  45. public static var assignedFrameRate:Number;
  46. /**
  47. * Time elapsed since the last frame (non-fixed framerate only).
  48. */
  49. public static var elapsed:Number;
  50. /**
  51. * Timescale applied to FP.elapsed (non-fixed framerate only).
  52. */
  53. public static var rate:Number = 1;
  54. /**
  55. * The Screen object, use to transform or offset the Screen.
  56. */
  57. public static var screen:Screen;
  58. /**
  59. * The current screen buffer, drawn to in the render loop.
  60. */
  61. public static var buffer:BitmapData;
  62. /**
  63. * A rectangle representing the size of the screen.
  64. */
  65. public static var bounds:Rectangle;
  66. /**
  67. * Point used to determine drawing offset in the render loop.
  68. */
  69. public static var camera:Point = new Point;
  70. /**
  71. * The currently active World object. When you set this, the World is flagged
  72. * to switch, but won't actually do so until the end of the current frame.
  73. */
  74. public static function get world():World { return _world; }
  75. public static function set world(value:World):void
  76. {
  77. if (_world == value) return;
  78. _goto = value;
  79. }
  80. /**
  81. * Resets the camera position.
  82. */
  83. public static function resetCamera():void
  84. {
  85. camera.x = camera.y = 0;
  86. }
  87. /**
  88. * Global volume factor for all sounds, a value from 0 to 1.
  89. */
  90. public static function get volume():Number { return _volume; }
  91. public static function set volume(value:Number):void
  92. {
  93. if (value < 0) value = 0;
  94. if (_volume == value) return;
  95. _soundTransform.volume = _volume = value;
  96. SoundMixer.soundTransform = _soundTransform;
  97. }
  98. /**
  99. * Global panning factor for all sounds, a value from -1 to 1.
  100. */
  101. public static function get pan():Number { return _pan; }
  102. public static function set pan(value:Number):void
  103. {
  104. if (value < -1) value = -1;
  105. if (value > 1) value = 1;
  106. if (_pan == value) return;
  107. _soundTransform.pan = _pan = value;
  108. SoundMixer.soundTransform = _soundTransform;
  109. }
  110. /**
  111. * Randomly chooses and returns one of the provided values.
  112. * @param ...objs The Objects you want to randomly choose from. Can be ints, Numbers, Points, etc.
  113. * @return A randomly chosen one of the provided parameters.
  114. */
  115. public static function choose(...objs):*
  116. {
  117. var c:* = (objs.length == 1 && (objs[0] is Array || objs[0] is Vector.<*>)) ? objs[0] : objs;
  118. return c[rand(c.length)];
  119. }
  120. /**
  121. * Finds the sign of the provided value.
  122. * @param value The Number to evaluate.
  123. * @return 1 if value > 0, -1 if value < 0, and 0 when value == 0.
  124. */
  125. public static function sign(value:Number):int
  126. {
  127. return value < 0 ? -1 : (value > 0 ? 1 : 0);
  128. }
  129. /**
  130. * Approaches the value towards the target, by the specified amount, without overshooting the target.
  131. * @param value The starting value.
  132. * @param target The target that you want value to approach.
  133. * @param amount How much you want the value to approach target by.
  134. * @return The new value.
  135. */
  136. public static function approach(value:Number, target:Number, amount:Number):Number
  137. {
  138. return value < target ? (target < value + amount ? target : value + amount) : (target > value - amount ? target : value - amount);
  139. }
  140. /**
  141. * Linear interpolation between two values.
  142. * @param a First value.
  143. * @param b Second value.
  144. * @param t Interpolation factor.
  145. * @return When t=0, returns a. When t=1, returns b. When t=0.5, will return halfway between a and b. Etc.
  146. */
  147. public static function lerp(a:Number, b:Number, t:Number = 1):Number
  148. {
  149. return a + (b - a) * t;
  150. }
  151. /**
  152. * Linear interpolation between two colors.
  153. * @param fromColor First color.
  154. * @param toColor Second color.
  155. * @param t Interpolation value. Clamped to the range [0, 1].
  156. * return RGB component-interpolated color value.
  157. */
  158. public static function colorLerp(fromColor:uint, toColor:uint, t:Number = 1):uint
  159. {
  160. if (t <= 0) { return fromColor; }
  161. if (t >= 1) { return toColor; }
  162. var a:uint = fromColor >> 24 & 0xFF,
  163. r:uint = fromColor >> 16 & 0xFF,
  164. g:uint = fromColor >> 8 & 0xFF,
  165. b:uint = fromColor & 0xFF,
  166. dA: int = (toColor >> 24 & 0xFF) - a,
  167. dR: int = (toColor >> 16 & 0xFF) - r,
  168. dG: int = (toColor >> 8 & 0xFF) - g,
  169. dB: int = (toColor & 0xFF) - b;
  170. a += dA * t;
  171. r += dR * t;
  172. g += dG * t;
  173. b += dB * t;
  174. return a << 24 | r << 16 | g << 8 | b;
  175. }
  176. /**
  177. * Steps the object towards a point.
  178. * @param object Object to move (must have an x and y property).
  179. * @param x X position to step towards.
  180. * @param y Y position to step towards.
  181. * @param distance The distance to step (will not overshoot target).
  182. */
  183. public static function stepTowards(object:Object, x:Number, y:Number, distance:Number = 1):void
  184. {
  185. point.x = x - object.x;
  186. point.y = y - object.y;
  187. if (point.length <= distance)
  188. {
  189. object.x = x;
  190. object.y = y;
  191. return;
  192. }
  193. point.normalize(distance);
  194. object.x += point.x;
  195. object.y += point.y;
  196. }
  197. /**
  198. * Finds the angle (in degrees) from point 1 to point 2.
  199. * @param x1 The first x-position.
  200. * @param y1 The first y-position.
  201. * @param x2 The second x-position.
  202. * @param y2 The second y-position.
  203. * @return The angle from (x1, y1) to (x2, y2).
  204. */
  205. public static function angle(x1:Number, y1:Number, x2:Number, y2:Number):Number
  206. {
  207. var a:Number = Math.atan2(y2 - y1, x2 - x1) * DEG;
  208. return a < 0 ? a + 360 : a;
  209. }
  210. /**
  211. * Sets the x/y values of the provided object to a vector of the specified angle and length.
  212. * @param object The object whose x/y properties should be set.
  213. * @param angle The angle of the vector, in degrees.
  214. * @param length The distance to the vector from (0, 0).
  215. */
  216. public static function angleXY(object:Object, angle:Number, length:Number = 1):void
  217. {
  218. angle *= RAD;
  219. object.x = Math.cos(angle) * length;
  220. object.y = Math.sin(angle) * length;
  221. }
  222. /**
  223. * Find the distance between two points.
  224. * @param x1 The first x-position.
  225. * @param y1 The first y-position.
  226. * @param x2 The second x-position.
  227. * @param y2 The second y-position.
  228. * @return The distance.
  229. */
  230. public static function distance(x1:Number, y1:Number, x2:Number = 0, y2:Number = 0):Number
  231. {
  232. return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  233. }
  234. /**
  235. * Find the distance between two rectangles. Will return 0 if the rectangles overlap.
  236. * @param x1 The x-position of the first rect.
  237. * @param y1 The y-position of the first rect.
  238. * @param w1 The width of the first rect.
  239. * @param h1 The height of the first rect.
  240. * @param x2 The x-position of the second rect.
  241. * @param y2 The y-position of the second rect.
  242. * @param w2 The width of the second rect.
  243. * @param h2 The height of the second rect.
  244. * @return The distance.
  245. */
  246. public static function distanceRects(x1:Number, y1:Number, w1:Number, h1:Number, x2:Number, y2:Number, w2:Number, h2:Number):Number
  247. {
  248. if (x1 < x2 + w2 && x2 < x1 + w1)
  249. {
  250. if (y1 < y2 + h2 && y2 < y1 + h1) return 0;
  251. if (y1 > y2) return y1 - (y2 + h2);
  252. return y2 - (y1 + h1);
  253. }
  254. if (y1 < y2 + h2 && y2 < y1 + h1)
  255. {
  256. if (x1 > x2) return x1 - (x2 + w2);
  257. return x2 - (x1 + w1)
  258. }
  259. if (x1 > x2)
  260. {
  261. if (y1 > y2) return distance(x1, y1, (x2 + w2), (y2 + h2));
  262. return distance(x1, y1 + h1, x2 + w2, y2);
  263. }
  264. if (y1 > y2) return distance(x1 + w1, y1, x2, y2 + h2)
  265. return distance(x1 + w1, y1 + h1, x2, y2);
  266. }
  267. /**
  268. * Find the distance between a point and a rectangle. Returns 0 if the point is within the rectangle.
  269. * @param px The x-position of the point.
  270. * @param py The y-position of the point.
  271. * @param rx The x-position of the rect.
  272. * @param ry The y-position of the rect.
  273. * @param rw The width of the rect.
  274. * @param rh The height of the rect.
  275. * @return The distance.
  276. */
  277. public static function distanceRectPoint(px:Number, py:Number, rx:Number, ry:Number, rw:Number, rh:Number):Number
  278. {
  279. if (px >= rx && px <= rx + rw)
  280. {
  281. if (py >= ry && py <= ry + rh) return 0;
  282. if (py > ry) return py - (ry + rh);
  283. return ry - py;
  284. }
  285. if (py >= ry && py <= ry + rh)
  286. {
  287. if (px > rx) return px - (rx + rw);
  288. return rx - px;
  289. }
  290. if (px > rx)
  291. {
  292. if (py > ry) return distance(px, py, rx + rw, ry + rh);
  293. return distance(px, py, rx + rw, ry);
  294. }
  295. if (py > ry) return distance(px, py, rx, ry + rh)
  296. return distance(px, py, rx, ry);
  297. }
  298. /**
  299. * Clamps the value within the minimum and maximum values.
  300. * @param value The Number to evaluate.
  301. * @param min The minimum range.
  302. * @param max The maximum range.
  303. * @return The clamped value.
  304. */
  305. public static function clamp(value:Number, min:Number, max:Number):Number
  306. {
  307. if (max > min)
  308. {
  309. value = value < max ? value : max;
  310. return value > min ? value : min;
  311. }
  312. value = value < min ? value : min;
  313. return value > max ? value : max;
  314. }
  315. /**
  316. * Transfers a value from one scale to another scale. For example, scale(.5, 0, 1, 10, 20) == 15, and scale(3, 0, 5, 100, 0) == 40.
  317. * @param value The value on the first scale.
  318. * @param min The minimum range of the first scale.
  319. * @param max The maximum range of the first scale.
  320. * @param min2 The minimum range of the second scale.
  321. * @param max2 The maximum range of the second scale.
  322. * @return The scaled value.
  323. */
  324. public static function scale(value:Number, min:Number, max:Number, min2:Number, max2:Number):Number
  325. {
  326. return min2 + ((value - min) / (max - min)) * (max2 - min2);
  327. }
  328. /**
  329. * Transfers a value from one scale to another scale, but clamps the return value within the second scale.
  330. * @param value The value on the first scale.
  331. * @param min The minimum range of the first scale.
  332. * @param max The maximum range of the first scale.
  333. * @param min2 The minimum range of the second scale.
  334. * @param max2 The maximum range of the second scale.
  335. * @return The scaled and clamped value.
  336. */
  337. public static function scaleClamp(value:Number, min:Number, max:Number, min2:Number, max2:Number):Number
  338. {
  339. value = min2 + ((value - min) / (max - min)) * (max2 - min2);
  340. if (max2 > min2)
  341. {
  342. value = value < max2 ? value : max2;
  343. return value > min2 ? value : min2;
  344. }
  345. value = value < min2 ? value : min2;
  346. return value > max2 ? value : max2;
  347. }
  348. /**
  349. * The random seed used by FP's random functions.
  350. */
  351. public static function get randomSeed():uint { return _getSeed; }
  352. public static function set randomSeed(value:uint):void
  353. {
  354. _seed = clamp(value, 1, 2147483646);
  355. _getSeed = _seed;
  356. }
  357. /**
  358. * Randomizes the random seed using Flash's Math.random() function.
  359. */
  360. public static function randomizeSeed():void
  361. {
  362. randomSeed = 2147483647 * Math.random();
  363. }
  364. /**
  365. * A pseudo-random Number produced using FP's random seed, where 0 <= Number < 1.
  366. */
  367. public static function get random():Number
  368. {
  369. _seed = (_seed * 16807) % 2147483647;
  370. return _seed / 2147483647;
  371. }
  372. /**
  373. * Returns a pseudo-random uint.
  374. * @param amount The returned uint will always be 0 <= uint < amount.
  375. * @return The uint.
  376. */
  377. public static function rand(amount:uint):uint
  378. {
  379. _seed = (_seed * 16807) % 2147483647;
  380. return (_seed / 2147483647) * amount;
  381. }
  382. /**
  383. * Returns the next item after current in the list of options.
  384. * @param current The currently selected item (must be one of the options).
  385. * @param options An array of all the items to cycle through.
  386. * @param loop If true, will jump to the first item after the last item is reached.
  387. * @return The next item in the list.
  388. */
  389. public static function next(current:*, options:Array, loop:Boolean = true):*
  390. {
  391. if (loop) return options[(options.indexOf(current) + 1) % options.length];
  392. return options[Math.max(options.indexOf(current) + 1, options.length - 1)];
  393. }
  394. /**
  395. * Returns the item previous to the current in the list of options.
  396. * @param current The currently selected item (must be one of the options).
  397. * @param options An array of all the items to cycle through.
  398. * @param loop If true, will jump to the last item after the first is reached.
  399. * @return The previous item in the list.
  400. */
  401. public static function prev(current:*, options:Array, loop:Boolean = true):*
  402. {
  403. if (loop) return options[((options.indexOf(current) - 1) + options.length) % options.length];
  404. return options[Math.max(options.indexOf(current) - 1, 0)];
  405. }
  406. /**
  407. * Swaps the current item between a and b. Useful for quick state/string/value swapping.
  408. * @param current The currently selected item.
  409. * @param a Item a.
  410. * @param b Item b.
  411. * @return Returns a if current is b, and b if current is a.
  412. */
  413. public static function swap(current:*, a:*, b:*):*
  414. {
  415. return current == a ? b : a;
  416. }
  417. /**
  418. * Creates a color value by combining the chosen RGB values.
  419. * @param R The red value of the color, from 0 to 255.
  420. * @param G The green value of the color, from 0 to 255.
  421. * @param B The blue value of the color, from 0 to 255.
  422. * @return The color uint.
  423. */
  424. public static function getColorRGB(R:uint = 0, G:uint = 0, B:uint = 0):uint
  425. {
  426. return R << 16 | G << 8 | B;
  427. }
  428. /**
  429. * Creates a color value with the chosen HSV values.
  430. * @param h The hue of the color (from 0 to 1).
  431. * @param s The saturation of the color (from 0 to 1).
  432. * @param v The value of the color (from 0 to 1).
  433. * @return The color uint.
  434. */
  435. public static function getColorHSV(h:Number, s:Number, v:Number):uint
  436. {
  437. h = int(h * 360);
  438. var hi:int = Math.floor(h / 60) % 6,
  439. f:Number = h / 60 - Math.floor(h / 60),
  440. p:Number = (v * (1 - s)),
  441. q:Number = (v * (1 - f * s)),
  442. t:Number = (v * (1 - (1 - f) * s));
  443. switch (hi)
  444. {
  445. case 0: return int(v * 255) << 16 | int(t * 255) << 8 | int(p * 255);
  446. case 1: return int(q * 255) << 16 | int(v * 255) << 8 | int(p * 255);
  447. case 2: return int(p * 255) << 16 | int(v * 255) << 8 | int(t * 255);
  448. case 3: return int(p * 255) << 16 | int(q * 255) << 8 | int(v * 255);
  449. case 4: return int(t * 255) << 16 | int(p * 255) << 8 | int(v * 255);
  450. case 5: return int(v * 255) << 16 | int(p * 255) << 8 | int(q * 255);
  451. default: return 0;
  452. }
  453. return 0;
  454. }
  455. /**
  456. * Finds the red factor of a color.
  457. * @param color The color to evaluate.
  458. * @return A uint from 0 to 255.
  459. */
  460. public static function getRed(color:uint):uint
  461. {
  462. return color >> 16 & 0xFF;
  463. }
  464. /**
  465. * Finds the green factor of a color.
  466. * @param color The color to evaluate.
  467. * @return A uint from 0 to 255.
  468. */
  469. public static function getGreen(color:uint):uint
  470. {
  471. return color >> 8 & 0xFF;
  472. }
  473. /**
  474. * Finds the blue factor of a color.
  475. * @param color The color to evaluate.
  476. * @return A uint from 0 to 255.
  477. */
  478. public static function getBlue(color:uint):uint
  479. {
  480. return color & 0xFF;
  481. }
  482. /**
  483. * Fetches a stored BitmapData object represented by the source.
  484. * @param source Embedded Bitmap class.
  485. * @return The stored BitmapData object.
  486. */
  487. public static function getBitmap(source:Class):BitmapData
  488. {
  489. if (_bitmap[String(source)]) return _bitmap[String(source)];
  490. return (_bitmap[String(source)] = (new source).bitmapData);
  491. }
  492. /**
  493. * Sets a time flag.
  494. * @return Time elapsed (in milliseconds) since the last time flag was set.
  495. */
  496. public static function timeFlag():uint
  497. {
  498. var t:uint = getTimer(),
  499. e:uint = t - _time;
  500. _time = t;
  501. return e;
  502. }
  503. /**
  504. * The global Console object.
  505. */
  506. public static function get console():Console
  507. {
  508. if (!_console) _console = new Console;
  509. return _console;
  510. }
  511. /**
  512. * Logs data to the console.
  513. * @param ...data The data parameters to log, can be variables, objects, etc. Parameters will be separated by a space (" ").
  514. */
  515. public static function log(...data):void
  516. {
  517. if (_console)
  518. {
  519. if (data.length > 1)
  520. {
  521. var i:int = 0, s:String = "";
  522. while (i < data.length)
  523. {
  524. if (i > 0) s += " ";
  525. s += data[i ++].toString();
  526. }
  527. _console.log(s);
  528. }
  529. else _console.log(data[0]);
  530. }
  531. }
  532. /**
  533. * Adds properties to watch in the console's debug panel.
  534. * @param ...properties The properties (strings) to watch.
  535. */
  536. public static function watch(...properties):void
  537. {
  538. if (_console)
  539. {
  540. if (properties.length > 1) _console.watch(properties);
  541. else _console.watch(properties[0]);
  542. }
  543. }
  544. /**
  545. * Loads the file as an XML object.
  546. * @param file The embedded file to load.
  547. * @return An XML object representing the file.
  548. */
  549. public static function getXML(file:Class):XML
  550. {
  551. var bytes:ByteArray = new file;
  552. return XML(bytes.readUTFBytes(bytes.length));
  553. }
  554. /**
  555. * Tweens numeric public properties of an Object. Shorthand for creating a MultiVarTween tween, starting it and adding it to a Tweener.
  556. * @param object The object containing the properties to tween.
  557. * @param values An object containing key/value pairs of properties and target values.
  558. * @param duration Duration of the tween.
  559. * @param options An object containing key/value pairs of the following optional parameters:
  560. * type Tween type.
  561. * complete Optional completion callback function.
  562. * ease Optional easer function.
  563. * tweener The Tweener to add this Tween to.
  564. * @return The added MultiVarTween object.
  565. *
  566. * Example: FP.tween(object, { x: 500, y: 350 }, 2.0, { ease: easeFunction, complete: onComplete } );
  567. */
  568. public static function tween(object:Object, values:Object, duration:Number, options:Object = null):MultiVarTween
  569. {
  570. var type:uint = Tween.ONESHOT,
  571. complete:Function = null,
  572. ease:Function = null,
  573. tweener:Tweener = FP.world;
  574. if (object is Tweener) tweener = object as Tweener;
  575. if (options)
  576. {
  577. if (options.hasOwnProperty("type")) type = options.type;
  578. if (options.hasOwnProperty("complete")) complete = options.complete;
  579. if (options.hasOwnProperty("ease")) ease = options.ease;
  580. if (options.hasOwnProperty("tweener")) tweener = options.tweener;
  581. }
  582. var tween:MultiVarTween = new MultiVarTween(complete, type);
  583. tween.tween(object, values, duration, ease);
  584. tweener.addTween(tween);
  585. return tween;
  586. }
  587. /**
  588. * Gets an array of frame indices.
  589. * @param from Starting frame.
  590. * @param to Ending frame.
  591. * @param skip Skip amount every frame (eg. use 1 for every 2nd frame).
  592. */
  593. public static function frames(from:int, to:int, skip:int = 0):Array
  594. {
  595. var a:Array = [];
  596. skip ++;
  597. if (from < to)
  598. {
  599. while (from <= to)
  600. {
  601. a.push(from);
  602. from += skip;
  603. }
  604. }
  605. else
  606. {
  607. while (from >= to)
  608. {
  609. a.push(from);
  610. from -= skip;
  611. }
  612. }
  613. return a;
  614. }
  615. /**
  616. * Shuffles the elements in the array.
  617. * @param a The Object to shuffle (an Array or Vector).
  618. */
  619. public static function shuffle(a:Object):void
  620. {
  621. if (a is Array || a is Vector.<*>)
  622. {
  623. var i:int = a.length, j:int, t:*;
  624. while (-- i)
  625. {
  626. t = a[i];
  627. a[i] = a[j = FP.rand(i + 1)];
  628. a[j] = t;
  629. }
  630. }
  631. }
  632. /**
  633. * Sorts the elements in the array.
  634. * @param object The Object to sort (an Array or Vector).
  635. * @param ascending If it should be sorted ascending (true) or descending (false).
  636. */
  637. public static function sort(object:Object, ascending:Boolean = true):void
  638. {
  639. if (object is Array || object is Vector.<*>) quicksort(object, 0, object.length - 1, ascending);
  640. }
  641. /**
  642. * Sorts the elements in the array by a property of the element.
  643. * @param object The Object to sort (an Array or Vector).
  644. * @param property The numeric property of object's elements to sort by.
  645. * @param ascending If it should be sorted ascending (true) or descending (false).
  646. */
  647. public static function sortBy(object:Object, property:String, ascending:Boolean = true):void
  648. {
  649. if (object is Array || object is Vector.<*>) quicksortBy(object, 0, object.length - 1, ascending, property);
  650. }
  651. /** @private Quicksorts the array. */
  652. private static function quicksort(a:Object, left:int, right:int, ascending:Boolean):void
  653. {
  654. var i:int = left, j:int = right, t:Number,
  655. p:* = a[Math.round((left + right) * .5)];
  656. if (ascending)
  657. {
  658. while (i <= j)
  659. {
  660. while (a[i] < p) i ++;
  661. while (a[j] > p) j --;
  662. if (i <= j)
  663. {
  664. t = a[i];
  665. a[i ++] = a[j];
  666. a[j --] = t;
  667. }
  668. }
  669. }
  670. else
  671. {
  672. while (i <= j)
  673. {
  674. while (a[i] > p) i ++;
  675. while (a[j] < p) j --;
  676. if (i <= j)
  677. {
  678. t = a[i];
  679. a[i ++] = a[j];
  680. a[j --] = t;
  681. }
  682. }
  683. }
  684. if (left < j) quicksort(a, left, j, ascending);
  685. if (i < right) quicksort(a, i, right, ascending);
  686. }
  687. /** @private Quicksorts the array by the property. */
  688. private static function quicksortBy(a:Object, left:int, right:int, ascending:Boolean, property:String):void
  689. {
  690. var i:int = left, j:int = right, t:Object,
  691. p:* = a[Math.round((left + right) * .5)][property];
  692. if (ascending)
  693. {
  694. while (i <= j)
  695. {
  696. while (a[i][property] < p) i ++;
  697. while (a[j][property] > p) j --;
  698. if (i <= j)
  699. {
  700. t = a[i];
  701. a[i ++] = a[j];
  702. a[j --] = t;
  703. }
  704. }
  705. }
  706. else
  707. {
  708. while (i <= j)
  709. {
  710. while (a[i][property] > p) i ++;
  711. while (a[j][property] < p) j --;
  712. if (i <= j)
  713. {
  714. t = a[i];
  715. a[i ++] = a[j];
  716. a[j --] = t;
  717. }
  718. }
  719. }
  720. if (left < j) quicksortBy(a, left, j, ascending, property);
  721. if (i < right) quicksortBy(a, i, right, ascending, property);
  722. }
  723. // World information.
  724. /** @private */ internal static var _world:World;
  725. /** @private */ internal static var _goto:World;
  726. // Console information.
  727. /** @private */ internal static var _console:Console;
  728. // Time information.
  729. /** @private */ internal static var _time:uint;
  730. /** @private */ public static var _updateTime:uint;
  731. /** @private */ public static var _renderTime:uint;
  732. /** @private */ public static var _gameTime:uint;
  733. /** @private */ public static var _flashTime:uint;
  734. // Bitmap storage.
  735. /** @private */ private static var _bitmap:Object = { };
  736. // Pseudo-random number generation (the seed is set in Engine's contructor).
  737. /** @private */ private static var _seed:uint = 0;
  738. /** @private */ private static var _getSeed:uint;
  739. // Volume control.
  740. /** @private */ private static var _volume:Number = 1;
  741. /** @private */ private static var _pan:Number = 0;
  742. /** @private */ private static var _soundTransform:SoundTransform = new SoundTransform;
  743. // Used for rad-to-deg and deg-to-rad conversion.
  744. /** @private */ public static const DEG:Number = -180 / Math.PI;
  745. /** @private */ public static const RAD:Number = Math.PI / -180;
  746. // Global Flash objects.
  747. /** @private */ public static var stage:Stage;
  748. /** @private */ public static var engine:Engine;
  749. // Global objects used for rendering, collision, etc.
  750. /** @private */ public static var point:Point = new Point;
  751. /** @private */ public static var point2:Point = new Point;
  752. /** @private */ public static var zero:Point = new Point;
  753. /** @private */ public static var rect:Rectangle = new Rectangle;
  754. /** @private */ public static var matrix:Matrix = new Matrix;
  755. /** @private */ public static var sprite:Sprite = new Sprite;
  756. /** @private */ public static var entity:Entity;
  757. }
  758. }