|
@@ -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[] = [];
|