PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Main.hx

http://deathbynescience.googlecode.com/
Haxe | 612 lines | 507 code | 62 blank | 43 comment | 58 complexity | 80e0011637eeabe36793021cbeb25745 MD5 | raw file
  1. import flash.display.BitmapData;
  2. import flash.display.Bitmap;
  3. import flash.geom.Rectangle;
  4. import flash.geom.Point;
  5. import flash.events.Event;
  6. import flash.events.KeyboardEvent;
  7. import flash.display.Sprite;
  8. import flash.ui.Keyboard;
  9. import flash.text.TextField;
  10. import flash.text.TextFieldAutoSize;
  11. import flash.text.TextFormat;
  12. import flash.text.TextFormatAlign;
  13. import flash.filters.GlowFilter;
  14. import haxe.FastList;
  15. import flash.events.MouseEvent;
  16. import flash.net.SharedObject;
  17. import flash.Lib;
  18. import haxe.Log;
  19. import flash.utils.Timer;
  20. import haxe.Timer;
  21. import flash.events.Event;
  22. import flash.display.MovieClip;
  23. import flash.media.SoundChannel;
  24. import Images;
  25. import Sounds;
  26. import MovieClips;
  27. import Blob;
  28. import Character;
  29. import Scene;
  30. import TextArea;
  31. import InstructionPage;
  32. import CreditsPage;
  33. import OptionsPage;
  34. import SoundPlayer;
  35. import PauseMenu;
  36. class Main extends Sprite{
  37. private var ground : Int;
  38. private var currentLevel : Int;
  39. private var Scenes : Array<Scene>;
  40. //Because the levels are so similiar i can do this. It's probably be to have a class for each level that inherents from the scence.
  41. private var Level1 : Scene;
  42. private var Level2 : Scene;
  43. private var Level3 : Scene;
  44. private var Level4 : Scene;
  45. private var Level5 : Scene;
  46. private var Level6 : Scene;
  47. private var Level7 : FinalScene;
  48. private var State : String;
  49. private var savedData:SharedObject;
  50. private var ContinueGameButton:Sprite;
  51. private var StartGameButton:Sprite;
  52. private var MainMenuButton:Sprite;
  53. private var InstructionsButton:Sprite;
  54. private var CreditsButton:Sprite;
  55. private var OptionsButton:Sprite;
  56. private var Title:TextField;
  57. private var label1:TextField;
  58. private var Zombie:Sprite;
  59. var myTimer:Timer;
  60. var music:BackgroundMusic;
  61. private var player:SoundPlayer;
  62. var preloader:PreloaderHost;
  63. private var Background : Bitmap;
  64. public var Instruction : InstructionPage;
  65. public var Credit: CreditsPage;
  66. public var OptionsMenu : OptionsPage;
  67. var pausePosition:Float;
  68. // music.play();
  69. var volume:Bool;
  70. var sound:Bool;
  71. var framerate:Int;
  72. var MyCharacter : Character;
  73. public function new(){
  74. super();
  75. Scenes = [];
  76. getLevelData();
  77. //inPostion,inMoveSpeed,inJumpSpeed,inAttackSpeed,inHealth:Int
  78. initLevels();
  79. //Add Main to Current Flash
  80. flash.Lib.current.addChild(this);
  81. //Create Menu (own Class?)
  82. Title = new TextField();
  83. Title.x = 50; Title.y = 20;
  84. Title.width = 540; Title.height = 420;
  85. Title.multiline = true;
  86. var myFormat:TextFormat = new TextFormat();
  87. myFormat.size = 25;
  88. myFormat.align = TextFormatAlign.CENTER;
  89. Title.defaultTextFormat = myFormat;
  90. Title.htmlText = "Death By Nescience";
  91. //It seems like there should be a better way to add a bitmap to a Sprite than filling the background with the bitmap. But this works.
  92. StartGameButton = new Sprite();
  93. StartGameButton.graphics.beginBitmapFill(new StartGame());
  94. StartGameButton.graphics.drawRect(0, 0, 250 ,48);
  95. StartGameButton.x = 195;
  96. StartGameButton.y = 75;
  97. StartGameButton.buttonMode = true;
  98. StartGameButton.addEventListener(MouseEvent.CLICK, onStartGameButtonClick);
  99. ContinueGameButton = new Sprite();
  100. ContinueGameButton.graphics.beginBitmapFill(new ContinueGame());
  101. ContinueGameButton.graphics.drawRect(0, 0, 250 ,48);
  102. ContinueGameButton.x = 195;
  103. ContinueGameButton.y = 145;
  104. ContinueGameButton.buttonMode = true;
  105. ContinueGameButton.addEventListener(MouseEvent.CLICK, onContinueGameButtonClick);
  106. InstructionsButton = new Sprite();
  107. InstructionsButton.graphics.beginBitmapFill(new Instructions());
  108. InstructionsButton.graphics.drawRect(0, 0, 250 ,48);
  109. InstructionsButton.x = 195;
  110. InstructionsButton.y = 215;
  111. InstructionsButton.buttonMode = true;
  112. InstructionsButton.addEventListener(MouseEvent.CLICK, onInstrutionsButtonClick);
  113. Instruction = new InstructionPage();
  114. Instruction.addEventListener("Back",onInstrutionsBack);
  115. CreditsButton = new Sprite();
  116. CreditsButton.graphics.beginBitmapFill(new Credits());
  117. CreditsButton.graphics.drawRect(0, 0, 250 ,48);
  118. CreditsButton.x = 195;
  119. CreditsButton.y = 285;
  120. CreditsButton.buttonMode = true;
  121. CreditsButton.addEventListener(MouseEvent.CLICK, onCreditsButtonClick);
  122. Credit = new CreditsPage();
  123. Credit.addEventListener("Back",onCreditsBack);
  124. OptionsButton = new Sprite();
  125. OptionsButton.graphics.beginBitmapFill(new Options());
  126. OptionsButton.graphics.drawRect(0, 0, 250 ,48);
  127. OptionsButton.x = 195;
  128. OptionsButton.y = 355;
  129. OptionsButton.buttonMode = true;
  130. OptionsButton.addEventListener(MouseEvent.CLICK, onOptionsButtonClick);
  131. OptionsMenu = new OptionsPage();
  132. OptionsMenu.addEventListener("Back",onOptionsMenuBack);
  133. OptionsMenu.addEventListener("ResetSavedData",onOptionsMenuResetSavedData);
  134. OptionsMenu.addEventListener("VolumeToggle",onOptionsMenuVolumeToggle);
  135. OptionsMenu.addEventListener("SoundEffectsToggle",onOptionsMenuSoundEffectsToggle);
  136. //this.addEventListener(Event.ENTER_FRAME, OnEnter);
  137. myTimer = new Timer(12);
  138. myTimer.addEventListener("timer", OnEnter);
  139. myTimer.start();
  140. label1 = new TextField();
  141. label1.x = 0; label1.y = 0;
  142. label1.width = 640; label1.height = 100;
  143. label1.autoSize = TextFieldAutoSize.CENTER;
  144. Zombie = new Sprite();
  145. Zombie.graphics.beginBitmapFill(new ZombieStand());
  146. Zombie.graphics.drawRect(0, 0, 46,52);
  147. Zombie.x = 300;
  148. Zombie.y = 75;
  149. MainMenuButton = new Sprite();
  150. MainMenuButton.graphics.beginBitmapFill(new MainMenu());
  151. MainMenuButton.graphics.drawRect(0, 0, 250 ,48);
  152. MainMenuButton.x = 200;
  153. MainMenuButton.y = 135;
  154. MainMenuButton.buttonMode = true;
  155. MainMenuButton.addEventListener(MouseEvent.CLICK, onMainMenuButtonClick);
  156. //The State Will determine whether to load a level, or show Main Menu, or whatever. Used in the OnEnterEvent.
  157. State = "InitLoad";
  158. //State = "LoadMainMenu";
  159. music = new BackgroundMusic();
  160. preloader = new PreloaderHost();
  161. Background = new Bitmap(new SunBackground());
  162. player = new SoundPlayer(music);
  163. volume = true;
  164. sound = true;
  165. //preloader.addEventListener(Event.ENTER_FRAME, OnMovieEnter);
  166. }
  167. function loadMenu()
  168. {
  169. addChild(Background);
  170. addChild(Title);
  171. addChild(StartGameButton);
  172. addChild(ContinueGameButton);
  173. addChild(InstructionsButton);
  174. addChild(CreditsButton);
  175. addChild(OptionsButton);
  176. }
  177. function removeMenu()
  178. {
  179. removeChild(Title);
  180. removeChild(StartGameButton);
  181. removeChild(ContinueGameButton);
  182. removeChild(Background);
  183. removeChild(InstructionsButton);
  184. removeChild(CreditsButton);
  185. removeChild(OptionsButton);
  186. }
  187. function loadTryAgainMenu(text:String)
  188. {
  189. addChild(label1);
  190. label1.htmlText = "<font size='15'>" + text + "</font>";
  191. addChild(MainMenuButton);
  192. addChild(Zombie);
  193. player.stop();
  194. }
  195. function removeTryAgainMenu()
  196. {
  197. removeChild(label1);
  198. removeChild(MainMenuButton);
  199. removeChild(Zombie);
  200. }
  201. function initLevels()
  202. {
  203. //Postion, MoveSpeed, JumpSpeed, AttackSpeed, Health, Bitmap)
  204. }
  205. function OnEnter(e:flash.events.Event)
  206. {
  207. if(State == "LoadMainMenu")
  208. {
  209. loadMenu();
  210. State = "MainMenu";
  211. }
  212. if(State == "MainMenu" )
  213. {
  214. return;
  215. }
  216. if(State == "TryAgain")
  217. {
  218. AnimateZombie();
  219. return;
  220. }
  221. if(State == "InitLoad")
  222. {
  223. addChild(preloader);
  224. preloader.play();
  225. State = "InitLoading";
  226. }
  227. if(State == "InitLoading")
  228. {
  229. //Always Playing. Possible error with swfmill
  230. //Skipping for now.
  231. if(!preloader.playing)
  232. {
  233. preloader.stop();
  234. removeChild(preloader);
  235. State = "LoadMainMenu";
  236. }
  237. }
  238. if(State == "Loading")
  239. {
  240. player.play(pausePosition);
  241. loadCurrentLevel();
  242. }
  243. if(State == "Playing")
  244. {
  245. if(Scenes[currentLevel-1] != null)
  246. {
  247. if(Scenes[currentLevel-1].running)
  248. {
  249. if(!volume)
  250. {
  251. pausePosition = player.getPosition();
  252. player.stop();
  253. }
  254. else
  255. {
  256. player.play(pausePosition);
  257. }
  258. return;
  259. }
  260. if(Scenes[currentLevel-1].victory == "Victory")
  261. {
  262. currentLevel++;
  263. savedData.data.Level = currentLevel;
  264. savedData.data.SoundPosition = player.getPosition();
  265. savedData.flush();
  266. }
  267. else if(Scenes[currentLevel-1].victory == "Loss")
  268. {
  269. loadTryAgainMenu("You Lose");
  270. State = "TryAgain";
  271. return;
  272. }
  273. else if(Scenes[currentLevel-1].victory == "GaveUp")
  274. {
  275. savedData.data.Level = currentLevel;
  276. savedData.data.SoundPosition = player.getPosition();
  277. savedData.flush();
  278. var BeatLevel:String;
  279. if(currentLevel <= 6)
  280. {
  281. BeatLevel = "level " + currentLevel;
  282. }
  283. else
  284. {
  285. BeatLevel = "The Game";
  286. }
  287. loadTryAgainMenu("You became a Zombie. Yay! and beat " + BeatLevel);
  288. State = "TryAgain";
  289. return;
  290. }
  291. }
  292. loadCurrentLevel();
  293. }
  294. if(State == "Won")
  295. {
  296. loadTryAgainMenu("You Won");
  297. State = "TryAgain";
  298. }
  299. // try
  300. // {
  301. // // var req:flash.net.URLRequest = new flash.net.URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3");
  302. // //
  303. // // Sound.load(req);
  304. // var music = new BackgroundMusic();
  305. // music.play();
  306. // }
  307. // catch (err:flash.Error)
  308. // {
  309. // trace(err.message);
  310. // }
  311. }
  312. public function AnimateZombie()
  313. {
  314. var randomNumber = Std.random(24);
  315. if(randomNumber == 1)
  316. Zombie.x = Zombie.x + 5;
  317. if(randomNumber == 2)
  318. Zombie.x = Zombie.x - 5;
  319. }
  320. //Loads Level based on Current Level
  321. public function loadCurrentLevel() : Bool
  322. {
  323. if(currentLevel <= 1)
  324. {
  325. var characters = new FastList<Character>();
  326. //Postion, MoveSpeed, JumpSpeed, AttackSpeed, Health, Bitmap)
  327. characters.add(new MainCharacter(new Point(300,374),25,250,10,50,25));
  328. characters.add(new EnemyCharacter(new Point(100,374),1,0,50,25,5));
  329. var Text = new FastList<TextArea>();
  330. Text.add(new TextArea("<font color='#009900' size='25'>Give Up!</font>",0x28A328));
  331. Level1 = new Scene(new SunBackground(),Text,characters);
  332. Scenes[currentLevel-1] = Level1;
  333. State = "Playing";
  334. }
  335. else if (currentLevel == 2)
  336. {
  337. var characters2 = new FastList<Character>();
  338. //Pos,Move,Jump:AttackSpeed,Health,AttackPower
  339. characters2.add(new MainCharacter(new Point(300,374),15,225,20,45,23));
  340. characters2.add(new EnemyCharacter(new Point(100,374),1,0,50,25,5));
  341. characters2.add(new EnemyCharacter(new Point(500,374),1,0,50,25,5));
  342. var Text = new FastList<TextArea>();
  343. Text.add(new TextArea("<font color='#009900' size='25'>Being a zombie ain't so bad.</font>",0x28A328));
  344. Text.add(new TextArea("<font color='#009900' size='25'>All you worry about is eating your next meal.</font>",0x28A328));
  345. Text.add(new TextArea("<font color='#009900' size='25'>Join us! Being a zombie is bliss!</font>",0x28A328));
  346. Level2 = new Scene(new SunBackground2(),Text,characters2);
  347. Scenes[currentLevel-1]=Level2;
  348. State = "Playing";
  349. }
  350. else if (currentLevel == 3)
  351. {
  352. var characters3= new FastList<Character>();
  353. characters3.add(new MainCharacter(new Point(100,374),10,200,30,40,20));
  354. characters3.add(new EnemyCharacter(new Point(500,374),1,0,50,25,5));
  355. var Text = new FastList<TextArea>();
  356. Text.add(new TextArea("<font color='#009900' size='25'>...</font>",0x28A328));
  357. Text.add(new TextArea("<font color='#009900' size='25'>I'm sorry, Please don't kill me.</font>",0x28A328));
  358. Level3 = new Scene(new SunBackground3(),Text,characters3);
  359. Scenes[currentLevel-1]=Level3;
  360. State = "Playing";
  361. }
  362. else if (currentLevel == 4)
  363. {
  364. currentLevel;
  365. var characters3= new FastList<Character>();
  366. characters3.add(new MainCharacter(new Point(100,374),7,160,40,35,16));
  367. characters3.add(new EnemyCharacter(new Point(500,374),1,0,50,25,5));
  368. var Text = new FastList<TextArea>();
  369. Text.add(new TextArea("<font color='#009900' size='25'>Isn't it tiring fighting zombie?</font>",0x28A328));
  370. Text.add(new TextArea("<font color='#009900' size='25'>Just Give up.</font>",0x28A328));
  371. Level4 = new Scene(new SunBackground4(),Text,characters3);
  372. Scenes[currentLevel-1]=Level4;
  373. State = "Playing";
  374. }
  375. else if (currentLevel == 5)
  376. {
  377. currentLevel;
  378. var characters= new FastList<Character>();
  379. characters.add(new MainCharacter(new Point(100,374),5,125,50,30,10));
  380. characters.add(new EnemyCharacter(new Point(500,374),1,0,50,25,5));
  381. var Text = new FastList<TextArea>();
  382. Text.add(new TextArea("<font color='#009900' size='25'>What's so great about being human?</font>",0x28A328));
  383. Level5 = new Scene(new SunBackground5(),Text,characters);
  384. Scenes[currentLevel-1]=Level5;
  385. State = "Playing";
  386. }
  387. else if (currentLevel == 6)
  388. {
  389. currentLevel;
  390. var characters= new FastList<Character>();
  391. characters.add(new MainCharacter(new Point(100,374),3,100,60,27,7));
  392. characters.add(new EnemyCharacter(new Point(500,374),1,0,50,25,5));
  393. var Text = new FastList<TextArea>();
  394. Text.add(new TextArea("<font color='#009900' size='25'>Do you something against zombies?</font>",0x28A328));
  395. Text.add(new TextArea("<font color='#009900' size='25'>Do you think you're better than me?</font>",0x28A328));
  396. Level6 = new Scene(new SunBackground6(),Text,characters);
  397. Scenes[currentLevel-1]=Level6;
  398. State = "Playing";
  399. }
  400. else if (currentLevel == 7)
  401. {
  402. currentLevel;
  403. var characters= new FastList<Character>();
  404. characters.add(new MainCharacter(new Point(100,374),1,0,50,25,5));
  405. characters.add(new BossCharacter(new Point(500,374),100,300,3,9001,100));//What?
  406. var Text = new FastList<TextArea>();
  407. Text.add(new TextArea("<font color='#000000' size='25'>Boss: So you've finally arrived.\nGive Up on this meaningless quest. Join me.</font>",0x28A328));
  408. Text.add(new TextArea("<font color='#FFFFFF' size='25'>Guy: Ummm...No.</font>",0x28A328));
  409. Text.add(new TextArea("<font color='#FFFFFF' size='25'>Did you think you could defeat me\nwith progessively harder zombies.\n You're Pathetic.</font>",0x28A328));
  410. Text.add(new TextArea("<font color='#000000' size='25'>hahaha.</font>",0x28A328));
  411. Text.add(new TextArea("<font color='#000000' size='25'>They weren't progressively harder.\nYou're just getting weaker.</font>",0x28A328));
  412. Text.add(new TextArea("<font color='#FFFFFF' size='25'>What?</font>",0x28A328));
  413. Text.add(new TextArea("<font color='#000000' size='25'>I mean it was pretty obivous. Give up!\nEven if you can defeat me, there will be someone else.\nYou can't defeat the whole world</font>",0x28A328));
  414. Text.add(new TextArea("<font color='#FFFFFF' size='25'>I can, and I will</font>",0x28A328));
  415. player.stop();
  416. Level7 = new FinalScene(new SunsetBackground(),Text,characters);
  417. Scenes[currentLevel-1]=Level7;
  418. State = "Playing";
  419. }
  420. else if(currentLevel > 7)
  421. {
  422. //Beat All Levels.
  423. State = "Won";
  424. return false;
  425. }
  426. //Level1.Load();
  427. Scenes[currentLevel-1].Load(volume,sound);
  428. Scenes[currentLevel-1].pauseMenu.renderVolume(volume);
  429. Scenes[currentLevel-1].pauseMenu.renderSoundEffects(sound);
  430. Scenes[currentLevel-1].addEventListener("VolumeToggle",onSceneVolumeToggle);
  431. Scenes[currentLevel-1].addEventListener("SoundEffectsToggle",onSceneSoundEffectsToggle);
  432. return true;
  433. }
  434. public function getLevelData()
  435. {
  436. currentLevel = 1;
  437. pausePosition = 0;
  438. //Load Save Data
  439. savedData = SharedObject.getLocal("LevelData");
  440. if(savedData.data.Level != null)
  441. {
  442. currentLevel = savedData.data.Level;
  443. pausePosition = savedData.data.SoundPosition;
  444. }
  445. }
  446. public function removeClip(preloader)
  447. {
  448. removeChild(preloader);
  449. }
  450. private function onStartGameButtonClick(event:MouseEvent) {
  451. currentLevel = 1;
  452. pausePosition = 0;
  453. removeMenu();
  454. State = "Loading";
  455. }
  456. private function onContinueGameButtonClick(event:MouseEvent) {
  457. removeMenu();
  458. State = "Loading";
  459. }
  460. private function onMainMenuButtonClick(event:MouseEvent) {
  461. removeTryAgainMenu();
  462. State = "LoadMainMenu";
  463. }
  464. private function onInstrutionsButtonClick(event:MouseEvent) {
  465. removeMenu();
  466. addChild(Instruction);
  467. }
  468. private function onCreditsButtonClick(event:MouseEvent) {
  469. removeMenu();
  470. addChild(Credit);
  471. }
  472. private function onOptionsButtonClick(event:MouseEvent) {
  473. removeMenu();
  474. addChild(OptionsMenu);
  475. OptionsMenu.renderVolume(volume);
  476. OptionsMenu.renderSoundEffects(sound);
  477. }
  478. private function onInstrutionsBack(event:InstructionPageEvent)
  479. {
  480. removeChild(Instruction);
  481. loadMenu();
  482. }
  483. private function onOptionsMenuBack(event:OptionsPageEvent)
  484. {
  485. removeChild(OptionsMenu);
  486. loadMenu();
  487. }
  488. private function onOptionsMenuVolumeToggle(event:OptionsPageEvent)
  489. {
  490. volume = !volume;
  491. OptionsMenu.renderVolume(volume);
  492. }
  493. private function onOptionsMenuSoundEffectsToggle(event:OptionsPageEvent)
  494. {
  495. sound = !sound;
  496. OptionsMenu.renderSoundEffects(sound);
  497. }
  498. private function onSceneVolumeToggle(event:PauseMenuEvent)
  499. {
  500. volume = !volume;
  501. Scenes[currentLevel-1].volume = volume;
  502. Scenes[currentLevel-1].pauseMenu.renderVolume(volume);
  503. }
  504. private function onSceneSoundEffectsToggle(event:PauseMenuEvent)
  505. {
  506. sound = !sound;
  507. Scenes[currentLevel-1].sound = sound;
  508. Scenes[currentLevel-1].pauseMenu.renderSoundEffects(sound);
  509. }
  510. private function onOptionsMenuResetSavedData(event:OptionsPageEvent)
  511. {
  512. savedData.data.Level = 1;
  513. savedData.data.SoundPosition = 0;
  514. savedData.flush();
  515. getLevelData();
  516. pausePosition = 0;
  517. currentLevel = 1;
  518. }
  519. private function onCreditsBack(event:CreditsPageEvent)
  520. {
  521. removeChild(Credit);
  522. loadMenu();
  523. }
  524. //Couldn't get preloader to work.
  525. static function onEnterFrame(event : Event)
  526. {
  527. Log.trace("Downloaded " + Lib.current.root.loaderInfo.bytesLoaded + " of " + Lib.current.root.loaderInfo.bytesTotal + " bytes");
  528. if (Lib.current.root.loaderInfo.bytesLoaded >= Lib.current.root.loaderInfo.bytesTotal)
  529. {
  530. Lib.current.root.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  531. Log.trace("Preloading finished");
  532. Lib.current.gotoAndStop("start");
  533. // new Images();
  534. var mainInstance:Main = new Main();
  535. }
  536. }
  537. static function main() {
  538. // Log.trace("Starting preloader");
  539. //Lib.current.root.addEventListener(Event.ENTER_FRAME, onEnterFrame);
  540. new Main();
  541. // Create the request object...
  542. // var loader = new flash.display.Loader();
  543. // // When the image is ready, instanciate the game class...
  544. // loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE,
  545. // function(_) { new Main(untyped loader.content.bitmapData); });
  546. // // Fire off the request and wait...
  547. // loader.load(new flash.net.URLRequest("img/file.gif"));
  548. //new Main();
  549. }
  550. }