export default function createGroundData() { return [ { height: 272, // px blockNum: 6, // kuai gap: 0 //px }, { height: 272, blockNum: 6, gap: 100 }, { height: 272, blockNum: 6, gap: 200 }, { height: 272, blockNum: 4, gap: 300 }, { height: 272, blockNum: 2, gap: 200 }, { height: 272, blockNum: 6, gap: 100 }, { height: 272, blockNum: 4, gap: 200 }, { height: 272, blockNum: 3, gap: 200 }, { height: 272, blockNum: 6, gap: 300 } ]; } class GroundData { /** * * @param {object} options 生成地形的配置参数 * @param {number} options.groundNum 数量 * @param {array} options.gaps 间距范围 * @param {array} options.widths 宽度范围 * @param {array} options.heights 高度范围 */ constructor(options) { this.options = options; return this.initData(); } initData() { let totalWidth = 0; let { groundNum, gaps, widths, heights, stageHeight } = this.options; let groundData = []; while (groundNum > 0) { let gap = this.getRandomBetweenTwoNumbers(gaps[0], gaps[1]); let width = totalWidth === 0 ? 750 : this.getRandomBetweenTwoNumbers(widths[0], widths[1]); let height = this.getRandomBetweenTwoNumbers( heights[0], heights[1] ); groundData.push({ x: totalWidth, y: stageHeight - height, width, height }); totalWidth += gap + width; groundNum--; } return groundData; } getRandomBetweenTwoNumbers(start, end) { return Math.random() * (end - start) + start; } } // export default GroundData;