Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.57 KB | None | 0 0
  1. package org.flixel
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.Stage;
  6. import flash.geom.Matrix;
  7. import flash.geom.Point;
  8.  
  9. import org.flixel.data.*;
  10.  
  11. /**
  12. * This is a global helper class full of useful functions for audio,
  13. * input, basic info, and the camera system among other things.
  14. */
  15. public class FlxG
  16. {
  17. /**
  18. * If you build and maintain your own version of flixel,
  19. * you can give it your own name here. Appears in the console.
  20. */
  21. static public var LIBRARY_NAME:String = "flixel";
  22. /**
  23. * Assign a major version to your library.
  24. * Appears before the decimal in the console.
  25. */
  26. static public var LIBRARY_MAJOR_VERSION:uint = 2;
  27. /**
  28. * Assign a minor version to your library.
  29. * Appears after the decimal in the console.
  30. */
  31. static public var LIBRARY_MINOR_VERSION:uint = 23;
  32.  
  33. /**
  34. * Internal tracker for game object (so we can pause & unpause)
  35. */
  36. static protected var _game:FlxGame;
  37. /**
  38. * Internal tracker for game pause state.
  39. */
  40. static protected var _pause:Boolean;
  41. /**
  42. * Whether you are running in Debug or Release mode.
  43. * Set automatically by <code>FlxFactory</code> during startup.
  44. */
  45. static public var debug:Boolean;
  46.  
  47. /**
  48. * Represents the amount of time in seconds that passed since last frame.
  49. */
  50. static public var elapsed:Number;
  51. /**
  52. * How fast or slow time should pass in the game; default is 1.0.
  53. */
  54. static public var timeScale:Number;
  55. /**
  56. * The width of the screen in game pixels.
  57. */
  58. static public var width:uint;
  59. /**
  60. * The height of the screen in game pixels.
  61. */
  62. static public var height:uint;
  63. /**
  64. * <code>FlxG.levels</code> and <code>FlxG.scores</code> are generic
  65. * global variables that can be used for various cross-state stuff.
  66. */
  67. static public var levels:Array;
  68. static public var level:int;
  69. static public var scores:Array;
  70. static public var score:int;
  71. /**
  72. * <code>FlxG.saves</code> is a generic bucket for storing
  73. * FlxSaves so you can access them whenever you want.
  74. */
  75. static public var saves:Array;
  76. static public var save:int;
  77.  
  78. /**
  79. * A reference to a <code>FlxMouse</code> object. Important for input!
  80. */
  81. static public var mouse:FlxMouse;
  82. /**
  83. * A reference to a <code>FlxKeyboard</code> object. Important for input!
  84. */
  85. static public var keys:FlxKeyboard;
  86.  
  87. /**
  88. * A handy container for a background music object.
  89. */
  90. static public var music:FlxSound;
  91. /**
  92. * A list of all the sounds being played in the game.
  93. */
  94. static public var sounds:Array;
  95. /**
  96. * Internal flag for whether or not the game is muted.
  97. */
  98. static protected var _mute:Boolean;
  99. /**
  100. * Internal volume level, used for global sound control.
  101. */
  102. static protected var _volume:Number;
  103.  
  104. /**
  105. * Tells the camera to follow this <code>FlxCore</code> object around.
  106. */
  107. static public var followTarget:FlxObject;
  108. /**
  109. * Used to force the camera to look ahead of the <code>followTarget</code>.
  110. */
  111. static public var followLead:Point;
  112. /**
  113. * Used to smoothly track the camera as it follows.
  114. */
  115. static public var followLerp:Number;
  116. /**
  117. * Stores the top and left edges of the camera area.
  118. */
  119. static public var followMin:Point;
  120. /**
  121. * Stores the bottom and right edges of the camera area.
  122. */
  123. static public var followMax:Point;
  124. /**
  125. * Internal, used to assist camera and scrolling.
  126. */
  127. static protected var _scrollTarget:Point;
  128.  
  129. /**
  130. * Stores the basic parallax scrolling values.
  131. */
  132. static public var scroll:Point;
  133. /**
  134. * Reference to the active graphics buffer.
  135. * Can also be referenced via <code>FlxState.screen</code>.
  136. */
  137. static public var buffer:BitmapData;
  138. /**
  139. * Internal storage system to prevent graphics from being used repeatedly in memory.
  140. */
  141. static protected var _cache:Object;
  142.  
  143. /**
  144. * Access to the Kongregate high scores and achievements API.
  145. */
  146. static public var kong:FlxKong;
  147.  
  148. /**
  149. * The support panel (twitter, reddit, stumbleupon, paypal, etc) visor thing
  150. */
  151. static public var panel:FlxPanel;
  152. /**
  153. * A special effect that shakes the screen. Usage: FlxG.quake.start();
  154. */
  155. static public var quake:FlxQuake;
  156. /**
  157. * A special effect that flashes a color on the screen. Usage: FlxG.flash.start();
  158. */
  159. static public var flash:FlxFlash;
  160. /**
  161. * A special effect that fades a color onto the screen. Usage: FlxG.fade.start();
  162. */
  163. static public var fade:FlxFade;
  164.  
  165. /**
  166. * Log data to the developer console.
  167. *
  168. * @param Data Anything you want to log to the console.
  169. */
  170. static public function log(Data:Object):void
  171. {
  172. if((_game != null) && (_game._console != null))
  173. _game._console.log((Data == null)?"ERROR: null object":Data.toString());
  174. }
  175.  
  176. /**
  177. * Set <code>pause</code> to true to pause the game, all sounds, and display the pause popup.
  178. */
  179. static public function get pause():Boolean
  180. {
  181. return _pause;
  182. }
  183.  
  184. /**
  185. * @private
  186. */
  187. static public function set pause(Pause:Boolean):void
  188. {
  189. var op:Boolean = _pause;
  190. _pause = Pause;
  191. if(_pause != op)
  192. {
  193. if(_pause)
  194. {
  195. _game.pauseGame();
  196. pauseSounds();
  197. }
  198. else
  199. {
  200. _game.unpauseGame();
  201. playSounds();
  202. }
  203. }
  204. }
  205.  
  206. /**
  207. *
  208. */
  209. static public function showDialog(Message:Array):void
  210. {
  211. _game.showDialog(Message);
  212. }
  213.  
  214.  
  215. /**
  216. * Reset the input helper objects (useful when changing screens or states)
  217. */
  218. static public function resetInput():void
  219. {
  220. keys.reset();
  221. mouse.reset();
  222. }
  223.  
  224. /**
  225. * Set up and play a looping background soundtrack.
  226. *
  227. * @param Music The sound file you want to loop in the background.
  228. * @param Volume How loud the sound should be, from 0 to 1.
  229. */
  230. static public function playMusic(Music:Class,Volume:Number=1.0):void
  231. {
  232. if(music == null)
  233. music = new FlxSound();
  234. else if(music.active)
  235. music.stop();
  236. music.loadEmbedded(Music,true);
  237. music.volume = Volume;
  238. music.survive = true;
  239. music.play();
  240. }
  241.  
  242. /**
  243. * Creates a new sound object from an embedded <code>Class</code> object.
  244. *
  245. * @param EmbeddedSound The sound you want to play.
  246. * @param Volume How loud to play it (0 to 1).
  247. * @param Looped Whether or not to loop this sound.
  248. *
  249. * @return A <code>FlxSound</code> object.
  250. */
  251. static public function play(EmbeddedSound:Class,Volume:Number=1.0,Looped:Boolean=false):FlxSound
  252. {
  253. var sl:uint = sounds.length;
  254. for(var i:uint = 0; i < sl; i++)
  255. if(!(sounds[i] as FlxSound).active)
  256. break;
  257. if(sounds[i] == null)
  258. sounds[i] = new FlxSound();
  259. var s:FlxSound = sounds[i];
  260. s.loadEmbedded(EmbeddedSound,Looped);
  261. s.volume = Volume;
  262. s.play();
  263. return s;
  264. }
  265.  
  266. /**
  267. * Creates a new sound object from a URL.
  268. *
  269. * @param EmbeddedSound The sound you want to play.
  270. * @param Volume How loud to play it (0 to 1).
  271. * @param Looped Whether or not to loop this sound.
  272. *
  273. * @return A FlxSound object.
  274. */
  275. static public function stream(URL:String,Volume:Number=1.0,Looped:Boolean=false):FlxSound
  276. {
  277. var sl:uint = sounds.length;
  278. for(var i:uint = 0; i < sl; i++)
  279. if(!(sounds[i] as FlxSound).active)
  280. break;
  281. if(sounds[i] == null)
  282. sounds[i] = new FlxSound();
  283. var s:FlxSound = sounds[i];
  284. s.loadStream(URL,Looped);
  285. s.volume = Volume;
  286. s.play();
  287. return s;
  288. }
  289.  
  290. /**
  291. * Set <code>mute</code> to true to turn off the sound.
  292. *
  293. * @default false
  294. */
  295. static public function get mute():Boolean
  296. {
  297. return _mute;
  298. }
  299.  
  300. /**
  301. * @private
  302. */
  303. static public function set mute(Mute:Boolean):void
  304. {
  305. _mute = Mute;
  306. changeSounds();
  307. }
  308.  
  309. /**
  310. * Get a number that represents the mute state that we can multiply into a sound transform.
  311. *
  312. * @return An unsigned integer - 0 if muted, 1 if not muted.
  313. */
  314. static public function getMuteValue():uint
  315. {
  316. if(_mute)
  317. return 0;
  318. else
  319. return 1;
  320. }
  321.  
  322. /**
  323. * Set <code>volume</code> to a number between 0 and 1 to change the global volume.
  324. *
  325. * @default 0.5
  326. */
  327. static public function get volume():Number { return _volume; }
  328.  
  329. /**
  330. * @private
  331. */
  332. static public function set volume(Volume:Number):void
  333. {
  334. _volume = Volume;
  335. if(_volume < 0)
  336. _volume = 0;
  337. else if(_volume > 1)
  338. _volume = 1;
  339. changeSounds();
  340. }
  341.  
  342. /**
  343. * Called by FlxGame on state changes to stop and destroy sounds.
  344. *
  345. * @param ForceDestroy Kill sounds even if they're flagged <code>survive</code>.
  346. */
  347. static internal function destroySounds(ForceDestroy:Boolean=false):void
  348. {
  349. if(sounds == null)
  350. return;
  351. if((music != null) && (ForceDestroy || !music.survive))
  352. music.destroy();
  353. var s:FlxSound;
  354. var sl:uint = sounds.length;
  355. for(var i:uint = 0; i < sl; i++)
  356. {
  357. s = sounds[i] as FlxSound;
  358. if((s != null) && (ForceDestroy || !s.survive))
  359. s.destroy();
  360. }
  361. }
  362.  
  363. /**
  364. * An internal function that adjust the volume levels and the music channel after a change.
  365. */
  366. static protected function changeSounds():void
  367. {
  368. if((music != null) && music.active)
  369. music.updateTransform();
  370. var s:FlxSound;
  371. var sl:uint = sounds.length;
  372. for(var i:uint = 0; i < sl; i++)
  373. {
  374. s = sounds[i] as FlxSound;
  375. if((s != null) && s.active)
  376. s.updateTransform();
  377. }
  378. }
  379.  
  380. /**
  381. * Called by the game loop to make sure the sounds get updated each frame.
  382. */
  383. static internal function updateSounds():void
  384. {
  385. if((music != null) && music.active)
  386. music.update();
  387. var s:FlxSound;
  388. var sl:uint = sounds.length;
  389. for(var i:uint = 0; i < sl; i++)
  390. {
  391. s = sounds[i] as FlxSound;
  392. if((s != null) && s.active)
  393. s.update();
  394. }
  395. }
  396.  
  397. /**
  398. * Internal helper, pauses all game sounds.
  399. */
  400. static protected function pauseSounds():void
  401. {
  402. if((music != null) && music.active)
  403. music.pause();
  404. var s:FlxSound;
  405. var sl:uint = sounds.length;
  406. for(var i:uint = 0; i < sl; i++)
  407. {
  408. s = sounds[i] as FlxSound;
  409. if((s != null) && s.active)
  410. s.pause();
  411. }
  412. }
  413.  
  414. /**
  415. * Internal helper, pauses all game sounds.
  416. */
  417. static protected function playSounds():void
  418. {
  419. if((music != null) && music.active)
  420. music.play();
  421. var s:FlxSound;
  422. var sl:uint = sounds.length;
  423. for(var i:uint = 0; i < sl; i++)
  424. {
  425. s = sounds[i] as FlxSound;
  426. if((s != null) && s.active)
  427. s.play();
  428. }
  429. }
  430.  
  431. /**
  432. * Check the local bitmap cache to see if a bitmap with this key has been loaded already.
  433. *
  434. * @param Key The string key identifying the bitmap.
  435. *
  436. * @return Whether or not this file can be found in the cache.
  437. */
  438. static public function checkBitmapCache(Key:String):Boolean
  439. {
  440. return (_cache[Key] != undefined) && (_cache[Key] != null);
  441. }
  442.  
  443. /**
  444. * Generates a new <code>BitmapData</code> object (a colored square) and caches it.
  445. *
  446. * @param Width How wide the square should be.
  447. * @param Height How high the square should be.
  448. * @param Color What color the square should be (0xAARRGGBB)
  449. *
  450. * @return The <code>BitmapData</code> we just created.
  451. */
  452. static public function createBitmap(Width:uint, Height:uint, Color:uint, Unique:Boolean=false, Key:String=null):BitmapData
  453. {
  454. var key:String = Key;
  455. if(key == null)
  456. {
  457. key = Width+"x"+Height+":"+Color;
  458. if(Unique && (_cache[key] != undefined) && (_cache[key] != null))
  459. {
  460. //Generate a unique key
  461. var inc:uint = 0;
  462. var ukey:String;
  463. do { ukey = key + inc++;
  464. } while((_cache[ukey] != undefined) && (_cache[ukey] != null));
  465. key = ukey;
  466. }
  467. }
  468. if(!checkBitmapCache(key))
  469. _cache[key] = new BitmapData(Width,Height,true,Color);
  470. return _cache[key];
  471. }
  472.  
  473. /**
  474. * Loads a bitmap from a file, caches it, and generates a horizontally flipped version if necessary.
  475. *
  476. * @param Graphic The image file that you want to load.
  477. * @param Reverse Whether to generate a flipped version.
  478. *
  479. * @return The <code>BitmapData</code> we just created.
  480. */
  481. static public function addBitmap(Graphic:Class, Reverse:Boolean=false, Unique:Boolean=false, Key:String=null):BitmapData
  482. {
  483. var needReverse:Boolean = false;
  484. var key:String = Key;
  485. if(key == null)
  486. {
  487. key = String(Graphic);
  488. if(Unique && (_cache[key] != undefined) && (_cache[key] != null))
  489. {
  490. //Generate a unique key
  491. var inc:uint = 0;
  492. var ukey:String;
  493. do { ukey = key + inc++;
  494. } while((_cache[ukey] != undefined) && (_cache[ukey] != null));
  495. key = ukey;
  496. }
  497. }
  498. //If there is no data for this key, generate the requested graphic
  499. if(!checkBitmapCache(key))
  500. {
  501. _cache[key] = (new Graphic).bitmapData;
  502. if(Reverse) needReverse = true;
  503. }
  504. var pixels:BitmapData = _cache[key];
  505. if(!needReverse && Reverse && (pixels.width == (new Graphic).bitmapData.width))
  506. needReverse = true;
  507. if(needReverse)
  508. {
  509. var newPixels:BitmapData = new BitmapData(pixels.width<<1,pixels.height,true,0x00000000);
  510. newPixels.draw(pixels);
  511. var mtx:Matrix = new Matrix();
  512. mtx.scale(-1,1);
  513. mtx.translate(newPixels.width,0);
  514. newPixels.draw(pixels,mtx);
  515. pixels = newPixels;
  516. }
  517. return pixels;
  518. }
  519.  
  520. /**
  521. * Tells the camera subsystem what <code>FlxCore</code> object to follow.
  522. *
  523. * @param Target The object to follow.
  524. * @param Lerp How much lag the camera should have (can help smooth out the camera movement).
  525. */
  526. static public function follow(Target:FlxObject, Lerp:Number=1):void
  527. {
  528. followTarget = Target;
  529. followLerp = Lerp;
  530. _scrollTarget.x = (width>>1)-followTarget.x-(followTarget.width>>1)+(followTarget as FlxSprite).offset.x;
  531. _scrollTarget.y = (height>>1)-followTarget.y-(followTarget.height>>1)+(followTarget as FlxSprite).offset.y;
  532. scroll.x = _scrollTarget.x;
  533. scroll.y = _scrollTarget.y;
  534. doFollow();
  535. }
  536.  
  537. /**
  538. * Specify an additional camera component - the velocity-based "lead",
  539. * or amount the camera should track in front of a sprite.
  540. *
  541. * @param LeadX Percentage of X velocity to add to the camera's motion.
  542. * @param LeadY Percentage of Y velocity to add to the camera's motion.
  543. */
  544. static public function followAdjust(LeadX:Number = 0, LeadY:Number = 0):void
  545. {
  546. followLead = new Point(LeadX,LeadY);
  547. }
  548.  
  549. /**
  550. * Specify the boundaries of the level or where the camera is allowed to move.
  551. *
  552. * @param MinX The smallest X value of your level (usually 0).
  553. * @param MinY The smallest Y value of your level (usually 0).
  554. * @param MaxX The largest X value of your level (usually the level width).
  555. * @param MaxY The largest Y value of your level (usually the level height).
  556. * @param UpdateWorldBounds Whether the quad tree's dimensions should be updated to match.
  557. */
  558. static public function followBounds(MinX:int=0, MinY:int=0, MaxX:int=0, MaxY:int=0, UpdateWorldBounds:Boolean=true):void
  559. {
  560. followMin = new Point(-MinX,-MinY);
  561. followMax = new Point(-MaxX+width,-MaxY+height);
  562. if(followMax.x > followMin.x)
  563. followMax.x = followMin.x;
  564. if(followMax.y > followMin.y)
  565. followMax.y = followMin.y;
  566. if(UpdateWorldBounds)
  567. FlxU.setWorldBounds(-MinX,-MinY,-MinX+MaxX,-MinY+MaxY);
  568. doFollow();
  569. }
  570.  
  571. /**
  572. * Retrieves the Flash stage object (required for event listeners)
  573. *
  574. * @return A Flash <code>MovieClip</code> object.
  575. */
  576. static public function get stage():Stage
  577. {
  578. if((_game._state != null) && (_game._state.parent != null))
  579. return _game._state.parent.stage;
  580. return null;
  581. }
  582.  
  583. /**
  584. * Safely access the current game state.
  585. */
  586. static public function get state():FlxState
  587. {
  588. return _game._state;
  589. }
  590.  
  591. /**
  592. * @private
  593. */
  594. static public function set state(State:FlxState):void
  595. {
  596. _game.switchState(State);
  597. }
  598.  
  599. /**
  600. * Called by <code>FlxGame</code> to set up <code>FlxG</code> during <code>FlxGame</code>'s constructor.
  601. */
  602. static internal function setGameData(Game:FlxGame,Width:uint,Height:uint,Zoom:uint):void
  603. {
  604. _game = Game;
  605. _cache = new Object();
  606. width = Width;
  607. height = Height;
  608. _mute = false;
  609. _volume = 0.5;
  610. sounds = new Array();
  611. mouse = new FlxMouse();
  612. keys = new FlxKeyboard();
  613. scroll = null;
  614. _scrollTarget = null;
  615. unfollow();
  616. FlxG.levels = new Array();
  617. FlxG.scores = new Array();
  618. level = 0;
  619. score = 0;
  620. FlxU.seed = NaN;
  621. kong = null;
  622. pause = false;
  623. timeScale = 1.0;
  624.  
  625. panel = new FlxPanel();
  626. quake = new FlxQuake(Zoom);
  627. flash = new FlxFlash();
  628. fade = new FlxFade();
  629.  
  630. FlxU.setWorldBounds();
  631. }
  632.  
  633. /**
  634. * Internal function that updates the camera and parallax scrolling.
  635. */
  636. static internal function doFollow():void
  637. {
  638. if(followTarget != null)
  639. {
  640. _scrollTarget.x = (width>>1)-followTarget.x-(followTarget.width>>1)+(followTarget as FlxSprite).offset.x;
  641. _scrollTarget.y = (height>>1)-followTarget.y-(followTarget.height>>1)+(followTarget as FlxSprite).offset.y;
  642. if((followLead != null) && (followTarget is FlxSprite))
  643. {
  644. _scrollTarget.x -= (followTarget as FlxSprite).velocity.x*followLead.x;
  645. _scrollTarget.y -= (followTarget as FlxSprite).velocity.y*followLead.y;
  646. }
  647. scroll.x += (_scrollTarget.x-scroll.x)*followLerp*FlxG.elapsed;
  648. scroll.y += (_scrollTarget.y-scroll.y)*followLerp*FlxG.elapsed;
  649.  
  650. if(followMin != null)
  651. {
  652. if(scroll.x > followMin.x)
  653. scroll.x = followMin.x;
  654. if(scroll.y > followMin.y)
  655. scroll.y = followMin.y;
  656. }
  657.  
  658. if(followMax != null)
  659. {
  660. if(scroll.x < followMax.x)
  661. scroll.x = followMax.x;
  662. if(scroll.y < followMax.y)
  663. scroll.y = followMax.y;
  664. }
  665. }
  666. }
  667.  
  668. /**
  669. * Stops and resets the camera.
  670. */
  671. static internal function unfollow():void
  672. {
  673. followTarget = null;
  674. followLead = null;
  675. followLerp = 1;
  676. followMin = null;
  677. followMax = null;
  678. if(scroll == null)
  679. scroll = new Point();
  680. else
  681. scroll.x = scroll.y = 0;
  682. if(_scrollTarget == null)
  683. _scrollTarget = new Point();
  684. else
  685. _scrollTarget.x = _scrollTarget.y = 0;
  686. }
  687.  
  688. /**
  689. * Calls update on the keyboard and mouse input tracking objects.
  690. */
  691. static internal function updateInput():void
  692. {
  693. keys.update();
  694. mouse.update(state.mouseX,state.mouseY,scroll.x,scroll.y);
  695. }
  696. }
  697. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement