PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Main.hx

http://save-the-princess.googlecode.com/
Haxe | 586 lines | 451 code | 79 blank | 56 comment | 31 complexity | f96acf2ca1f9460853c4db9a9e6563d6 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 Scene;
  29. import TextArea;
  30. import InstructionPage;
  31. import CreditsPage;
  32. import OptionsPage;
  33. import SoundPlayer;
  34. import PauseMenu;
  35. import Kongregate;
  36. import Logo;
  37. import CustomSprite;
  38. class Main extends Sprite{
  39. private var ground : Int;
  40. private var currentLevel : Int;
  41. private var Scenes : Array<Scene>;
  42. //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.
  43. private var Level1 : Scene;
  44. private var State : String;
  45. // private var savedData:SharedObject;
  46. private var ContinueGameButton:Sprite;
  47. private var StartGameButton:Sprite;
  48. private var MainMenuButton:Sprite;
  49. private var InstructionsButton:Sprite;
  50. private var CreditsButton:Sprite;
  51. private var OptionsButton:Sprite;
  52. private var Title:TextField;
  53. private var myLogo : Logo;
  54. private var label1:TextField;
  55. private var Zombie:Sprite;
  56. var myTimer:Timer;
  57. //var music:BackgroundMusic;
  58. public var Castle:CustomSprite;
  59. var preloader:PreloaderHost;
  60. private var BackgroundImage : Bitmap;
  61. public var Instruction : InstructionPage;
  62. public var Credit: CreditsPage;
  63. public var OptionsMenu : OptionsPage;
  64. var pausePosition:Float;
  65. // music.play();
  66. var volume:Bool;
  67. var sound:Bool;
  68. var framerate:Int;
  69. var mKeyDown:Array<Bool>;
  70. public function new(){
  71. haxe.remoting.AMFConnection.registerClassAlias("FastListAlias",FastList);
  72. super();
  73. Scenes = [];
  74. getLevelData();
  75. //inPostion,inMoveSpeed,inJumpSpeed,inAttackSpeed,inHealth:Int
  76. initLevels();
  77. //Add Main to Current Flash
  78. flash.Lib.current.addChild(this);
  79. //Create Menu (own Class?)
  80. Title = new TextField();
  81. Title.x = 50; Title.y = 20;
  82. Title.width = Constants.flashWidth-100; Title.height = Constants.flashHeight;
  83. Title.multiline = true;
  84. var myFormat:TextFormat = new TextFormat();
  85. myFormat.size = 25;
  86. myFormat.align = TextFormatAlign.CENTER;
  87. Title.defaultTextFormat = myFormat;
  88. Title.htmlText = "Save The Princess";
  89. Castle = new CustomSprite();
  90. Castle.graphics.beginBitmapFill(new CastleMainImage());
  91. Castle.graphics.drawRect(0, 0, 530,500);
  92. // Castle.graphics.beginFill(0xFFFFFF);
  93. // Castle.graphics.lineStyle(1);
  94. // Castle.graphics.drawRect(0,0,530,500);
  95. Castle.graphics.endFill();
  96. Castle.x = 150;Castle.y= 100;
  97. var txtFormat:TextFormat = new TextFormat();
  98. txtFormat.align = TextFormatAlign.CENTER;
  99. //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.
  100. StartGameButton = new Sprite();
  101. StartGameButton.graphics.beginBitmapFill(new Flag());
  102. StartGameButton.graphics.drawRect(0, 0, 130 ,230);
  103. StartGameButton.x = 152;
  104. StartGameButton.y = 140;
  105. var StartGameButtonText = new TextField();
  106. StartGameButtonText.htmlText = "<font size='20' color='#000000'>"+"Start Game"+"</font>";
  107. StartGameButtonText.width = 120;
  108. StartGameButtonText.x = 2;
  109. StartGameButtonText.y = 50;
  110. StartGameButtonText.setTextFormat(txtFormat);
  111. StartGameButtonText.selectable = false;
  112. StartGameButtonText.mouseEnabled = false;
  113. StartGameButton.addChild(StartGameButtonText);
  114. StartGameButton.buttonMode = true;
  115. StartGameButton.addEventListener(MouseEvent.CLICK, onStartGameButtonClick);
  116. ContinueGameButton = new Sprite();
  117. ContinueGameButton.graphics.beginBitmapFill(new Flag());
  118. ContinueGameButton.graphics.drawRect(0, 0, 130 ,230);
  119. ContinueGameButton.x = 283;
  120. ContinueGameButton.y = 140;
  121. var ContinueGameButtonText = new TextField();
  122. ContinueGameButtonText.htmlText = "<font size='20'>"+"Continue\nGame"+"</font>";
  123. ContinueGameButtonText.width = 130;
  124. ContinueGameButtonText.x = 0;
  125. ContinueGameButtonText.setTextFormat(txtFormat);
  126. ContinueGameButtonText.y = 50;
  127. ContinueGameButtonText.selectable = false;
  128. ContinueGameButtonText.mouseEnabled = false;
  129. ContinueGameButton.addChild(ContinueGameButtonText);
  130. ContinueGameButton.buttonMode = true;
  131. ContinueGameButton.addEventListener(MouseEvent.CLICK, onContinueGameButtonClick);
  132. InstructionsButton = new Sprite();
  133. InstructionsButton.graphics.beginBitmapFill(new Flag());
  134. InstructionsButton.graphics.drawRect(0, 0, 130 ,230);
  135. InstructionsButton.x = 390;
  136. InstructionsButton.y = 150;
  137. InstructionsButton.buttonMode = true;
  138. InstructionsButton.addEventListener(MouseEvent.CLICK, onInstrutionsButtonClick);
  139. Instruction = new InstructionPage();
  140. Instruction.addEventListener("Back",onInstrutionsBack);
  141. CreditsButton = new Sprite();
  142. CreditsButton.graphics.beginBitmapFill(new Flag());
  143. CreditsButton.graphics.drawRect(0, 0, 130 ,230);
  144. CreditsButton.x = 414;
  145. CreditsButton.y = 140;
  146. var CreditsButtonText = new TextField();
  147. CreditsButtonText.htmlText = "<font size='20' >"+"Credits"+"</font>";
  148. CreditsButtonText.width = 130;
  149. CreditsButtonText.x = 0;
  150. CreditsButtonText.setTextFormat(txtFormat);
  151. CreditsButtonText.y = 50;
  152. CreditsButtonText.selectable = false;
  153. CreditsButtonText.mouseEnabled = false;
  154. CreditsButton.addChild(CreditsButtonText);
  155. CreditsButton.buttonMode = true;
  156. CreditsButton.addEventListener(MouseEvent.CLICK, onCreditsButtonClick);
  157. Credit = new CreditsPage();
  158. Credit.addEventListener("Back",onCreditsBack);
  159. OptionsButton = new Sprite();
  160. OptionsButton.graphics.beginBitmapFill(new Flag());
  161. OptionsButton.graphics.drawRect(0, 0, 130 ,230);
  162. var OptionsButtonText = new TextField();
  163. OptionsButtonText.htmlText = "<font size='20'>"+"Options"+"</font>";
  164. OptionsButtonText.width = 130;
  165. OptionsButtonText.x = 0;
  166. OptionsButtonText.setTextFormat(txtFormat);
  167. OptionsButtonText.y = 50;
  168. OptionsButtonText.selectable = false;
  169. OptionsButtonText.mouseEnabled = false;
  170. OptionsButton.addChild(OptionsButtonText);
  171. OptionsButton.x = 545;
  172. OptionsButton.y = 140;
  173. OptionsButton.buttonMode = true;
  174. OptionsButton.addEventListener(MouseEvent.CLICK, onOptionsButtonClick);
  175. OptionsMenu = new OptionsPage();
  176. OptionsMenu.addEventListener("Back",onOptionsMenuBack);
  177. OptionsMenu.addEventListener("ResetSavedData",onOptionsMenuResetSavedData);
  178. OptionsMenu.addEventListener("VolumeToggle",onOptionsMenuVolumeToggle);
  179. OptionsMenu.addEventListener("SoundEffectsToggle",onOptionsMenuSoundEffectsToggle);
  180. //this.addEventListener(Event.ENTER_FRAME, OnEnter);
  181. myTimer = new Timer(12);
  182. myTimer.addEventListener("timer", OnEnter);
  183. myTimer.start();
  184. label1 = new TextField();
  185. label1.x = 0; label1.y = 0;
  186. label1.width = Constants.flashWidth; label1.height = 100;
  187. label1.autoSize = TextFieldAutoSize.CENTER;
  188. Zombie = new Sprite();
  189. Zombie.graphics.beginBitmapFill(new Menu());
  190. Zombie.graphics.drawRect(0, 0, 46,52);
  191. Zombie.x = 300;
  192. Zombie.y = 75;
  193. MainMenuButton = new Sprite();
  194. MainMenuButton.graphics.beginBitmapFill(new MainMenu());
  195. MainMenuButton.graphics.drawRect(0, 0, 250 ,48);
  196. MainMenuButton.x = 200;
  197. MainMenuButton.y = 135;
  198. MainMenuButton.buttonMode = true;
  199. MainMenuButton.addEventListener(MouseEvent.CLICK, onMainMenuButtonClick);
  200. //The State Will determine whether to load a level, or show Main Menu, or whatever. Used in the OnEnterEvent.
  201. //State = "StartGame";
  202. State = "LoadMainMenu";
  203. //music = new BackgroundMusic();
  204. preloader = new PreloaderHost();
  205. BackgroundImage = new Bitmap(new Background());
  206. volume = true;
  207. sound = true;
  208. myLogo = new Logo();
  209. addChild(myLogo);
  210. mKeyDown = [];
  211. stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown );
  212. stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyUp );
  213. //preloader.addEventListener(Event.ENTER_FRAME, OnMovieEnter);
  214. }
  215. public function OnKeyUp (event:KeyboardEvent)
  216. {
  217. mKeyDown[event.keyCode] = false;
  218. //lastUp = event.keyCode;
  219. }
  220. public function OnKeyDown(event:KeyboardEvent)
  221. {
  222. // When a key is held down, multiple KeyDown events are generated.
  223. // This check means we only pick up the first one.
  224. if (!mKeyDown[event.keyCode])
  225. {
  226. // Store for use in game
  227. mKeyDown[event.keyCode] = true;
  228. //I do this here, because if they hold down P,
  229. // it won't pause and unpause constantly.
  230. if(mKeyDown[ 77 ] == true)
  231. {
  232. volume = !volume;
  233. Scenes[currentLevel-1].volume = volume;
  234. Scenes[currentLevel-1].pauseMenu.renderVolume(volume);
  235. }
  236. }
  237. }
  238. function loadMenu()
  239. {
  240. addChild(BackgroundImage);
  241. addChild(Castle);
  242. addChild(Title);
  243. addChild(StartGameButton);
  244. addChild(ContinueGameButton);
  245. //addChild(InstructionsButton);
  246. addChild(CreditsButton);
  247. addChild(OptionsButton);
  248. }
  249. function removeMenu()
  250. {
  251. removeChild(Title);
  252. removeChild(Castle);
  253. removeChild(StartGameButton);
  254. removeChild(ContinueGameButton);
  255. removeChild(BackgroundImage);
  256. //removeChild(InstructionsButton);
  257. removeChild(CreditsButton);
  258. removeChild(OptionsButton);
  259. }
  260. //Change to stat Menu
  261. function loadTryAgainMenu(text:String)
  262. {
  263. addChild(label1);
  264. label1.htmlText = "<font size='15'>" + text + "</font>";
  265. addChild(MainMenuButton);
  266. addChild(Zombie);
  267. }
  268. function removeTryAgainMenu()
  269. {
  270. removeChild(label1);
  271. removeChild(MainMenuButton);
  272. removeChild(Zombie);
  273. }
  274. function initLevels()
  275. {
  276. //Postion, MoveSpeed, JumpSpeed, AttackSpeed, Health, Bitmap)
  277. }
  278. function OnEnter(e:flash.events.Event)
  279. {
  280. if(State == "StartGame")
  281. {
  282. loadCurrentLevel(false);
  283. }
  284. if(State == "ContinueGame")
  285. {
  286. loadCurrentLevel(true);
  287. }
  288. if(State == "LoadMainMenu")
  289. {
  290. loadMenu();
  291. State = "MainMenu";
  292. }
  293. if(State == "MainMenu" )
  294. {
  295. return;
  296. }
  297. if(State == "TryAgain")
  298. {
  299. AnimateZombie();
  300. return;
  301. }
  302. if(State == "InitLoad")
  303. {
  304. addChild(preloader);
  305. preloader.play();
  306. State = "InitLoading";
  307. }
  308. if(State == "InitLoading")
  309. {
  310. //Always Playing. Possible error with swfmill
  311. //Skipping for now.
  312. if(!preloader.playing)
  313. {
  314. preloader.stop();
  315. removeChild(preloader);
  316. State = "LoadingLogo";
  317. }
  318. }
  319. if(State == "LoadingLogo")
  320. {
  321. if(!myLogo.play())
  322. {
  323. State = "LoadMainMenu";
  324. }
  325. }
  326. if(State == "Playing")
  327. {
  328. if(Scenes[currentLevel-1] != null)
  329. {
  330. if(Scenes[currentLevel-1].running)
  331. {
  332. return;
  333. }
  334. }
  335. loadCurrentLevel(true);
  336. }
  337. // try
  338. // {
  339. // // var req:flash.net.URLRequest = new flash.net.URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3");
  340. // //
  341. // // Sound.load(req);
  342. // var music = new BackgroundMusic();
  343. // music.play();
  344. // }
  345. // catch (err:flash.Error)
  346. // {
  347. // trace(err.message);
  348. // }
  349. }
  350. public function AnimateZombie()
  351. {
  352. var randomNumber = Std.random(24);
  353. if(randomNumber == 1)
  354. Zombie.x = Zombie.x + 5;
  355. if(randomNumber == 2)
  356. Zombie.x = Zombie.x - 5;
  357. }
  358. //Loads Level based on Current Level
  359. public function loadCurrentLevel(inContinue:Bool) : Bool
  360. {
  361. currentLevel = 1;
  362. Level1 = new Scene(new Background());
  363. Scenes[currentLevel-1] = Level1;
  364. State = "Playing";
  365. //Level1.Load();
  366. Scenes[currentLevel-1].Load(volume,sound,inContinue);
  367. Scenes[currentLevel-1].pauseMenu.renderVolume(volume);
  368. // Scenes[currentLevel-1].pauseMenu.renderSoundEffects(sound);
  369. Scenes[currentLevel-1].addEventListener("VolumeToggle",onSceneVolumeToggle);
  370. Scenes[currentLevel-1].addEventListener("SoundEffectsToggle",onSceneSoundEffectsToggle);
  371. Scenes[currentLevel-1].addEventListener("LevelQuit",onLevelQuit);
  372. Scenes[currentLevel-1].addEventListener("LevelRetry",onLevelRetry);
  373. return true;
  374. }
  375. private function onLevelRetry(event:Event)
  376. {
  377. removeLevelEventListeners();
  378. State = "StartGame";
  379. Scenes[currentLevel-1].unLoad();
  380. }
  381. private function onLevelQuit(event:Event)
  382. {
  383. removeLevelEventListeners();
  384. State = "LoadingLogo";
  385. Scenes[currentLevel-1].unLoad();
  386. }
  387. private function removeLevelEventListeners()
  388. {
  389. Scenes[currentLevel-1].removeEventListener("VolumeToggle",onSceneVolumeToggle);
  390. Scenes[currentLevel-1].removeEventListener("SoundEffectsToggle",onSceneSoundEffectsToggle);
  391. Scenes[currentLevel-1].removeEventListener("LevelQuit",onLevelQuit);
  392. Scenes[currentLevel-1].removeEventListener("LevelRetry",onLevelRetry);
  393. }
  394. public function getLevelData()
  395. {
  396. }
  397. public function removeClip(preloader)
  398. {
  399. removeChild(preloader);
  400. }
  401. private function onStartGameButtonClick(event:MouseEvent) {
  402. currentLevel = 1;
  403. pausePosition = 0;
  404. removeMenu();
  405. State = "StartGame";
  406. }
  407. private function onContinueGameButtonClick(event:MouseEvent) {
  408. removeMenu();
  409. State = "ContinueGame";
  410. }
  411. private function onMainMenuButtonClick(event:MouseEvent) {
  412. removeTryAgainMenu();
  413. State = "LoadMainMenu";
  414. }
  415. private function onInstrutionsButtonClick(event:MouseEvent) {
  416. removeMenu();
  417. addChild(Instruction);
  418. }
  419. private function onCreditsButtonClick(event:MouseEvent) {
  420. removeMenu();
  421. addChild(Credit);
  422. }
  423. private function onOptionsButtonClick(event:MouseEvent) {
  424. removeMenu();
  425. addChild(OptionsMenu);
  426. OptionsMenu.renderVolume(volume);
  427. OptionsMenu.renderSoundEffects(sound);
  428. }
  429. private function onInstrutionsBack(event:InstructionPageEvent)
  430. {
  431. removeChild(Instruction);
  432. loadMenu();
  433. }
  434. private function onOptionsMenuBack(event:OptionsPageEvent)
  435. {
  436. removeChild(OptionsMenu);
  437. loadMenu();
  438. }
  439. private function onOptionsMenuVolumeToggle(event:OptionsPageEvent)
  440. {
  441. volume = !volume;
  442. OptionsMenu.renderVolume(volume);
  443. Scenes[currentLevel-1].pauseMenu.renderVolume(volume);
  444. }
  445. private function onOptionsMenuSoundEffectsToggle(event:OptionsPageEvent)
  446. {
  447. sound = !sound;
  448. OptionsMenu.renderSoundEffects(sound);
  449. }
  450. private function onSceneVolumeToggle(event:PauseMenuEvent)
  451. {
  452. volume = !volume;
  453. Scenes[currentLevel-1].volume = volume;
  454. Scenes[currentLevel-1].pauseMenu.renderVolume(volume);
  455. }
  456. private function onSceneSoundEffectsToggle(event:PauseMenuEvent)
  457. {
  458. sound = !sound;
  459. Scenes[currentLevel-1].sound = sound;
  460. //Scenes[currentLevel-1].pauseMenu.renderSoundEffects(sound);
  461. }
  462. private function onOptionsMenuResetSavedData(event:OptionsPageEvent)
  463. {
  464. var savedData = SharedObject.getLocal("GameData");
  465. savedData.data.Player = null;
  466. savedData.data.PrincessPosition = null;
  467. savedData.flush();
  468. }
  469. private function onCreditsBack(event:CreditsPageEvent)
  470. {
  471. removeChild(Credit);
  472. loadMenu();
  473. }
  474. //Couldn't get preloader to work.
  475. static function onEnterFrame(event : Event)
  476. {
  477. Log.trace("Downloaded " + Lib.current.root.loaderInfo.bytesLoaded + " of " + Lib.current.root.loaderInfo.bytesTotal + " bytes");
  478. if (Lib.current.root.loaderInfo.bytesLoaded >= Lib.current.root.loaderInfo.bytesTotal)
  479. {
  480. Lib.current.root.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  481. Log.trace("Preloading finished");
  482. Lib.current.gotoAndStop("start");
  483. // new Images();
  484. var mainInstance:Main = new Main();
  485. }
  486. }
  487. static function main() {
  488. // Log.trace("Starting preloader");
  489. //Lib.current.roott.addEventListener(Event.ENTER_FRAME, onEnterFrame);
  490. try
  491. {
  492. new Main();
  493. }
  494. catch (err:flash.Error)
  495. {
  496. trace(err.message);
  497. }
  498. // Create the request object...
  499. // var loader = new flash.display.Loader();
  500. // // When the image is ready, instanciate the game class...
  501. // loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE,
  502. // function(_) { new Main(untyped loader.content.bitmapData); });
  503. // // Fire off the request and wait...
  504. // loader.load(new flash.net.URLRequest("img/file.gif"));
  505. //new Main();
  506. }
  507. }