groudData.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. class GroundData {
  2. /**
  3. *
  4. * @param {object} options 生成地形的配置参数
  5. * @param {number} options.groundNum 数量
  6. * @param {array} options.gaps 间距范围
  7. * @param {array} options.widths 宽度范围
  8. * @param {array} options.heights 高度范围
  9. */
  10. constructor(options) {
  11. this.options = options;
  12. return this.initData();
  13. }
  14. initData() {
  15. let totalWidth = 0;
  16. let { groundNum, gaps, widths, heights, stageHeight } = this.options;
  17. let groundData = [];
  18. while (groundNum > 0) {
  19. let gap = this.getRandomBetweenTwoNumbers(gaps[0], gaps[1]);
  20. let width = totalWidth === 0 ? 750 : this.getRandomBetweenTwoNumbers(widths[0], widths[1]);
  21. let height = this.getRandomBetweenTwoNumbers(
  22. heights[0],
  23. heights[1]
  24. );
  25. groundData.push({
  26. x: totalWidth,
  27. y: stageHeight - height,
  28. width,
  29. height
  30. });
  31. totalWidth += gap + width;
  32. groundNum--;
  33. }
  34. return groundData
  35. }
  36. getRandomBetweenTwoNumbers(start, end) {
  37. return Math.random() * (end - start) + start;
  38. }
  39. }
  40. export default GroundData;