groudData.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. export default function createGroundData() {
  2. return [
  3. {
  4. height: 272, // px
  5. blockNum: 6, // kuai
  6. gap: 0 //px
  7. },
  8. {
  9. height: 272,
  10. blockNum: 6,
  11. gap: 100
  12. },
  13. {
  14. height: 272,
  15. blockNum: 6,
  16. gap: 200
  17. },
  18. {
  19. height: 272,
  20. blockNum: 4,
  21. gap: 300
  22. },
  23. {
  24. height: 272,
  25. blockNum: 2,
  26. gap: 200
  27. },
  28. {
  29. height: 272,
  30. blockNum: 6,
  31. gap: 100
  32. },
  33. {
  34. height: 272,
  35. blockNum: 4,
  36. gap: 200
  37. },
  38. {
  39. height: 272,
  40. blockNum: 3,
  41. gap: 200
  42. },
  43. {
  44. height: 272,
  45. blockNum: 6,
  46. gap: 300
  47. }
  48. ];
  49. }
  50. class GroundData {
  51. /**
  52. *
  53. * @param {object} options 生成地形的配置参数
  54. * @param {number} options.groundNum 数量
  55. * @param {array} options.gaps 间距范围
  56. * @param {array} options.widths 宽度范围
  57. * @param {array} options.heights 高度范围
  58. */
  59. constructor(options) {
  60. this.options = options;
  61. return this.initData();
  62. }
  63. initData() {
  64. let totalWidth = 0;
  65. let { groundNum, gaps, widths, heights, stageHeight } = this.options;
  66. let groundData = [];
  67. while (groundNum > 0) {
  68. let gap = this.getRandomBetweenTwoNumbers(gaps[0], gaps[1]);
  69. let width =
  70. totalWidth === 0
  71. ? 750
  72. : this.getRandomBetweenTwoNumbers(widths[0], widths[1]);
  73. let height = this.getRandomBetweenTwoNumbers(
  74. heights[0],
  75. heights[1]
  76. );
  77. groundData.push({
  78. x: totalWidth,
  79. y: stageHeight - height,
  80. width,
  81. height
  82. });
  83. totalWidth += gap + width;
  84. groundNum--;
  85. }
  86. return groundData;
  87. }
  88. getRandomBetweenTwoNumbers(start, end) {
  89. return Math.random() * (end - start) + start;
  90. }
  91. }
  92. // export default GroundData;