maputil.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. function Map() {
  2. this.blockSize = 64;
  3. this.blocks = {};
  4. this.paths = {
  5. roads: {}
  6. };
  7. return this;
  8. }
  9. Map.prototype.getBlockForPt = function(x, y) {
  10. return this.getBlock(Math.floor(x / this.blockSize), Math.floor(y / this.blockSize));
  11. };
  12. Map.prototype.getBlock = function(bx, by) {
  13. return this.blocks[parseInt(bx) + ":" + parseInt(by)];
  14. };
  15. Map.prototype.makeBlock = function(bx, by) {
  16. var t = {
  17. blockPos: {x: bx, y: by},
  18. tiles: new Uint8Array(this.blockSize * this.blockSize),
  19. type: new Uint8Array(this.blockSize * this.blockSize),
  20. tilesTexIndex: null,
  21. tilesDirty: true,
  22. };
  23. this.blocks[parseInt(bx) + ":" + parseInt(by)] = t;
  24. return t;
  25. };
  26. Map.prototype.forceBlockForPt = function(x, y) {
  27. var bx = Math.floor((x + .5) / 64);
  28. var by = Math.floor((y + .5) / 64);
  29. var t = this.getBlock(bx, by);
  30. if(t) return t;
  31. return this.makeBlock(bx, by);
  32. }
  33. Map.prototype.blocksInArea = function(a, b) {
  34. var out = [];
  35. var min = {
  36. x: Math.min(a.x, b.x),
  37. y: Math.min(a.y, b.y),
  38. };
  39. var max = {
  40. x: Math.max(a.x, b.x),
  41. y: Math.max(a.y, b.y),
  42. };
  43. for(var y = min.y; y <= max.y; y += this.blockSize) {
  44. for(var x = min.x; x <= max.x; x += this.blockSize) {
  45. var b = this.getBlockForPt(x, y);
  46. if(b) out.push(b);
  47. }
  48. }
  49. return out;
  50. }
  51. //////////////////////////////
  52. // Map Editing Functions
  53. // only axis-aligned
  54. Map.prototype.placeRoad = function(from, to, stripe) {
  55. var min = {
  56. x: Math.min(from.x, to.x),
  57. y: Math.min(from.y, to.y),
  58. };
  59. var max = {
  60. x: Math.max(from.x, to.x),
  61. y: Math.max(from.y, to.y),
  62. };
  63. var delta;
  64. if(from.x == to.x) delta = {x: 1, y: 0};
  65. else delta = {x: 0, y: 1};
  66. // console.log(min, max)
  67. // var perpdelta = {x: delta.y, y: delta.x};
  68. // var pos = min.x
  69. for(var y = min.y; y <= max.y; y++) {
  70. for(var x = min.x; x <= max.x; x++) {
  71. // console.log(x,y);
  72. this.placeStripe(stripe, {x:x, y:y}, delta);
  73. }
  74. }
  75. }
  76. Map.prototype.fillRandom = function(a, b, tiles) {
  77. var min = Math.min(a.x, b.x);
  78. var max = Math.max(a.x, b.x);
  79. var len = tiles.length - 1;
  80. for(var y = min; y <= max; y++) {
  81. for(var x = min; x <= max; x++) {
  82. this.set_(x, y, tiles[Math.floor((Math.random() * len) + 0.5)]);
  83. }
  84. }
  85. }
  86. Map.prototype.fill = function(a, b, tile) {
  87. var min = Math.min(a.x, b.x);
  88. var max = Math.max(a.x, b.x);
  89. for(var y = min; y <= max; y++) {
  90. for(var x = min; x <= max; x++) {
  91. this.set_(x, y, tile);
  92. }
  93. }
  94. }
  95. function pt(x, y) {
  96. return {x:x, y:y};
  97. }
  98. function vadd(a, b) {
  99. return {x: a.x + b.x, y: a.y + b.y};
  100. }
  101. Map.prototype.set_ = function(x, y, tile) {
  102. var b = this.forceBlockForPt(x, y);
  103. var lx = Math.floor((x % this.blockSize) + .5);
  104. var ly = Math.floor((y % this.blockSize) + .5);
  105. b.tiles[lx + (ly * this.blockSize)] = tile;
  106. }
  107. Map.prototype.set = function(pos, tile) {
  108. this.set_(pos.x, pos.y, tile);
  109. }
  110. Map.prototype.placeStripe = function(stripe, start, delta) {
  111. var pos = {x: start.x, y: start.y};
  112. for(var i = 0; i < stripe.length; i++) {
  113. this.set(pos, stripe[i]);
  114. pos.x += delta.x;
  115. pos.y += delta.y;
  116. }
  117. }