PageRenderTime 81ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/src/net/flashpunk/FP.as

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