Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.30 KB | None | 0 0
  1. package org.flixel
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.Sprite;
  6. import flash.display.StageAlign;
  7. import flash.display.StageScaleMode;
  8. import flash.events.*;
  9. import flash.geom.Point;
  10. import flash.geom.Rectangle;
  11. import flash.text.AntiAliasType;
  12. import flash.text.GridFitType;
  13. import flash.text.TextField;
  14. import flash.text.TextFormat;
  15. import flash.ui.Mouse;
  16. import flash.utils.getTimer;
  17.  
  18. import org.flixel.data.FlxConsole;
  19. import org.flixel.data.FlxPause;
  20.  
  21. /**
  22. * FlxGame is the heart of all flixel games, and contains a bunch of basic game loops and things.
  23. * It is a long and sloppy file that you shouldn't have to worry about too much!
  24. * It is basically only used to create your game object in the first place,
  25. * after that FlxG and FlxState have all the useful stuff you actually need.
  26. */
  27. public class FlxGame extends Sprite
  28. {
  29. [Embed(source="data/nokiafc22.ttf",fontFamily="system")] protected var junk:String;
  30. [Embed(source="data/beep.mp3")] protected var SndBeep:Class;
  31. [Embed(source="data/flixel.mp3")] protected var SndFlixel:Class;
  32.  
  33. /**
  34. * Essentially locks the framerate to 30 FPS minimum
  35. */
  36. internal const MAX_ELAPSED:Number = 0.0333;
  37.  
  38. /**
  39. * Sets 0, -, and + to control the global volume and P to pause.
  40. * @default true
  41. */
  42. public var useDefaultHotKeys:Boolean;
  43. /**
  44. * Displayed whenever the game is paused.
  45. * Override with your own <code>FlxLayer</code> for hot custom pause action!
  46. * Defaults to <code>data.FlxPause</code>.
  47. */
  48. public var pause:FlxGroup;
  49.  
  50. /**
  51. * Object for displaying dialog messages while the game is paused
  52. */
  53. public var dialog:FlxDialog;
  54.  
  55. //startup
  56. internal var _iState:Class;
  57. internal var _created:Boolean;
  58.  
  59. //basic display stuff
  60. internal var _state:FlxState;
  61. internal var _buffer:Sprite;
  62. internal var _bmpBack:Bitmap;
  63. internal var _bmpFront:Bitmap;
  64. internal var _r:Rectangle;
  65. internal var _flipped:Boolean;
  66. internal var _zoom:uint;
  67. internal var _gameXOffset:int;
  68. internal var _gameYOffset:int;
  69. internal var _frame:Class;
  70. internal var _zeroPoint:Point;
  71.  
  72. //basic update stuff
  73. internal var _elapsed:Number;
  74. internal var _total:uint;
  75. internal var _paused:Boolean;
  76. internal var _dialog:Boolean;
  77.  
  78.  
  79. //Pause screen, sound tray, support panel, dev console, and special effects objects
  80. internal var _soundTray:Sprite;
  81. internal var _soundTrayTimer:Number;
  82. internal var _soundTrayBars:Array;
  83. internal var _console:FlxConsole;
  84.  
  85. /**
  86. * Game object constructor - sets up the basic properties of your game.
  87. *
  88. * @param GameSizeX The width of your game in pixels (e.g. 320).
  89. * @param GameSizeY The height of your game in pixels (e.g. 240).
  90. * @param InitialState The class name of the state you want to create and switch to first (e.g. MenuState).
  91. * @param Zoom The level of zoom (e.g. 2 means all pixels are now rendered twice as big).
  92. */
  93. public function FlxGame(GameSizeX:uint,GameSizeY:uint,InitialState:Class,Zoom:uint=2)
  94. {
  95. flash.ui.Mouse.hide();
  96.  
  97. _zoom = Zoom;
  98. FlxState.bgColor = 0xff000000;
  99. FlxG.setGameData(this,GameSizeX,GameSizeY,Zoom);
  100. _elapsed = 0;
  101. _total = 0;
  102. pause = new FlxPause();
  103. _state = null;
  104. _iState = InitialState;
  105. _zeroPoint = new Point();
  106.  
  107. useDefaultHotKeys = true;
  108.  
  109. _frame = null;
  110. _gameXOffset = 0;
  111. _gameYOffset = 0;
  112.  
  113. _paused = false;
  114. _created = false;
  115. addEventListener(Event.ENTER_FRAME, onEnterFrame);
  116. }
  117.  
  118. /**
  119. * Adds a frame around your game for presentation purposes (see Canabalt, Gravity Hook).
  120. *
  121. * @param Frame If you want you can add a little graphical frame to the outside edges of your game.
  122. * @param ScreenOffsetX Width in pixels of left side of frame.
  123. * @param ScreenOffsetY Height in pixels of top of frame.
  124. *
  125. * @return This <code>FlxGame</code> instance.
  126. */
  127. protected function addFrame(Frame:Class,ScreenOffsetX:uint,ScreenOffsetY:uint):FlxGame
  128. {
  129. _frame = Frame;
  130. _gameXOffset = ScreenOffsetX;
  131. _gameYOffset = ScreenOffsetY;
  132. return this;
  133. }
  134.  
  135. /**
  136. * Makes the little volume tray slide out.
  137. *
  138. * @param Silent Whether or not it should beep.
  139. */
  140. public function showSoundTray(Silent:Boolean=false):void
  141. {
  142. if(!Silent)
  143. FlxG.play(SndBeep);
  144. _soundTrayTimer = 1;
  145. _soundTray.y = _gameYOffset*_zoom;
  146. _soundTray.visible = true;
  147. var gv:uint = Math.round(FlxG.volume*10);
  148. if(FlxG.mute)
  149. gv = 0;
  150. for (var i:uint = 0; i < _soundTrayBars.length; i++)
  151. {
  152. if(i < gv) _soundTrayBars[i].alpha = 1;
  153. else _soundTrayBars[i].alpha = 0.5;
  154. }
  155. }
  156.  
  157. /**
  158. * Switch from one <code>FlxState</code> to another.
  159. * Usually called from <code>FlxG</code>.
  160. *
  161. * @param State The class name of the state you want (e.g. PlayState)
  162. */
  163. public function switchState(State:FlxState):void
  164. {
  165. //Basic reset stuff
  166. FlxG.panel.hide();
  167. FlxG.unfollow();
  168. FlxG.resetInput();
  169. FlxG.destroySounds();
  170. FlxG.flash.stop();
  171. FlxG.fade.stop();
  172. FlxG.quake.stop();
  173. _buffer.x = 0;
  174. _buffer.y = 0;
  175.  
  176. //Swap the new state for the old one and dispose of it
  177. _buffer.addChild(State);
  178. if(_state != null)
  179. {
  180. _state.destroy(); //important that it is destroyed while still in the display list
  181. _buffer.swapChildren(State,_state);
  182. _buffer.removeChild(_state);
  183. }
  184. _state = State;
  185.  
  186. //Finally, create the new state
  187. _state.create();
  188. }
  189.  
  190. /**
  191. * Internal event handler for input and focus.
  192. */
  193. protected function onKeyUp(event:KeyboardEvent):void
  194. {
  195. if((event.keyCode == 192) || (event.keyCode == 220)) //FOR ZE GERMANZ
  196. {
  197. _console.toggle();
  198. return;
  199. }
  200. if(useDefaultHotKeys)
  201. {
  202. var c:int = event.keyCode;
  203. var code:String = String.fromCharCode(event.charCode);
  204. switch(c)
  205. {
  206. case 48:
  207. case 96:
  208. FlxG.mute = !FlxG.mute;
  209. showSoundTray();
  210. return;
  211. case 109:
  212. case 189:
  213. FlxG.mute = false;
  214. FlxG.volume = FlxG.volume - 0.1;
  215. showSoundTray();
  216. return;
  217. case 107:
  218. case 187:
  219. FlxG.mute = false;
  220. FlxG.volume = FlxG.volume + 0.1;
  221. showSoundTray();
  222. return;
  223. case 80:
  224. FlxG.pause = !FlxG.pause;
  225. default: break;
  226. }
  227. }
  228. FlxG.keys.handleKeyUp(event);
  229. }
  230.  
  231. /**
  232. * Internal event handler for input and focus.
  233. */
  234. protected function onFocus(event:Event=null):void
  235. {
  236. if(FlxG.pause)
  237. FlxG.pause = false;
  238. }
  239.  
  240. /**
  241. * Internal event handler for input and focus.
  242. */
  243. protected function onFocusLost(event:Event=null):void
  244. {
  245. FlxG.pause = true;
  246. }
  247.  
  248. /**
  249. * Internal function to help with basic pause game functionality.
  250. */
  251. internal function unpauseGame():void
  252. {
  253. if(!FlxG.panel.visible) flash.ui.Mouse.hide();
  254. FlxG.resetInput();
  255. _paused = false;
  256. stage.frameRate = 60;
  257. }
  258.  
  259. /**
  260. * Internal function to help with basic pause game functionality.
  261. */
  262. internal function pauseGame():void
  263. {
  264. if((x != 0) || (y != 0))
  265. {
  266. x = 0;
  267. y = 0;
  268. }
  269. if(!_flipped)
  270. _bmpBack.bitmapData.copyPixels(_bmpFront.bitmapData,_r,_zeroPoint);
  271. else
  272. _bmpFront.bitmapData.copyPixels(_bmpBack.bitmapData,_r,_zeroPoint);
  273. flash.ui.Mouse.show();
  274. _paused = true;
  275. stage.frameRate = 10;
  276. }
  277. /**
  278. * Internal function to help with dialog showing and pause game functionality.
  279. */
  280. internal function showDialog(Message:Array):void
  281. {
  282. if((x != 0) || (y != 0))
  283. {
  284. x = 0;
  285. y = 0;
  286. }
  287. if(!_flipped)
  288. _bmpBack.bitmapData.copyPixels(_bmpFront.bitmapData,_r,new Point(0,0));
  289. else
  290. _bmpFront.bitmapData.copyPixels(_bmpBack.bitmapData, _r, new Point(0, 0));
  291.  
  292. dialog.message = Message;
  293. _dialog = true;
  294. }
  295. /**
  296. * This is the main game loop, but only once creation and logo playback is finished.
  297. */
  298. protected function onEnterFrame(event:Event):void
  299. {
  300. var i:uint;
  301. var soundPrefs:FlxSave;
  302.  
  303. //Frame timing
  304. var t:uint = getTimer();
  305. _elapsed = (t-_total)/1000;
  306. if(_created)
  307. _console.lastElapsed = _elapsed;
  308. _total = t;
  309. FlxG.elapsed = _elapsed;
  310. if(FlxG.elapsed > MAX_ELAPSED)
  311. FlxG.elapsed = MAX_ELAPSED;
  312. FlxG.elapsed *= FlxG.timeScale;
  313.  
  314. if(_soundTray != null)
  315. {
  316. if(_soundTrayTimer > 0)
  317. _soundTrayTimer -= _elapsed;
  318. else if(_soundTray.y > -_soundTray.height)
  319. {
  320. _soundTray.y -= _elapsed*FlxG.height*2;
  321. if(_soundTray.y <= -_soundTray.height)
  322. {
  323. _soundTray.visible = false;
  324.  
  325. //Save sound preferences
  326. soundPrefs = new FlxSave();
  327. if(soundPrefs.bind("flixel"))
  328. {
  329. if(soundPrefs.data.sound == null)
  330. soundPrefs.data.sound = new Object;
  331. soundPrefs.data.mute = FlxG.mute;
  332. soundPrefs.data.volume = FlxG.volume;
  333. soundPrefs.forceSave();
  334. }
  335. }
  336. }
  337. }
  338.  
  339. if(_created)
  340. {
  341. //Animate flixel HUD elements
  342. FlxG.panel.update();
  343. _console.update();
  344.  
  345. //State updating
  346. FlxG.updateInput();
  347. FlxG.updateSounds();
  348. if(_paused)
  349. {
  350. pause.update();
  351. if(_flipped)
  352. FlxG.buffer.copyPixels(_bmpFront.bitmapData,_r,_zeroPoint);
  353. else
  354. FlxG.buffer.copyPixels(_bmpBack.bitmapData,_r,_zeroPoint);
  355. pause.render();
  356. }
  357. else
  358. {
  359. //Clear video buffer
  360. if(_flipped)
  361. FlxG.buffer = _bmpFront.bitmapData;
  362. else
  363. FlxG.buffer = _bmpBack.bitmapData;
  364. FlxState.screen.unsafeBind(FlxG.buffer);
  365. _state.preProcess();
  366.  
  367. //Update the camera and game state
  368. FlxG.doFollow();
  369. _state.update();
  370.  
  371. //Update the various special effects
  372. if(FlxG.flash.exists)
  373. FlxG.flash.update();
  374. if(FlxG.fade.exists)
  375. FlxG.fade.update();
  376. FlxG.quake.update();
  377. _buffer.x = FlxG.quake.x;
  378. _buffer.y = FlxG.quake.y;
  379.  
  380. //Render game content, special fx, and overlays
  381. _state.render();
  382. if(FlxG.flash.exists)
  383. FlxG.flash.render();
  384. if(FlxG.fade.exists)
  385. FlxG.fade.render();
  386. if(FlxG.panel.visible)
  387. FlxG.panel.render();
  388. if(FlxG.mouse.cursor != null)
  389. {
  390. if(FlxG.mouse.cursor.active)
  391. FlxG.mouse.cursor.update();
  392. if(FlxG.mouse.cursor.visible)
  393. FlxG.mouse.cursor.render();
  394. }
  395.  
  396. //Post-processing hook
  397. _state.postProcess();
  398.  
  399. //Swap video buffers
  400. _bmpBack.visible = !(_bmpFront.visible = _flipped);
  401. _flipped = !_flipped;
  402. }
  403. }
  404. else if(root != null)
  405. {
  406. //Set up the view window and double buffering
  407. stage.scaleMode = StageScaleMode.NO_SCALE;
  408. stage.align = StageAlign.TOP_LEFT;
  409. stage.frameRate = 60;
  410. _buffer = new Sprite();
  411. _buffer.scaleX = _zoom;
  412. _buffer.scaleY = _zoom;
  413. addChild(_buffer);
  414. _bmpBack = new Bitmap(new BitmapData(FlxG.width,FlxG.height,true,FlxState.bgColor));
  415. _bmpBack.x = _gameXOffset;
  416. _bmpBack.y = _gameYOffset;
  417. _buffer.addChild(_bmpBack);
  418. _bmpFront = new Bitmap(new BitmapData(_bmpBack.width,_bmpBack.height,true,FlxState.bgColor));
  419. _bmpFront.x = _bmpBack.x;
  420. _bmpFront.y = _bmpBack.y;
  421. _buffer.addChild(_bmpFront);
  422. _flipped = false;
  423. _r = new Rectangle(0,0,_bmpFront.width,_bmpFront.height);
  424.  
  425. //Initialize game console
  426. _console = new FlxConsole(_gameXOffset,_gameYOffset,_zoom);
  427. addChild(_console);
  428. var vstring:String = FlxG.LIBRARY_NAME+" v"+FlxG.LIBRARY_MAJOR_VERSION+"."+FlxG.LIBRARY_MINOR_VERSION;
  429. if(FlxG.debug)
  430. vstring += " [debug]";
  431. else
  432. vstring += " [release]";
  433. var underline:String = "";
  434. for(i = 0; i < vstring.length+32; i++)
  435. underline += "-";
  436. FlxG.log(vstring);
  437. FlxG.log(underline);
  438.  
  439. //Add basic input even listeners
  440. stage.addEventListener(KeyboardEvent.KEY_DOWN, FlxG.keys.handleKeyDown);
  441. stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
  442. stage.addEventListener(MouseEvent.MOUSE_DOWN, FlxG.mouse.handleMouseDown);
  443. stage.addEventListener(MouseEvent.MOUSE_UP, FlxG.mouse.handleMouseUp);
  444. stage.addEventListener(MouseEvent.MOUSE_OUT, FlxG.mouse.handleMouseOut);
  445. stage.addEventListener(MouseEvent.MOUSE_OVER, FlxG.mouse.handleMouseOver);
  446.  
  447. //Initialize the pause screen
  448. stage.addEventListener(Event.DEACTIVATE, onFocusLost);
  449. stage.addEventListener(Event.ACTIVATE, onFocus);
  450.  
  451. //Sound Tray popup
  452. _soundTray = new Sprite();
  453. _soundTray.visible = false;
  454. _soundTray.scaleX = 2;
  455. _soundTray.scaleY = 2;
  456. var tmp:Bitmap = new Bitmap(new BitmapData(80,30,true,0x7F000000));
  457. _soundTray.x = (_gameXOffset+FlxG.width/2)*_zoom-(tmp.width/2)*_soundTray.scaleX;
  458. _soundTray.addChild(tmp);
  459.  
  460. var text:TextField = new TextField();
  461. text.width = tmp.width;
  462. text.height = tmp.height;
  463. text.multiline = true;
  464. text.wordWrap = true;
  465. text.selectable = false;
  466. text.embedFonts = true;
  467. text.antiAliasType = AntiAliasType.NORMAL;
  468. text.gridFitType = GridFitType.PIXEL;
  469. text.defaultTextFormat = new TextFormat("system",8,0xffffff,null,null,null,null,null,"center");;
  470. _soundTray.addChild(text);
  471. text.text = "VOLUME";
  472. text.y = 16;
  473.  
  474. var bx:uint = 10;
  475. var by:uint = 14;
  476. _soundTrayBars = new Array();
  477. for(i = 0; i < 10; i++)
  478. {
  479. tmp = new Bitmap(new BitmapData(4,i+1,false,0xffffff));
  480. tmp.x = bx;
  481. tmp.y = by;
  482. _soundTrayBars.push(_soundTray.addChild(tmp));
  483. bx += 6;
  484. by--;
  485. }
  486. addChild(_soundTray);
  487.  
  488. //Initialize the decorative frame (optional)
  489. if(_frame != null)
  490. {
  491. var bmp:Bitmap = new _frame;
  492. bmp.scaleX = _zoom;
  493. bmp.scaleY = _zoom;
  494. addChild(bmp);
  495. }
  496.  
  497. //Check for saved sound preference data
  498. soundPrefs = new FlxSave();
  499. if(soundPrefs.bind("flixel") && (soundPrefs.data.sound != null))
  500. {
  501. if(soundPrefs.data.volume != null)
  502. FlxG.volume = soundPrefs.data.volume;
  503. if(soundPrefs.data.mute != null)
  504. FlxG.mute = soundPrefs.data.mute;
  505. showSoundTray(true);
  506. }
  507.  
  508. //All set!
  509. _created = true;
  510. switchState(new _iState());
  511. }
  512. }
  513. }
  514. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement