PageRenderTime 110ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/net/flashpunk/FP.as

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