MCPcopy
hub / github.com/liyupi/yulegeyu / initGame

Function initGame

src/core/game.ts:69–194  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

67 * 游戏初始化
68 */
69 const initGame = () => {
70 console.log("initGame", gameConfig);
71
72 // 0. 设置父容器宽高
73 const levelBoardDom: any = document.getElementsByClassName("level-board");
74 levelBoardDom[0].style.width = widthUnit * boxWidthNum + "px";
75 levelBoardDom[0].style.height = heightUnit * boxHeightNum + "px";
76
77 // 1. 规划块数
78 // 块数单位(总块数必须是该值的倍数)
79 const blockNumUnit = gameConfig.composeNum * gameConfig.typeNum;
80 console.log("块数单位", blockNumUnit);
81
82 // 随机生成的总块数
83 const totalRandomBlockNum = gameConfig.randomBlocks.reduce((pre, curr) => {
84 return pre + curr;
85 }, 0);
86 console.log("随机生成的总块数", totalRandomBlockNum);
87
88 // 需要的最小块数
89 const minBlockNum =
90 gameConfig.levelNum * gameConfig.levelBlockNum + totalRandomBlockNum;
91 console.log("需要的最小块数", minBlockNum);
92
93 // 补齐到 blockNumUnit 的倍数
94 // e.g. minBlockNum = 14, blockNumUnit = 6, 补到 18
95 totalBlockNum = minBlockNum;
96 if (totalBlockNum % blockNumUnit !== 0) {
97 totalBlockNum =
98 (Math.floor(minBlockNum / blockNumUnit) + 1) * blockNumUnit;
99 }
100 console.log("总块数", totalBlockNum);
101
102 // 2. 初始化块,随机生成块的内容
103 // 保存所有块的数组
104 const animalBlocks: string[] = [];
105 // 需要用到的动物数组
106 const needAnimals = gameConfig.animals.slice(0, gameConfig.typeNum);
107 // 依次把块塞到数组里
108 for (let i = 0; i < totalBlockNum; i++) {
109 animalBlocks.push(needAnimals[i % gameConfig.typeNum]);
110 }
111 // 打乱数组
112 const randomAnimalBlocks = _.shuffle(animalBlocks);
113
114 // 初始化
115 const allBlocks: BlockType[] = [];
116 for (let i = 0; i < totalBlockNum; i++) {
117 const newBlock = {
118 id: i,
119 status: 0,
120 level: 0,
121 type: randomAnimalBlocks[i],
122 higherThanBlocks: [] as BlockType[],
123 lowerThanBlocks: [] as BlockType[],
124 } as BlockType;
125 allBlocks.push(newBlock);
126 }

Callers 1

doStartFunction · 0.85

Calls 1

genLevelBlockPosFunction · 0.85

Tested by

no test coverage detected