PageRenderTime 32ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tunnelgame.as

https://github.com/bitc/tunnelgame
ActionScript | 316 lines | 270 code | 39 blank | 7 comment | 68 complexity | 700db8e5b815d7e0f6864dd9526284bd MD5 | raw file
  1. package
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.Sprite;
  5. import flash.text.TextField;
  6. import flash.display.Graphics;
  7. import flash.display.Shape;
  8. import flash.system.System;
  9. import flash.display.Stage;
  10. import flash.display.StageQuality;
  11. import flash.display.StageScaleMode;
  12. import flash.events.Event;
  13. import flash.ui.Keyboard;
  14. import flash.ui.Mouse;
  15. import flash.events.MouseEvent;
  16. import flash.events.KeyboardEvent;
  17. import flash.geom.Matrix;
  18. import flash.geom.Point;
  19. import Tunnel;
  20. [SWF(width="480", height="480", backgroundColor="#000000")]
  21. [Frame(factoryClass="Preloader")]
  22. public class tunnelgame extends Sprite
  23. {
  24. CONFIG::debugging
  25. {
  26. private var blackBars : Shape;
  27. }
  28. private var state : GameState;
  29. public function tunnelgame()
  30. {
  31. // the start() function acts as a constructor. Can't do any
  32. // initialization in the real constructor because the Preloader
  33. // requires that it instantiate this class and and inserts it
  34. // into the stage, so here in the constructor we don't yet have
  35. // access to the stage.
  36. }
  37. public function start() : void
  38. {
  39. stage.quality = StageQuality.LOW;
  40. stage.scaleMode = StageScaleMode.NO_SCALE;
  41. stage.showDefaultContextMenu = false;
  42. mouseEnabled = false;
  43. mouseChildren = false;
  44. stage.addEventListener(Event.ENTER_FRAME, tick);
  45. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
  46. stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
  47. controller = new Controller();
  48. CONFIG::debugging
  49. {
  50. var squaresOverlay : Shape = new Shape();
  51. squaresOverlay.graphics.lineStyle(1, 0x000000);
  52. squaresOverlay.graphics.drawRect(-1, -1, 481, 481);
  53. squaresOverlay.graphics.drawRect(-5, -5, 489, 489);
  54. addChild(squaresOverlay);
  55. blackBars = new Shape();
  56. blackBars.graphics.lineStyle();
  57. var barSize : int = 300;
  58. blackBars.graphics.beginFill(0x000000);
  59. blackBars.graphics.drawRect(-barSize, 0, barSize, 480);
  60. blackBars.graphics.drawRect(480, 0, barSize, 480);
  61. blackBars.graphics.drawRect(-barSize, -barSize, barSize*2+480, barSize);
  62. blackBars.graphics.drawRect(-barSize, 480, barSize*2+480, barSize);
  63. blackBars.graphics.endFill();
  64. addChild(blackBars);
  65. blackBars.visible = true;
  66. }
  67. registerMouseEvents();
  68. setStateMainMenu();
  69. }
  70. private function registerMouseEvents() : void
  71. {
  72. stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
  73. stage.addEventListener(Event.MOUSE_LEAVE, mouseLeave);
  74. stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
  75. stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  76. }
  77. private function unregisterMouseEvents() : void
  78. {
  79. stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
  80. stage.removeEventListener(Event.MOUSE_LEAVE, mouseLeave);
  81. stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
  82. stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
  83. }
  84. private function registerFocusEvents() : void
  85. {
  86. stage.addEventListener(Event.DEACTIVATE, deactivate);
  87. }
  88. private function unregisterFocusEvents() : void
  89. {
  90. stage.removeEventListener(Event.DEACTIVATE, deactivate);
  91. }
  92. private function setStateMainMenu() : void
  93. {
  94. state = GameState.MAIN_MENU;
  95. mainMenu = new MainMenu();
  96. addChild(mainMenu);
  97. }
  98. private function setStatePauseGame() : void
  99. {
  100. pauseMenu = new PauseMenu();
  101. addChild(pauseMenu);
  102. state = GameState.PAUSE_MENU;
  103. Mouse.show();
  104. registerMouseEvents();
  105. unregisterFocusEvents();
  106. }
  107. private function setStateUnpauseGame() : void
  108. {
  109. state = GameState.IN_GAME;
  110. Mouse.hide();
  111. unregisterMouseEvents();
  112. registerFocusEvents();
  113. }
  114. private var mainMenu : MainMenu;
  115. private var game : Game
  116. private var pauseMenu : PauseMenu;
  117. private function tick(event : Event) : void
  118. {
  119. if(state === GameState.IN_GAME)
  120. {
  121. game.tick(controller);
  122. }
  123. else if(state === GameState.MAIN_MENU)
  124. {
  125. var stayInMenu : Boolean = mainMenu.tick();
  126. if(!stayInMenu)
  127. {
  128. removeChild(mainMenu);
  129. mainMenu = null;
  130. controller.reset();
  131. game = new Game();
  132. game.addToDisplay(this);
  133. CONFIG::debugging
  134. {
  135. // Move the debugging overlays to the top
  136. addChild(removeChildAt(0));
  137. addChild(removeChildAt(0));
  138. }
  139. state = GameState.IN_GAME;
  140. Mouse.hide();
  141. unregisterMouseEvents();
  142. registerFocusEvents();
  143. }
  144. }
  145. else if(state === GameState.PAUSE_MENU)
  146. {
  147. var result : int = pauseMenu.tick();
  148. if(result == PauseMenu.CONTINUE)
  149. {
  150. // Don't do anything
  151. }
  152. else if(result == PauseMenu.RESUME)
  153. {
  154. removeChild(pauseMenu);
  155. pauseMenu = null;
  156. setStateUnpauseGame();
  157. }
  158. else if(result == PauseMenu.MAIN_MENU)
  159. {
  160. removeChild(pauseMenu);
  161. pauseMenu = null;
  162. game.removeFromDisplay(this);
  163. game = null;
  164. setStateMainMenu();
  165. }
  166. else
  167. {
  168. throw new Error("PauseMenu tick() returned an unknown result");
  169. }
  170. }
  171. else
  172. {
  173. throw new Error("tick() called and there is no valid GameState");
  174. }
  175. }
  176. private var controller : Controller;
  177. private function keyDown(event : KeyboardEvent) : void
  178. {
  179. if(event.keyCode == 90 || event.keyCode == 88) // Z || X
  180. controller.fire = true;
  181. else if(event.keyCode == Keyboard.UP)
  182. controller.up = true;
  183. else if(event.keyCode == Keyboard.DOWN)
  184. controller.down = true;
  185. else if(event.keyCode == Keyboard.LEFT)
  186. controller.left = true;
  187. else if(event.keyCode == Keyboard.RIGHT)
  188. controller.right = true;
  189. else if(event.keyCode == Keyboard.ESCAPE || event.keyCode == 80) // P
  190. {
  191. if(state === GameState.IN_GAME)
  192. {
  193. setStatePauseGame();
  194. }
  195. else if(state === GameState.PAUSE_MENU)
  196. {
  197. removeChild(pauseMenu);
  198. pauseMenu = null;
  199. setStateUnpauseGame();
  200. }
  201. }
  202. CONFIG::debugging
  203. {
  204. if(event.keyCode == 66) // B
  205. blackBars.visible = !blackBars.visible;
  206. }
  207. }
  208. private function keyUp(event : KeyboardEvent) : void
  209. {
  210. if(event.keyCode == 90 || event.keyCode == 88) // Z || X
  211. controller.fire = false;
  212. else if(event.keyCode == Keyboard.UP)
  213. controller.up = false;
  214. else if(event.keyCode == Keyboard.DOWN)
  215. controller.down = false;
  216. else if(event.keyCode == Keyboard.LEFT)
  217. controller.left = false;
  218. else if(event.keyCode == Keyboard.RIGHT)
  219. controller.right = false;
  220. }
  221. private function mouseMove(event : MouseEvent) : void
  222. {
  223. if(state === GameState.MAIN_MENU)
  224. {
  225. mainMenu.mouseMove(event.stageX, event.stageY);
  226. }
  227. else if(state === GameState.PAUSE_MENU)
  228. {
  229. pauseMenu.mouseMove(event.stageX, event.stageY);
  230. }
  231. }
  232. private function mouseLeave(event : Event) : void
  233. {
  234. if(state === GameState.MAIN_MENU)
  235. {
  236. mainMenu.mouseMove(-1, -1);
  237. }
  238. else if(state === GameState.PAUSE_MENU)
  239. {
  240. pauseMenu.mouseMove(-1, -1);
  241. }
  242. }
  243. private function mouseDown(event : MouseEvent) : void
  244. {
  245. if(state === GameState.MAIN_MENU)
  246. {
  247. mainMenu.mouseDown(event.stageX, event.stageY);
  248. }
  249. else if(state === GameState.PAUSE_MENU)
  250. {
  251. pauseMenu.mouseDown(event.stageX, event.stageY);
  252. }
  253. }
  254. private function mouseUp(event : MouseEvent) : void
  255. {
  256. if(state === GameState.MAIN_MENU)
  257. {
  258. mainMenu.mouseUp(event.stageX, event.stageY);
  259. }
  260. else if(state === GameState.PAUSE_MENU)
  261. {
  262. pauseMenu.mouseUp(event.stageX, event.stageY);
  263. }
  264. }
  265. private function deactivate(event : Event) : void
  266. {
  267. setStatePauseGame();
  268. controller.reset();
  269. }
  270. }
  271. }
  272. class GameState
  273. {
  274. public static const MAIN_MENU : GameState = new GameState();
  275. public static const IN_GAME : GameState = new GameState();
  276. public static const PAUSE_MENU : GameState = new GameState();
  277. }