PageRenderTime 57ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/net/flashpunk/FP.as

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