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;