map.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. var TILE_PASSABLE = 0x0001,
  2. TILE_DRIVEABLE = 0x0002,
  3. TILE_WALKPATH = 0x0004,
  4. TILE_BUILDABLE = 0x0008,
  5. TILE_HAS_BUILDING = 0x0010;
  6. var Map = function(options) {
  7. var defaults = {
  8. size: pt(256,256),
  9. canvasSelector: null,
  10. canvas: null,
  11. context: null,
  12. showDebugLabels: false,
  13. prevCenter: {x: 0, y: 0},
  14. texCodes: null,
  15. layout: null,
  16. nameToCode: {},
  17. images: {},
  18. };
  19. var e = $.extend({}, defaults, options);
  20. for(x in e) this[x] = e[x];
  21. this.init();
  22. }
  23. Map.prototype.init = function() {
  24. // initialize the storage
  25. this.texCodes = new Uint8Array(this.size.x * this.size.y);
  26. this.layout = new Uint16Array(this.size.x * this.size.y);
  27. this.tileInfo = new Uint8Array(this.size.x * this.size.y);
  28. this.canvas = $(this.canvasSelector)[0];
  29. this.context = this.canvas.getContext("2d");
  30. this.context.setTransform(1, 0, 0, 1, 0, 0);
  31. // temporary hacks
  32. this.texImgMapping = {
  33. 0: 'grass',
  34. 1: 'road',
  35. 2: 'sidewalk_0000',
  36. 3: 'sidewalk_0001',
  37. 4: 'sidewalk_0010',
  38. 5: 'sidewalk_0011',
  39. 6: 'sidewalk_0100',
  40. 7: 'sidewalk_0101',
  41. 8: 'sidewalk_0110',
  42. 9: 'sidewalk_0111',
  43. 10: 'sidewalk_1000',
  44. 11: 'sidewalk_1001',
  45. 12: 'sidewalk_1010',
  46. 13: 'sidewalk_1011',
  47. 14: 'sidewalk_1100',
  48. 15: 'sidewalk_1101',
  49. 16: 'sidewalk_1110',
  50. 17: 'sidewalk_1111',
  51. // 18: '',
  52. // 19: '',
  53. // 20: '',
  54. // 21: '',
  55. // 22: '',
  56. // 23: '',
  57. // 24: '',
  58. // 25: '',
  59. // 26: '',
  60. // 27: '',
  61. // 28: '',
  62. // 29: '',
  63. };
  64. this.texImgMapping_inv = inverse(this.texImgMapping);
  65. // this.images[0] = images.grass;
  66. // this.images[1] = images.debug_road;
  67. // this.images[2] = images.debug_sidewalk;
  68. // this.nameToCode['grass'] = 0;
  69. // this.nameToCode['road'] = 1;
  70. // this.nameToCode['sidewalk'] = 2;
  71. this.texLayMapping = {
  72. grass: 'grass',
  73. road: 'road',
  74. sidewalk: {
  75. type: 'directional',
  76. prefix: 'sidewalk_',
  77. },
  78. };
  79. // eventually this will be a bit field
  80. this.layoutCodes = {
  81. grass: 0,
  82. road: 1,
  83. sidewalk: 2,
  84. walkingpath: 4,
  85. }
  86. this.layoutCodes_inv = inverse(this.layoutCodes);
  87. this.layoutCodes_inv[3] = 'road';
  88. }
  89. Map.prototype.getEdges = function(center) {
  90. var cwidth = this.canvas.width;
  91. var cheight = this.canvas.height;
  92. return rect(
  93. center.y + (cheight / 128),
  94. center.y - (cheight / 128),
  95. center.x - (cwidth / 128),
  96. center.x + (cwidth / 128)
  97. );
  98. }
  99. Map.prototype.getTileAt = function(x,y) {
  100. // temp hack
  101. if(x < 0 || y < 0 || x > this.size.x || y > this.size.y)
  102. return images.debug_grass;
  103. // return this.images[this.texCodes[x + (y * this.size.x)]];
  104. var tc = this.texCodes[x + (y * this.size.x)];
  105. var img = this.texImgMapping[tc]
  106. return images[img];
  107. }
  108. Map.prototype.mapToCanvas = function(center, mx, my) {
  109. var x = 0;
  110. var y = 0;
  111. x = center.x - mx; // offset from center
  112. x *= -64; // scale for tile size and invert (dunno why but it works)
  113. x -= 32; // scoot to the middle of the tile
  114. y = center.y - my; // offset from center
  115. y *= 64; // scale for tile size
  116. y -= 32; // scoot to the middle of the tile
  117. return {
  118. x: x,
  119. y: y
  120. };
  121. }
  122. // converts HTML document coordinates to map coordinates
  123. Map.prototype.documentToMap = function(docX, docY) {
  124. var cwidth = this.canvas.width / 128;
  125. var cheight = this.canvas.height / 128;
  126. return pt(
  127. this.prevCenter.x - cwidth + docX/64,
  128. this.prevCenter.y + cheight - docY/64
  129. );
  130. }
  131. // remember y is inverted and the whole thing is scaled by 64
  132. //center is in world coordinates
  133. Map.prototype.render = function(center) {
  134. // don't render if nothing changed
  135. if(center.x == this.prevCenter.x && center.y == this.prevCenter.y) {
  136. // the fp compare is ok since we are actually checking for identical values
  137. return;
  138. }
  139. this.prevCenter = ptc(center);
  140. var cwidth = this.canvas.width;
  141. var cheight = this.canvas.height;
  142. var ctx = this.context;
  143. ctx.clearRect(0, 0, cwidth, cheight);
  144. ctx.save();
  145. // first move the origin to the middle of the screen
  146. // now all canvas coordinates will be relative to the center point
  147. ctx.translate(cwidth / 2, cheight / 2);
  148. // figure out the bounds of the map to render
  149. // these are in map coordinates
  150. var map_minx = Math.floor(center.x - (cwidth / 128));
  151. var map_maxx = Math.ceil(center.x + (cwidth / 128));
  152. var map_miny = Math.floor(center.y - (cheight / 128));
  153. var map_maxy = Math.ceil(center.y + (cheight / 128));
  154. for(var y = map_miny; y <= map_maxy; y++) {
  155. for(var x = map_minx; x <= map_maxx; x++) {
  156. // fetch which tile to render
  157. var tile = this.getTileAt(x,y);
  158. // get the top-left canvas rendering coordinate
  159. var cc = this.mapToCanvas(center, x, y);
  160. ctx.drawImage(tile, cc.x, cc.y);
  161. if(this.showDebugLabels) {
  162. ctx.font = ' 10px Sans Serif';
  163. ctx.fillText(x + "," + y, cc.x + 10, cc.y + 20);
  164. }
  165. }
  166. }
  167. ctx.restore();
  168. }
  169. Map.prototype.setTexAt = function(x, y, name) {
  170. if(x < 0 || y < 0 || x > this.size.x || y > this.size.y) return null;
  171. var old = this.nameToCode.indexOf(this.texCodes[x + (y * this.size.x)]);
  172. this.texCodes[x + (y * this.size.x)] = this.nameToCode[name];
  173. return old;
  174. }
  175. Map.prototype.fillRectTex = function(x1, y1, x2, y2, name) {
  176. var xmin = max(min(x1, x2), 0);
  177. var xmax = min(max(x1, x2), this.size.x);
  178. var ymin = max(min(y1, y2), 0);
  179. var ymax = min(max(y1, y2), this.size.y);
  180. var code = this.nameToCode[name];
  181. for(var y = ymin; y <= ymax; y++) {
  182. for(var x = xmin; x <= xmax; x++) {
  183. this.texCodes[x + (y * this.size.x)] = code;
  184. }
  185. }
  186. }
  187. Map.prototype.setLayAt = function(x, y, name) {
  188. if(x < 0 || y < 0 || x > this.size.x || y > this.size.y) return null;
  189. var old = this.layout[x + (y * this.size.x)];
  190. this.layout[x + (y * this.size.x)] = this.layoutCodes[name];
  191. return old;
  192. }
  193. Map.prototype.orLayAt = function(x, y, name) {
  194. if(x < 0 || y < 0 || x > this.size.x || y > this.size.y) return null;
  195. var old = this.layout[x + (y * this.size.x)];
  196. this.layout[x + (y * this.size.x)] = old | this.layoutCodes[name];
  197. return old;
  198. }
  199. Map.prototype.fillRectLay = function(x1, y1, x2, y2, name) {
  200. var xmin = max(min(x1, x2), 0);
  201. var xmax = min(max(x1, x2), this.size.x);
  202. var ymin = max(min(y1, y2), 0);
  203. var ymax = min(max(y1, y2), this.size.y);
  204. var code = this.layoutCodes[name];
  205. for(var y = ymin; y <= ymax; y++) {
  206. for(var x = xmin; x <= xmax; x++) {
  207. this.layout[x + (y * this.size.x)] = code;
  208. }
  209. }
  210. }
  211. // these set bits
  212. Map.prototype.setInfoAt = function(x, y, mask, op) {
  213. if(x < 0 || y < 0 || x > this.size.x || y > this.size.y) return null;
  214. if(op == '|') // sets bits
  215. this.tileInfo[x + (y * this.size.x)] |= mask;
  216. else if(op == '&') // clears all other bits
  217. this.tileInfo[x + (y * this.size.x)] &= mask;
  218. else if(op == '!') // clears selected bits
  219. this.tileInfo[x + (y * this.size.x)] &= !mask;
  220. else if(op == '^') // why not?
  221. this.tileInfo[x + (y * this.size.x)] ^= mask;
  222. return true;
  223. }
  224. Map.prototype.fillRectInfo = function(x1, y1, x2, y2, mask, op) {
  225. var xmin = max(min(x1, x2), 0);
  226. var xmax = min(max(x1, x2), this.size.x);
  227. var ymin = max(min(y1, y2), 0);
  228. var ymax = min(max(y1, y2), this.size.y);
  229. for(var y = ymin; y <= ymax; y++) {
  230. for(var x = xmin; x <= xmax; x++) {
  231. if(op == '|') // sets bits
  232. this.tileInfo[x + (y * this.size.x)] |= mask;
  233. else if(op == '&') // clears all other bits
  234. this.tileInfo[x + (y * this.size.x)] &= mask;
  235. else if(op == '!') // clears selected bits
  236. this.tileInfo[x + (y * this.size.x)] &= !mask;
  237. else if(op == '^') // why not?
  238. this.tileInfo[x + (y * this.size.x)] ^= mask;
  239. }
  240. }
  241. }