1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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;
|