Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

202 rader
4.1 KiB

  1. module.exports = (scope, map) => {
  2. const { templates } = scope;
  3. templates.forEach((r, typeId) => {
  4. if (r.properties.mapping)
  5. return;
  6. r.typeId = typeId;
  7. let { noRotate = false, canFlipX = true, canFlipY = true } = r.properties;
  8. //Property values are strings. So we turn '1' and '0' into 1 and 0
  9. canFlipX = ~~canFlipX;
  10. canFlipY = ~~canFlipY;
  11. //Fix Polygons
  12. r.objects.forEach(o => {
  13. if (!o.fog)
  14. return;
  15. const newArea = o.area.map(p => {
  16. const [ px, py ] = p;
  17. const hpx = px - r.x;
  18. const hpy = py - r.y;
  19. return [hpx, hpy];
  20. });
  21. Object.assign(o, {
  22. x: o.x - r.x,
  23. y: o.y - r.y,
  24. area: newArea
  25. });
  26. });
  27. //FlipX Loop
  28. for (let i = 0; i < 2; i++) {
  29. if (i && !canFlipX)
  30. continue;
  31. //FlipY Loop
  32. for (let j = 0; j < 2; j++) {
  33. if (j && !canFlipY)
  34. continue;
  35. //Rotate Loop
  36. for (let k = 0; k < 2; k++) {
  37. if (k && noRotate)
  38. continue;
  39. if (i + j + k === 0)
  40. continue;
  41. let flipped = extend({
  42. flipX: !!i,
  43. flipY: !!j,
  44. rotate: !!k
  45. }, r);
  46. flipped.exits.forEach(e => {
  47. let direction = JSON.parse(e.properties.exit);
  48. if (flipped.flipX) {
  49. direction[0] *= -1;
  50. e.x = r.x + r.width - (e.x - r.x) - e.width;
  51. }
  52. if (flipped.flipY) {
  53. direction[1] *= -1;
  54. e.y = r.y + r.height - (e.y - r.y) - e.height;
  55. }
  56. if (flipped.rotate) {
  57. direction = [direction[1], direction[0]];
  58. let t = e.x;
  59. e.x = r.x + (e.y - r.y);
  60. e.y = r.y + (t - r.x);
  61. t = e.width;
  62. e.width = e.height;
  63. e.height = t;
  64. }
  65. e.properties.exit = JSON.stringify(direction);
  66. });
  67. flipped.objects.forEach(o => {
  68. if (!o.fog) {
  69. if (flipped.flipX)
  70. o.x = r.x + r.width - (o.x - r.x) - 1;
  71. if (flipped.flipY)
  72. o.y = r.y + r.height - (o.y - r.y) - 1;
  73. if (flipped.rotate) {
  74. let t = o.x;
  75. o.x = r.x + (o.y - r.y);
  76. o.y = r.y + (t - r.x);
  77. }
  78. } else {
  79. if (flipped.flipX) {
  80. const newArea = o.area.map(p => {
  81. const [ px, py ] = p;
  82. const hpx = r.width - px;
  83. return [hpx, py];
  84. });
  85. Object.assign(o, {
  86. area: newArea
  87. });
  88. }
  89. if (flipped.flipY) {
  90. const newArea = o.area.map(p => {
  91. const [ px, py ] = p;
  92. const hpy = r.height - py;
  93. return [px, hpy];
  94. });
  95. Object.assign(o, {
  96. area: newArea
  97. });
  98. }
  99. if (flipped.rotate) {
  100. const newArea = o.area.map(p => {
  101. const [ px, py ] = p;
  102. const t = px;
  103. const hpx = py;
  104. const hpy = t;
  105. return [hpx, hpy];
  106. });
  107. Object.assign(o, {
  108. area: newArea
  109. });
  110. }
  111. //Fix polygon bounds
  112. let lowX = r.width;
  113. let lowY = r.height;
  114. let highX = 0;
  115. let highY = 0;
  116. o.area.forEach(p => {
  117. const [ px, py ] = p;
  118. if (px < lowX)
  119. lowX = px;
  120. if (px > highX)
  121. highX = px;
  122. if (py < lowY)
  123. lowY = py;
  124. if (py > highY)
  125. highY = py;
  126. });
  127. o.x = lowX;
  128. o.y = lowY;
  129. o.width = highX - lowX;
  130. o.height = highY - lowY;
  131. }
  132. });
  133. if (flipped.rotate) {
  134. let t = flipped.width;
  135. flipped.width = flipped.height;
  136. flipped.height = t;
  137. }
  138. templates.push(flipped);
  139. }
  140. }
  141. }
  142. });
  143. templates.forEach(r => {
  144. let rotate = r.rotate;
  145. let w = rotate ? r.height : r.width;
  146. let h = rotate ? r.width : r.height;
  147. r.map = _.get2dArray(r.width, r.height);
  148. r.tiles = _.get2dArray(r.width, r.height);
  149. r.collisionMap = _.get2dArray(r.width, r.height);
  150. r.oldExits = extend([], r.exits);
  151. for (let i = 0; i < w; i++) {
  152. for (let j = 0; j < h; j++) {
  153. let ii = rotate ? j : i;
  154. let jj = rotate ? i : j;
  155. let x = r.flipX ? (r.x + w - i - 1) : (r.x + i);
  156. let y = r.flipY ? (r.y + h - j - 1) : (r.y + j);
  157. r.map[ii][jj] = map.oldMap[x][y];
  158. r.tiles[ii][jj] = map.oldLayers.tiles[x][y];
  159. r.collisionMap[ii][jj] = map.oldCollisionMap[x][y];
  160. }
  161. }
  162. });
  163. };