PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/World.hx

http://github.com/gwillen/test
Haxe | 318 lines | 206 code | 43 blank | 69 comment | 25 complexity | ea06ee60267f1614b01b817db1ff7d5e MD5 | raw file
Possible License(s): GPL-3.0
  1. import Utils.Option;
  2. import Utils;
  3. import flash.net.URLRequest;
  4. import flash.display.Loader;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.display.Bitmap;
  8. import flash.display.BitmapData;
  9. typedef World_t = Array<Tile>;
  10. typedef Coor = {
  11. var x : Int;
  12. var y : Int;
  13. }
  14. class World {
  15. public static var tile:Array<Ref<Loader>>;
  16. // These are indices into the tile array.
  17. public static var NOTHING : Int = 0;
  18. public static var WALL : Int = 14;
  19. public static var MOVEDOWN : Int = 10;
  20. public static var MOVELEFT : Int = 11;
  21. public static var MOVERIGHT : Int = 12;
  22. public static var MOVEUP : Int = 13;
  23. public static var TURTLEL : Int = 0x0001;
  24. public static var TURTLER : Int = 0x0002;
  25. public static var TURTLELDEAD : Int = 0x0035;
  26. public static var TURTLERDEAD : Int = 0x0036;
  27. public static var CONVEYORL : Int = 0x0032;
  28. public static var CONVEYORR : Int = 0x0033;
  29. public static var GWILLEN : Int = 0x0042;
  30. public static var tilesLoaded : Bool = false;
  31. public static function loadStuff() {
  32. tileStyles = new Array<TileStyle>();
  33. allTiles = new Array<Tile>();
  34. // Danger: This call is async.
  35. LoadStuff.loadTileMap(tileStyles, function() {
  36. for (i in 0...tileStyles.length) {
  37. allTiles[i] = new Tile();
  38. allTiles[i].style = tileStyles[i];
  39. tilesLoaded = true;
  40. }
  41. });
  42. LoadStuff.loadImageAndCall("background_nightsky.png", function(l) {
  43. Game.setBackground(l);
  44. });
  45. // Also async
  46. worldState = new Array<Tile>();
  47. LoadStuff.loadLevel("skyline1.map", worldState);
  48. // call initTiles when the loading is all completed. XXX: it is no longer
  49. // really necessary to do this in this order, since all our logic is in
  50. // frameDidLoad or whatever, which won't get called till we return from
  51. // main (which is what's calling this to do the loading.)
  52. /*
  53. LoadStuff.startBatchLoad(initTiles);
  54. LoadStuff.batchLoadImage("assets/tile_xwindows.png", tile[0]);
  55. LoadStuff.batchLoadImage("assets/tile_suspensionbridge.png", tile[1]);
  56. LoadStuff.batchLoadImage("assets/tile_zebra.png", tile[2]);
  57. LoadStuff.batchLoadImage("assets/tile_psychedelic.png", tile[3]);
  58. LoadStuff.endBatchLoad();
  59. */
  60. //initTiles();
  61. Game.debugtf.trace("World loading stuff.\n");
  62. }
  63. // Returns the tile that this thing should become if it dies.
  64. public static function deathTile(t : Int) : Int {
  65. switch(t) {
  66. case 0x0001: return World.TURTLELDEAD;
  67. case 0x0002: return World.TURTLERDEAD;
  68. default: return World.NOTHING;
  69. }
  70. }
  71. public static var worldState: World_t;
  72. public static var tileStyles : Array<TileStyle>;
  73. public static var allTiles : Array<Tile>;
  74. public static function findAndRemoveBadgers(world : Array<Tile>) : Array<Coor> {
  75. var rv : Array<Coor> = new Array<Coor>();
  76. for (i in 0...world.length) {
  77. var c = tileCoords(i);
  78. if (world[i].style.prop.isbadger) {
  79. world[i] = allTiles[0]; // empty tile
  80. rv.push(c);
  81. }
  82. }
  83. return rv;
  84. }
  85. public static function moveBadger(world : World_t, new_x, new_y)
  86. : Void {
  87. var badger = Option.none;
  88. for (i in 0...world.length) {
  89. if ( world[i].style.prop.isbadger) {
  90. badger = Option.some(world[i]);
  91. world[i] = allTiles[0]; // empty tile
  92. break;
  93. }
  94. }
  95. var dest = getTileW(world, new_x, new_y);
  96. if(dest == allTiles[0]){
  97. switch (badger) {
  98. case some(w): {setTileW(world, new_x, new_y, w);}
  99. case none : {return;}
  100. }
  101. }
  102. /*
  103. switch(badger) {
  104. case some(t): {
  105. trace("found a badger");
  106. for (i in 0...world.length) {
  107. if (new_x*30 + new_y == i) { // new badger location
  108. world[i] = t;
  109. }
  110. }
  111. }
  112. default: {
  113. trace("DID NOT FIND IT");
  114. }
  115. }
  116. */
  117. }
  118. public static function drawTheTiles(frame : Int) {
  119. drawTiles(worldState, frame);
  120. }
  121. public static function clearTheTiles() {
  122. while(Game.mainmc.numChildren > 0) {
  123. Game.mainmc.removeChildAt(0);
  124. }
  125. }
  126. // game parameters
  127. public static var screenw:Int = 600;
  128. public static var screenh:Int = 600;
  129. public static var tilesize:Int = Tile.size;
  130. public static var tilesh = Math.round(screenh/tilesize);
  131. public static var tilesw = Math.round(screenw/tilesize);
  132. /*
  133. public static function initTiles () {
  134. Game.debugtf.trace("initTiles");
  135. worldState = new Array<Tile>();
  136. for (y in 0...tilesh) {
  137. for (x in 0...tilesw) {
  138. setTile(x, y, new Tile());
  139. }
  140. }
  141. }
  142. */
  143. public static function drawTiles(tiles : Array<Tile>, frame : Int) {
  144. for (y in 0...tilesh) {
  145. for (x in 0...tilesw) {
  146. var thistile:Tile = getTile(x, y);
  147. if (thistile == null) {
  148. // trace('NULL ASSHOLE');
  149. } else {
  150. var tileb = thistile.getImage(frame, x, y);
  151. var b = new Bitmap(tileb);
  152. var s = new Sprite();
  153. s.x = x * tilesize;
  154. s.y = y * tilesize - 12; // 12-pixel overlap zone
  155. s.addChild(b);
  156. Game.mainmc.addChild(s);
  157. }
  158. }
  159. }
  160. }
  161. public static function tileIndex(x: Int, y: Int) : Int {
  162. return y*tilesw + x;
  163. }
  164. public static function tileCoords(index: Int) : Coor {
  165. return { x : index % tilesw, y : Math.floor(index / tilesw) };
  166. }
  167. public static function getTile(x:Int, y:Int) : Tile {
  168. return worldState[tileIndex(x, y)];
  169. }
  170. public static function getTileW(w: World_t, x:Int, y:Int) : Tile {
  171. return w[tileIndex(x, y)];
  172. }
  173. public static function setTile(x:Int, y:Int, t:Tile) {
  174. worldState[y*tilesw + x] = t;
  175. }
  176. public static function setTileW(w: World_t, x:Int, y:Int, t:Tile) {
  177. w[y*tilesw + x] = t;
  178. }
  179. // XXX
  180. // Must work on invalid coords; return true.
  181. public static function isBlocked(w: World_t, x:Int, y:Int) :Bool {
  182. if (x < 0 || x >= tilesw || y < 0 || y >= tilesh) {
  183. return true;
  184. } else {
  185. return getTileW(w,x, y).style.prop.solid;
  186. }
  187. }
  188. // XXX
  189. // Must work on invalid coords; return false;
  190. public static function canStandOn(w: World_t, x:Int, y:Int) :Bool {
  191. if (x < 0 || x >= tilesw || y < 0 || y >= tilesh) {
  192. return false;
  193. } else {
  194. return getTileW(w,x, y).style.prop.standon;
  195. }
  196. }
  197. /*XXX dummy
  198. public static function findBadgers() :Array<{x:Int, y:Int}> {
  199. return [{x:4, y:11}];
  200. }
  201. */
  202. }
  203. enum TileType {
  204. nothing;
  205. floor;
  206. movingfloor;
  207. floorstop; // Displays and acts like nothing, but stops moving floors
  208. butan; // Pressed or unpressed
  209. man;
  210. turtal;
  211. }
  212. class MovingFloor {
  213. var id:Option<Int>; // id of linked butan
  214. }
  215. class TileFrame {
  216. public function new() {}
  217. public var filename : String;
  218. public var buf : BitmapData;
  219. public var delay : Int;
  220. }
  221. class TileProperties {
  222. public function new(bits : String) {
  223. standon = Utils.parseBool(bits.charAt(0));
  224. solid = Utils.parseBool(bits.charAt(1));
  225. isbadger = Utils.parseBool(bits.charAt(2));
  226. isturtle = Utils.parseBool(bits.charAt(3));
  227. conveyed = Utils.parseBool(bits.charAt(4));
  228. falls = Utils.parseBool(bits.charAt(5));
  229. nostep = Utils.parseBool(bits.charAt(6));
  230. }
  231. public var standon : Bool;
  232. public var solid : Bool;
  233. public var isbadger : Bool;
  234. public var isturtle : Bool;
  235. // Moved by conveyors.
  236. public var conveyed : Bool;
  237. // Falls if nothing's under it.
  238. public var falls : Bool;
  239. // If something lands on it, then it dies.
  240. public var nostep : Bool;
  241. }
  242. class TileStyle {
  243. public function new() {}
  244. public var frames : Array<TileFrame>;
  245. public var totalFrames : Int; // Sum of all frames[i].delay
  246. public var prop : TileProperties;
  247. public var id : Int;
  248. }
  249. class Tile {
  250. public function new() {}
  251. public static var size:Int = 20; // width and height
  252. //public var type:TileType;
  253. public var style:TileStyle;
  254. public function getImage(frame : Int, x : Int, y : Int) {
  255. // Default animation mode is position-dependent
  256. var animFrame = (frame+x+y) % style.totalFrames;
  257. for (i in 0...style.frames.length) {
  258. animFrame -= style.frames[i].delay;
  259. if (animFrame < 0) {
  260. return style.frames[i].buf;
  261. }
  262. }
  263. throw("Malformed animation?");
  264. /*
  265. var styleNum : Int = Math.floor(Math.random() * World.tileStyles.length);
  266. try {
  267. return World.tileStyles[styleNum].frames[0].buf;
  268. } catch ( d : Dynamic ) {
  269. Game.debugtf.trace("Failed number was " + styleNum);
  270. //Game.debugtf.trace("Failed frame was " + World.tileStyles[styleNum].frames[0].filename);
  271. return World.tileStyles[0].frames[0].buf;
  272. }
  273. */
  274. }
  275. }