PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Game.hx

http://github.com/gwillen/test
Haxe | 158 lines | 123 code | 24 blank | 11 comment | 20 complexity | c0d97bb5c2f15e90338de3f4c7d05dc7 MD5 | raw file
Possible License(s): GPL-3.0
  1. import flash.display.MovieClip;
  2. import flash.display.Sprite;
  3. import flash.text.TextField;
  4. import flash.display.Graphics;
  5. import flash.Lib;
  6. import flash.display.Loader;
  7. import flash.net.URLRequest;
  8. import flash.display.Sprite;
  9. import flash.display.Bitmap;
  10. import World;
  11. import World.World;
  12. import World.Tile;
  13. import DebugTextField;
  14. import flash.events.Event;
  15. import flash.events.MouseEvent;
  16. import flash.events.KeyboardEvent;
  17. import Simulate;
  18. enum GameState {
  19. initialization;
  20. titleScreen;
  21. gamePlay;
  22. }
  23. class Game {
  24. public static var WORLD_HEIGHT = 600;
  25. public static var WORLD_WIDTH = 600;
  26. public static var rootmc : MovieClip;
  27. public static var mainmc : MovieClip;
  28. public static var backgroundmc : MovieClip;
  29. public static var debugtf : DebugTextField;
  30. public static var lastClick;
  31. static function handleclick(event : MouseEvent) {
  32. lastClick = event;
  33. debugtf.trace("you mousedowned at " + event.localX + " " + event.localY + "\n");
  34. // teleport the badget there
  35. var tile_coord:Coor = {x : cast (event.stageX / World.tilesize) ,
  36. y : cast (event.stageY / World.tilesize)};
  37. trace ("teleporting to :" + Jump.stringofcoor(tile_coord));
  38. World.moveBadger(World.worldState, tile_coord.x, tile_coord.y);
  39. }
  40. public static var lastKey;
  41. static function handlekeydown(event : KeyboardEvent) {
  42. lastKey = event;
  43. debugtf.trace("you pushed this button: " + event.charCode + " " +
  44. event.keyCode + "\n");
  45. if(event.charCode == 100){ // 'd'
  46. debugtf.visible = !debugtf.visible;
  47. }
  48. if(event.keyCode == 39) { // ->
  49. // XXX make badger move right
  50. trace("moveright");
  51. }
  52. if(event.keyCode == 37) { // <-
  53. // XXX make badger move left
  54. trace("moveleft");
  55. }
  56. }
  57. // XXX this will wrap stupidly and everything will be ruined forever
  58. static var frame : Int = 0;
  59. static function mainLoop(e : Event) {
  60. // race condition lol
  61. //trace('mainloop:');
  62. if (gamestate == initialization) {
  63. return;
  64. } else if (gamestate == titleScreen) {
  65. if (lastClick == null && lastKey == null) {
  66. return;
  67. } else {
  68. gamestate = GameState.gamePlay;
  69. startGame();
  70. return;
  71. }
  72. }
  73. if (!LoadStuff.loadsDone() ||
  74. !World.tilesLoaded) {
  75. return;
  76. }
  77. Achievements.tick();
  78. // trace('clearthetiles:');
  79. World.clearTheTiles();
  80. // trace('drawthetiles:');
  81. World.drawTheTiles(frame++);
  82. var state0 = World.worldState;
  83. var state1 = World.worldState.copy();
  84. var badger_coord = World.findAndRemoveBadgers(state1)[0]; //XXX
  85. var badg_x = badger_coord.x;
  86. var badg_y = badger_coord.y;
  87. //trace ("badger x = " + badg_x + " and y = " + badg_y);
  88. var jump_dests = Utils.map(function(j:Jump.Jmp) { return j.dest; },
  89. Jump.validJumps(state0, state1, badg_x, badg_y));
  90. Simulate.drawMovesRel(badg_x, badg_y, jump_dests);
  91. // This is retardo -- you need to do this as part of PROPOSAL X.
  92. if ((frame % 5) == 0) {
  93. World.worldState = Simulate.step(World.worldState);
  94. }
  95. }
  96. private static function myTrace( v : Dynamic, ?inf : haxe.PosInfos ) {
  97. debugtf.trace(v+"\n");
  98. }
  99. public static var gamestate = initialization;
  100. static function main() {
  101. try {
  102. haxe.Log.trace = myTrace;
  103. rootmc = flash.Lib.current;
  104. mainmc = new MovieClip();
  105. backgroundmc = new MovieClip();
  106. mainmc.addEventListener(Event.ENTER_FRAME, mainLoop);
  107. debugtf = new DebugTextField();
  108. rootmc.addChild(backgroundmc);
  109. rootmc.addChild(mainmc);
  110. rootmc.addChild(debugtf);
  111. mainmc.stage.addEventListener(MouseEvent.MOUSE_DOWN, handleclick );
  112. mainmc.stage.addEventListener(KeyboardEvent.KEY_DOWN, handlekeydown );
  113. LoadStuff.loadImageAndCall("title.png", function(l) {
  114. Game.setBackground(l);
  115. });
  116. gamestate = titleScreen;
  117. //flash.Lib.setErrorHandler(Utils.myHandler);
  118. } catch ( s : String ) {
  119. trace("Exception (String): " + s);
  120. } catch ( unknown : Dynamic ) {
  121. trace("Exception (Dynamic): " + Std.string(unknown));
  122. }
  123. }
  124. public static function setBackground(l:flash.display.DisplayObject) {
  125. while (backgroundmc.numChildren > 0) {
  126. backgroundmc.removeChildAt(0);
  127. }
  128. backgroundmc.addChild(l);
  129. }
  130. static function startGame() {
  131. Achievements.init();
  132. World.loadStuff();
  133. }
  134. }