Ver Fonte

方式2 根据车难易度 carSeats排序

woso_javan há 4 meses atrás
pai
commit
2f75b97b11

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

@@ -99,8 +99,10 @@ export class CarCarColorsComponent extends Component {
         new_pin.setWorldPosition(pinWorldPos)
 
         role.removeFromParent()
+        role.destroy()
+
         tween(new_pin)
-            .to(0.5, {
+            .to(0.4, {
                 worldPosition: new Vec3(seatWorldPos.x, seatWorldPos.y, 0)
             })
             .call(() => {
@@ -111,6 +113,7 @@ export class CarCarColorsComponent extends Component {
 
                 new_pin.setParent(seat);
                 new_pin.setPosition(Vec3.ZERO);
+                new_pin.getComponent(PinComponent)!.pin_color = this.carColor;
                 EventDispatcher.instance.emit(GameEvent.EVENT_CHECK_ELEMENT_CHILDREN)
             })
             .start()
@@ -232,6 +235,20 @@ export class CarCarColorsComponent extends Component {
 
         return false
     }
+
+    //前方的碰撞体的数量
+    checkCollisionNum(): number {
+        const objs = GameUtil.getWorldPositionAsVec2(this.node);
+        const obje = GameUtil.calculateRayEnd(this.node, 1000);
+
+        // 射线检测
+        let results = PhysicsSystem2D.instance.raycast(objs, obje, ERaycast2DType.Closest);
+        const collider = results[0].collider;
+        //碰到车
+        if (collider.group == 1 << 1) {
+            return results.length
+        }
+    }
 }
 
 

+ 16 - 5
assets/module_movecar/Script/ElementAction.ts

@@ -5,6 +5,9 @@ import { HoleComponent } from './Components/HoleComponent';
 import { PinComponent } from './Components/PinComponent';
 import { GameEvent } from './Enum/GameEvent';
 import { ResourcePool } from './ResourcePool';
+import { CarColorsGlobalInstance } from './CarColorsGlobalInstance';
+import { UnitColorsSysterm } from './Systems/UnitColorsSysterm';
+import { CarCarColorsSysterm } from './Systems/CarCarColorsSysterm';
 
 const { ccclass, property } = _decorator;
 
@@ -75,14 +78,22 @@ export class ElementAction extends Component {
         //获取 hole
         this.node.children.forEach(element => {
             if (element.getComponent(HoleComponent)) {
-                // let new_pin = instantiate(ResourcePool.instance.get_prefab("pin"));
-                // this.node.addChild(new_pin);
-                // new_pin.setPosition(element.position);
-                // new_pin.getComponent(PinComponent).init_date(group_id, color_pin_arr.shift(), element.getComponent(HoleComponent));
+                const pin = element.getComponentInChildren(PinComponent)!;
+                if (pin) {
+                    // pin.init_date(group_id, color_pin_arr.shift(), element.getComponent(HoleComponent));
+                    const index = CarColorsGlobalInstance.instance.carSysterm.pinColorIndex++;
+                    pin.init_date(group_id, color_pin_arr[index], element.getComponent(HoleComponent));
+                }
+            }
+        });
+    }
 
+    public change_pin_color(color: CarColors) {
+        this.node.children.forEach(element => {
+            if (element.getComponent(HoleComponent)) {
                 const pin = element.getComponentInChildren(PinComponent)!;
                 if (pin) {
-                    pin.init_date(group_id, color_pin_arr.shift(), element.getComponent(HoleComponent));
+                    pin.pin_color = color;
                 }
             }
         });

+ 4 - 1
assets/module_movecar/Script/Enum/GameEvent.ts

@@ -22,7 +22,10 @@ export class GameEvent {
     /** 磁力事件*/
     static readonly EVENT_MAGNET = 'EVENT_MAGNET';
 
-    /** 颜色刷新事件*/
+    /** 刷新钉子颜色事件*/
+    static readonly EVENT_REFRESH_PIN_COLORS = 'EVENT_REFRESH_PIN_COLORS';
+
+    /** 按钮功能 颜色刷新事件*/
     static readonly EVENT_REFRESH_COLOR = 'EVENT_REFRESH_COLOR';
 
     /** 检测游戏是否结束*/

+ 7 - 1
assets/module_movecar/Script/LayerAction.ts

@@ -39,6 +39,12 @@ export class LayerAction extends Component {
         });
     }
 
+    public change_pin_color(color: CarColors) {
+        this.node.children.forEach(element_node => {
+            element_node.getComponent(ElementAction).change_pin_color(color);
+        });
+    }
+
     get_pin_color(arr: PinComponent[]) {
         this.node.children.forEach(element_node => {
             element_node.getComponent(ElementAction)!.get_pin_color(arr);
@@ -69,7 +75,7 @@ export class LayerAction extends Component {
                         element_node.getComponent(ElementAction)?.flash_img();
                     });
                 }
-                console.log('显示layer+++++++++++++')
+                // console.log('显示layer+++++++++++++')
                 break;
             case 2:
                 if (this.layer_status != 2) {

+ 8 - 2
assets/module_movecar/Script/LevelAction.ts

@@ -19,7 +19,7 @@ export class LevelAction extends Component {
     start() {
         this.registerListener();
         this.init_level();
-        this.schedule(this.moveToCar, 1);
+        this.schedule(this.moveToCar, 1.2);
     }
 
     registerListener() {
@@ -55,7 +55,9 @@ export class LevelAction extends Component {
             }
         });
 
+        CarColorsGlobalInstance.instance.carSysterm.sortCarSeatsByDifficulty();
         const color_pin_arr = CarColorsGlobalInstance.instance.carSysterm.carSeats;
+        console.log(color_pin_arr);
         this.node.children.forEach(unit_node => {
             if (unit_node.getComponent(UnitAction)) {
                 unit_node.getComponent(UnitAction)!.init_pin(color_pin_arr);
@@ -125,6 +127,7 @@ export class LevelAction extends Component {
             if (show_num <= default_show_layer_num) {
                 if (layer_action.layer_status != 1) {
                     layer_action.set_status(1);
+                    // EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_PIN_COLORS);
                 }
             } else if (show_num == (default_show_layer_num + 1)) {
                 if (layer_action.layer_status != 2) {
@@ -158,7 +161,7 @@ export class LevelAction extends Component {
     get_pin_color(): PinComponent[] {
         let arr: PinComponent[] = [];
         const units = this.node.getComponentsInChildren(UnitAction)!;
-        console.log('units.length:', units.length);
+        // console.log('units.length:', units.length);
         units.forEach((unit) => {
             unit.get_pin_color(arr);
         })
@@ -232,6 +235,9 @@ export class LevelAction extends Component {
 
                         // 匹配的车
                         if (selectedCar !== null) {
+                            const pin_color = pinCom.pin_color
+                            CarColorsGlobalInstance.instance.carSysterm.removeColorFromSeats(pin_color)
+
                             if (selectedCar.getComponent(CarCarColorsComponent).addRole(pinCom.node)) {
                                 selectedCar.setParent(find("Canvas/Scene/Levels"), true);
                             }

+ 74 - 0
assets/module_movecar/Script/Systems/CarCarColorsSysterm.ts

@@ -18,6 +18,8 @@ export class CarCarColorsSysterm extends Component {
 
     carBoxMap: Array<CarBoxComponent> = []
 
+    pinColorIndex: number = 0;
+
     protected start(): void {
         EventDispatcher.instance.on(GameEvent.EVENT_REFRESH_COLOR, this.refreshCarColor, this);
     }
@@ -56,6 +58,67 @@ export class CarCarColorsSysterm extends Component {
         }
     }
 
+    //根据难易度对 carSeats 进行排序
+    sortCarSeatsByDifficulty() {
+        // 生成车辆颜色与其对应的难易度评分映射
+        const carDifficultyMap: { color: CarColors; difficultyScore: number; seats: number }[] = [];
+
+        this.activeCar.forEach((node, uuid) => {
+            const carComponent = node.getComponent(CarCarColorsComponent);
+            if (!carComponent) return;
+
+            // 调用计算难易度的方法
+            const difficultyScore = this.calculateCarDifficulty(node);
+
+            // 获取车类型对应的座位数
+            let seats = 0;
+            if (carComponent.carType === CarTypes.Bus) {
+                seats = 8;
+            } else if (carComponent.carType === CarTypes.Minivan) {
+                seats = 6;
+            } else if (carComponent.carType === CarTypes.Sedan) {
+                seats = 4;
+            } else if (carComponent.carType === CarTypes.Single) {
+                seats = 1;
+            }
+
+            // 添加到映射数组
+            carDifficultyMap.push({
+                color: carComponent.carColor,
+                difficultyScore,
+                seats,
+            });
+        });
+
+        // 按难易度降序排序(难的排前面,易的排后面)
+        carDifficultyMap.sort((a, b) => b.difficultyScore - a.difficultyScore);
+
+        // 根据排序结果重新生成 carSeats 数组
+        this.carSeats = [];
+        carDifficultyMap.forEach((item) => {
+            for (let i = 0; i < item.seats; i++) {
+                this.carSeats.push(item.color);
+            }
+        });
+    }
+
+    // 计算车的难易度评分
+    calculateCarDifficulty(node: Node): number {
+        const carComponent = node.getComponent(CarCarColorsComponent);
+        if (!carComponent) return Infinity;
+
+        // 检测车是否被阻挡
+        const isBlocked = carComponent.checkCollision();
+
+        // 获取车前方障碍物数量
+        const obstacleCount = carComponent.checkCollisionNum();
+
+        // 计算最终难易度评分
+        // 难易度越低越容易:没有被阻挡则 -10,障碍物数量乘以权重 2,类型分值
+        const difficultyScore = (isBlocked ? 10 : -10) + obstacleCount * 2;
+        return difficultyScore;
+    }
+
     removeCar(node: Node) {
         this.activeCar.delete(node.uuid)
         // console.log(this.activeCar);
@@ -68,6 +131,17 @@ export class CarCarColorsSysterm extends Component {
 
         this.carSeats.length = 0
         this.carSeats = []
+
+        this.pinColorIndex = 0;
+    }
+
+    //移除被解锁的钉子颜色
+    removeColorFromSeats(color: CarColors) {
+        // console.log('被移除的钉子颜色:', CarColorLog[color]);
+        const index = this.carSeats.indexOf(color)
+        if (index > -1) {
+            this.carSeats.splice(index, 1)
+        }
     }
 
     refreshCarColor() {

+ 79 - 0
assets/module_movecar/Script/Systems/UnitColorsSysterm.ts

@@ -7,6 +7,8 @@ import { UnitAction } from "../UnitAction";
 import { EventDispatcher } from "db://assets/core_tgx/easy_ui_framework/EventDispatcher";
 import { GameEvent } from "../Enum/GameEvent";
 import { LevelAction } from "../LevelAction";
+import { LayerAction } from "../LayerAction";
+import { CarColors } from "../CarColorsGlobalTypes";
 const { ccclass, property } = _decorator;
 
 @ccclass('UnitColorsSysterm')
@@ -21,6 +23,7 @@ export class UnitColorsSysterm extends Component {
 
     registerEvent() {
         EventDispatcher.instance.on(GameEvent.EVENT_MAGNET, this.onMagnet, this);
+        // EventDispatcher.instance.on(GameEvent.EVENT_REFRESH_PIN_COLORS, this.changePinColor, this);
     }
 
     private onMagnet() {
@@ -85,6 +88,82 @@ export class UnitColorsSysterm extends Component {
         this.clearpinsCar();
     }
 
+    // changePinColor() {
+    //     let activeLayerPins: PinComponent[] = [];
+    //     const level = CarColorsGlobalInstance.instance.levels.children[0];
+    //     const layer_arr = level.getComponent(LevelAction)!.get_all_layer();
+
+    //     // 当前剩余的所有颜色保存的数组
+    //     const carSeats: CarColors[] = CarColorsGlobalInstance.instance.carSysterm.carSeats;
+    //     const findAllCars: CarColors[] = this.findCanOutCars();
+
+    //     // 获取显示层上所有钉子组件
+    //     for (const layer of layer_arr) {
+    //         if (layer.layer_status !== 1) continue; // 只检查当前显示的层级
+    //         const pins = layer.getComponentsInChildren(PinComponent)!;
+    //         activeLayerPins.push(...pins);
+    //     }
+
+    //     // 初始化一个颜色设置数组,优先从 findAllCars 取颜色
+    //     let colorPool: CarColors[] = [...findAllCars];
+
+    //     // 如果 findAllCars 的颜色不够,用 carSeats 的颜色补充,但排除 findAllCars 里已有的颜色
+    //     for (const seatColor of carSeats) {
+    //         if (!findAllCars.includes(seatColor)) {
+    //             colorPool.push(seatColor);
+    //         }
+    //     }
+
+    //     // 给 activeLayerPins 逐个分配颜色
+    //     activeLayerPins.forEach((pin, index) => {
+    //         if (index < colorPool.length) {
+    //             pin.pin_color = colorPool[index]; // 假设 PinComponent 有 setColor 方法
+    //         } else {
+    //             console.warn(`Not enough colors to assign to pin at index ${index}`);
+    //         }
+    //     });
+    // }
+
+    //返回所有可以出车的车辆和停车位车辆 所有车辆钉子数组
+    findCanOutCars() {
+        const canOutCar: CarColors[] = [];
+        const cars = find("Canvas/Scene/Levels").children[0].getComponentsInChildren(CarCarColorsComponent)!;
+        cars.forEach(car => {
+            const carComp = car.getComponent(CarCarColorsComponent);
+            const collider = carComp.checkCollision();
+
+            if (!collider) {
+                const remainSeat = carComp.getRemainSeat();
+                for (let i = 0; i < remainSeat; i++) {
+                    canOutCar.push(carComp.carColor);
+                }
+            }
+        });
+
+        const points = find("Canvas/Scene/Parkings").children
+        for (let i = points.length; i--;) {
+            if (points[i].name === "inuse" && points[i].children.length === 1) {
+                const carComp = points[i].children[0].getComponent(CarCarColorsComponent);
+                const remainSeat = carComp.getRemainSeat();
+                for (let i = 0; i < remainSeat; i++) {
+                    canOutCar.push(carComp.carColor);
+                }
+                continue
+            }
+
+            if (points[i].name === "inuse" && points[i].children.length === 2) {
+                const carComp = points[i].children[1].getComponent(CarCarColorsComponent);
+                const remainSeat = carComp.getRemainSeat();
+                for (let i = 0; i < remainSeat; i++) {
+                    canOutCar.push(carComp.carColor);
+                }
+                continue
+            }
+        }
+
+        return canOutCar;
+    }
+
     clearpinsCar() {
         this.selectedCar = null
         this.findPins = []

+ 7 - 7
assets/module_movecar/rooster_movecar.scene

@@ -102,7 +102,7 @@
     "_lpos": {
       "__type__": "cc.Vec3",
       "x": 380,
-      "y": 799.9999999999999,
+      "y": 800.0000000000001,
       "z": 0
     },
     "_lrot": {
@@ -187,7 +187,7 @@
     "_priority": 1073741824,
     "_fov": 45,
     "_fovAxis": 0,
-    "_orthoHeight": 799.9999999999999,
+    "_orthoHeight": 800.0000000000001,
     "_near": 1,
     "_far": 2000,
     "_color": {
@@ -283,7 +283,7 @@
     "_contentSize": {
       "__type__": "cc.Size",
       "width": 760,
-      "height": 1599.9999999999998
+      "height": 1600.0000000000002
     },
     "_anchorPoint": {
       "__type__": "cc.Vec2",
@@ -3081,7 +3081,7 @@
     "_lpos": {
       "__type__": "cc.Vec3",
       "x": 0,
-      "y": -550,
+      "y": -600,
       "z": 0
     },
     "_lrot": {
@@ -4637,7 +4637,7 @@
     "_lpos": {
       "__type__": "cc.Vec3",
       "x": 0,
-      "y": -700,
+      "y": -700.0000000000002,
       "z": 0
     },
     "_lrot": {
@@ -6324,7 +6324,7 @@
     "_contentSize": {
       "__type__": "cc.Size",
       "width": 760,
-      "height": 1599.9999999999998
+      "height": 1600.0000000000002
     },
     "_anchorPoint": {
       "__type__": "cc.Vec2",
@@ -6463,7 +6463,7 @@
     "_contentSize": {
       "__type__": "cc.Size",
       "width": 760,
-      "height": 1599.9999999999998
+      "height": 1600.0000000000002
     },
     "_anchorPoint": {
       "__type__": "cc.Vec2",

+ 2 - 2
settings/v2/packages/information.json

@@ -7,7 +7,7 @@
       "enable": true,
       "customSplash": {
         "complete": true,
-        "form": "https://creator-api.cocos.com/api/form/show?sid=1b5d5bf985437b6f957cb414ac09793a"
+        "form": "https://creator-api.cocos.com/api/form/show?sid=441f33883f4cc43ae3cef841ef990d52"
       }
     },
     "removeSplash": {
@@ -16,7 +16,7 @@
       "enable": true,
       "removeSplash": {
         "complete": true,
-        "form": "https://creator-api.cocos.com/api/form/show?sid=1b5d5bf985437b6f957cb414ac09793a"
+        "form": "https://creator-api.cocos.com/api/form/show?sid=441f33883f4cc43ae3cef841ef990d52"
       }
     }
   }