You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tile.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. export default class Tile {
  2. constructor(x, y, name) {
  3. this.x = x;
  4. this.y = y;
  5. this.name = name;
  6. }
  7. }
  8. function realNames(base, override = {}) {
  9. return {
  10. topLeft: override.topLeft || base+"-top-l",
  11. top: override.top || base+"-top",
  12. topRight: override.topRight || base+"-top-r",
  13. topBoth: override.topBoth || base+"-top-lr",
  14. left: override.left || base+"-l",
  15. mid: override.mid || base,
  16. right: override.right || base+"-r",
  17. both: override.both || base+"-lr",
  18. bottomLeft: override.bottomLeft || base+"-bottom-l",
  19. bottom: override.bottom || base+"-bottom",
  20. bottomRight: override.bottomRight || base+"-bottom-r",
  21. bottomBoth: override.bottomBoth || base+"-bottom-lr",
  22. };
  23. }
  24. function getName(x, y, size, names) {
  25. let name = null;
  26. if (size.x <= 1) {
  27. if (y === 0)
  28. name = names.topBoth;
  29. else if (y === size.y - 1)
  30. name = names.bottomBoth;
  31. else
  32. name = names.both;
  33. } else {
  34. if (y === 0) {
  35. if (x === 0)
  36. name = names.topLeft;
  37. else if (x === size.x - 1)
  38. name = names.topRight;
  39. else
  40. name = names.top;
  41. } else if (y === size.y - 1) {
  42. if (x === 0)
  43. name = names.bottomLeft;
  44. else if (x === size.x - 1)
  45. name = names.bottomRight;
  46. else
  47. name = names.bottom;
  48. } else {
  49. if (x === 0)
  50. name = names.left;
  51. else if (x === size.x - 1)
  52. name = names.right;
  53. else
  54. name = names.mid;
  55. }
  56. }
  57. if (!name)
  58. name = "--invalid-"+x+"-"+y+"--";
  59. return name;
  60. }
  61. Tile.createBox = function(pos, size, base, names) {
  62. names = realNames(base, names);
  63. let arr = [];
  64. for (let x = 0; x < size.x; ++x) {
  65. for (let y = 0; y < size.y; ++y) {
  66. arr.push(new Tile(
  67. pos.x + x, pos.y + y, getName(x, y, size, names)));
  68. }
  69. }
  70. return arr;
  71. }
  72. function getNameLine(x, width, names) {
  73. if (width === 1)
  74. return names.both;
  75. else if (x === 0)
  76. return names.left;
  77. else if (x === width - 1)
  78. return names.right;
  79. else
  80. return names.mid;
  81. }
  82. Tile.createLine = function(pos, width, base, names) {
  83. names = realNames(base, names);
  84. let arr = [];
  85. for (let x = 0; x < width; ++x) {
  86. arr.push(new Tile(
  87. pos.x + x, pos.y, getNameLine(x, width, names)));
  88. }
  89. return arr;
  90. }