Kaynağa Gözat

修改种类出现计算方式

woso_javan 3 ay önce
ebeveyn
işleme
5e7cb63b88
1 değiştirilmiş dosya ile 48 ekleme ve 7 silme
  1. 48 7
      assets/module_take_goblet/Script/LevelAction.ts

+ 48 - 7
assets/module_take_goblet/Script/LevelAction.ts

@@ -105,15 +105,56 @@ export class LevelAction extends Component {
         const rule = instance.cocktailCupRule();
         const [x, y] = rule;
         const colorPool = levelColors.slice(0, y);
-        console.log(`colorPool:`,colorPool)
+        console.log(`colorPool:`, colorPool);
 
-        // 为所有杯子分配颜色,并控制颜色种类不超过y种
-        allCups.forEach(cup => {
+        // 为所有杯子分配颜色
+        // 确保所有颜色至少出现一次
+        const shuffledCups = [...allCups];
+        const shuffledColors = [...levelColors];
+        let currentIndex = 0;
+
+        // 先为每个颜色分配一个杯子
+        for (let i = 0; i < shuffledColors.length && i < shuffledCups.length; i++) {
+            const cup = shuffledCups[i];
             const cupComp = cup.getComponent(CocktailCup)!;
-            // 从颜色池随机选择一个颜色
-            const randomColor = colorPool[Math.floor(Math.random() * colorPool.length)];
-            cupComp.cupColor = randomColor;
-        });
+            cupComp.cupColor = shuffledColors[i];
+            currentIndex = i + 1;
+        }
+
+        // 为剩余杯子分配颜色,确保不超过x个相同颜色相邻
+        for (let i = currentIndex; i < shuffledCups.length; i++) {
+            const cup = shuffledCups[i];
+            const cupComp = cup.getComponent(CocktailCup)!;
+
+            // 获取前x-1个杯子的颜色
+            const previousColors = new Set<WaterColors>();
+            for (let j = Math.max(0, i - (x - 1)); j < i; j++) {
+                const prevCup = shuffledCups[j].getComponent(CocktailCup)!;
+                previousColors.add(prevCup.cupColor);
+            }
+
+            // 从可用颜色中筛选出合适的颜色
+            const availableColors = shuffledColors.filter(color => !previousColors.has(color));
+
+            // 如果没有合适的颜色,就从所有颜色中随机选择
+            const colorPool = availableColors.length > 0 ? availableColors : shuffledColors;
+            cupComp.cupColor = colorPool[Math.floor(Math.random() * colorPool.length)];
+        }
+
+        // 随机打乱杯子顺序
+        for (let i = shuffledCups.length - 1; i > 0; i--) {
+            const j = Math.floor(Math.random() * (i + 1));
+            [shuffledCups[i], shuffledCups[j]] = [shuffledCups[j], shuffledCups[i]];
+        }
+
+        allCups.length = 0;
+        allCups.push(...shuffledCups);
+        // allCups.forEach(cup => {
+        //     const cupComp = cup.getComponent(CocktailCup)!;
+        //     // 从颜色池随机选择一个颜色
+        //     const randomColor = colorPool[Math.floor(Math.random() * colorPool.length)];
+        //     cupComp.cupColor = randomColor;
+        // });
 
         console.log('allCups: ', allCups.length);
         // 分配初始位置