Quellcode durchsuchen

优化刷新逻辑 优先保证开出车子

woso_javan vor 4 Monaten
Ursprung
Commit
5c13ebfe4d

Datei-Diff unterdrückt, da er zu groß ist
+ 186 - 173
assets/module_movecar/Prefabs/CarColorsLevels/lvl_2.prefab


+ 1 - 1
assets/module_movecar/Script/Components/CarCarColorsComponent.ts

@@ -256,7 +256,7 @@ export class CarCarColorsComponent extends Component {
             const collider = results[0].collider;
             //碰到车
             if (collider.group == 1 << 1) {
-                console.log(`${CarColorLog[this.carColor]}色车 --> 前方障碍物有${results.length - 1}个`);
+                // console.log(`${CarColorLog[this.carColor]}色车 --> 前方障碍物有${results.length - 1}个`);
                 return results.length - 1
             }
         }

+ 1 - 0
assets/module_movecar/Script/LevelAction.ts

@@ -398,6 +398,7 @@ export class LevelAction extends Component {
 
                             // 颜色相同
                             if (pinCom.pin_color === carColor) {
+                                console.log('匹配成功');
                                 return true; // 找到匹配的钉子
                             }
                         }

+ 28 - 40
assets/module_movecar/Script/Systems/CarCarColorsSysterm.ts

@@ -148,6 +148,7 @@ export class CarCarColorsSysterm extends Component {
         // 找出外圈没被挡住的车子
         const canOutCar: CarCarColorsComponent[] = [];
         const cars = find("Canvas/Scene/Levels").children[0].getComponentsInChildren(CarCarColorsComponent)!;
+
         cars.forEach(car => {
             const carComp = car.getComponent(CarCarColorsComponent);
             const collider = carComp.checkCollision();
@@ -166,18 +167,21 @@ export class CarCarColorsSysterm extends Component {
             if (layer.layer_status == 1) {
                 layer.node.children.forEach((element) => {
                     const pins = element.getComponentsInChildren(PinComponent)!;
-                    pins.forEach(async (pin) => {
+                    pins.forEach((pin) => {
                         const pinCom = pin.getComponent(PinComponent)!;
                         if (!pinCom.isBlocked) {
-                            if (!topPins.has(pinCom.pin_color)) {
-                                topPins.add(pinCom.pin_color);
-                            }
+                            topPins.add(pinCom.pin_color);
                         }
                     });
                 });
             }
         });
 
+        if (topPins.size === 0) {
+            console.log("没有需要替换的颜色!");
+            return;
+        }
+
         // 按车类型分组
         const carTypes = new Map<CarTypes, CarCarColorsComponent[]>();
         cars.forEach(car => {
@@ -188,48 +192,32 @@ export class CarCarColorsSysterm extends Component {
             carTypes.get(carType)!.push(car);
         });
 
-        // 检查是否有同类型的车可以交换颜色
-        let hasSameTypeCars = false;
-        carTypes.forEach((carsOfType, type) => {
-            if (carsOfType.length > 1) {
-                hasSameTypeCars = true;
-            }
-        });
+        let replaced = false;
 
-        if (!hasSameTypeCars) {
-            console.log("没有同类型的车可以交换颜色!");
-            return; // 如果没有同类型的车,直接返回
-        }
+        // **遍历 canOutCar,优先用 topPins 颜色替换**
+        for (const car of canOutCar) {
+            const sameTypeCars = carTypes.get(car.carType);
+            if (!sameTypeCars || sameTypeCars.length < 2) continue; // 确保有同类型的车可以交换
 
-        // 在同类型的车之间交换颜色
-        carTypes.forEach((carsOfType, type) => {
-            if (carsOfType.length > 1) {
-                // 随机交换颜色
-                for (let i = 0; i < carsOfType.length; i++) {
-                    const j = Math.floor(Math.random() * carsOfType.length);
-                    const tempColor = carsOfType[i].carColor;
-                    carsOfType[i].carColor = carsOfType[j].carColor;
-                    carsOfType[j].carColor = tempColor;
-                }
+            // 查找同类型的车,颜色在 topPins 里
+            const swapCar = sameTypeCars.find(c => c !== car && topPins.has(c.carColor));
+            if (swapCar) {
+                // 交换颜色
+                const tempColor = car.carColor;
+                car.carColor = swapCar.carColor;
+                swapCar.carColor = tempColor;
+
+                topPins.delete(car.carColor); // 移除已经替换过的颜色
+                replaced = true;
             }
-        });
+        }
 
-        // 检查可开出的车中是否至少有一个车的颜色是待解锁的钉子颜色之一
-        // let hasValidColor = false;
-        // canOutCar.forEach(car => {
-        //     if (topPins.has(car.carColor)) {
-        //         hasValidColor = true;
-        //     }
-        // });
-
-        // if (!hasValidColor && canOutCar.length > 0) {
-        //     // 如果没有符合条件的车,将第一个可开出的车的颜色改为待解锁的钉子颜色之一
-        //     const firstCar = canOutCar[0];
-        //     const newColor = Array.from(topPins)[0]; // 取第一个待解锁的颜色
-        //     firstCar.carColor = newColor;
-        // }
+        if (!replaced) {
+            console.log("没有合适的车可以进行颜色交换!");
+        }
     }
 
+
     //找出所有可开出的车
     findCanOutCars() {
         const canOutCar: CarCarColorsComponent[] = [];

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.