Browse Source

fixed 等待至调酒 (排序问题待处理)

woso_javan 3 months ago
parent
commit
31d6cc1e1f

+ 19 - 10
assets/module_take_goblet/Script/Component/CocktailCup.ts

@@ -3,6 +3,7 @@ import { CupHeight, WaterColorHex, WaterColors } from '../TakeGobletGlobalInstan
 import { Water } from './Water';
 import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
 import { GameEvent } from '../Enum/GameEvent';
+import { OutArea } from './OutArea';
 const { ccclass, property, executeInEditMode } = _decorator;
 
 /** 调酒杯组件脚本*/
@@ -44,7 +45,7 @@ export class CocktailCup extends Component {
     }
 
     // 添加水层
-    addLayer(color: WaterColors) {
+    async addLayer(color: WaterColors) {
         const nextIndex = this.waters.children.filter(n => n.active).length;
         const waterNode = this.waters.children[nextIndex];
         if (waterNode) {
@@ -53,7 +54,7 @@ export class CocktailCup extends Component {
             waterNode.active = true;
 
             // 添加后检查是否满杯
-            this.checkAndDestroy();
+            await this.checkAndDestroy(); // 等待销毁流程完成
         }
     }
 
@@ -67,15 +68,23 @@ export class CocktailCup extends Component {
         return this.waters.children.every(node => node.active);
     }
 
-    checkAndDestroy() {
+    private async checkAndDestroy() {
         if (this.isFull) {
-            tween(this.node)
-                .to(0.3, { scale: Vec3.ZERO })
-                .call(() => {
-                    this.node.destroy();
-                    EventDispatcher.instance.emit(GameEvent.EVENT_COCKTAIL_CUP_DESTROYED, this.node);
-                })
-                .start();
+            await new Promise<void>(resolve => {
+                tween(this.node)
+                    .to(0.3, { scale: Vec3.ZERO })
+                    .call(async () => {
+                        // 先触发排列再销毁
+                        const outArea = this.node.parent?.parent?.getComponent(OutArea);
+                        if (outArea) {
+                            this.node.removeFromParent(); // 先从父节点移除
+                            await outArea.arrangeCups();   // 等待排列完成
+                        }
+                        this.node.destroy();               // 最后销毁节点
+                        resolve();
+                    })
+                    .start();
+            });
         }
     }
 

+ 4 - 2
assets/module_take_goblet/Script/Component/OriginCup.ts

@@ -29,14 +29,16 @@ export class OriginCup extends Component {
     }
 
     destroyOriginCup() {
-        const originalPos = this.node.position.clone();
+        // 在销毁前记录位置
+        const id = this.node.uuid;
         tween(this.node)
             .to(0.3, { scale: Vec3.ZERO })
             .call(() => {
+                EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, id);
                 this.node.destroy();
-                EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.node.uuid);
             })
             .start();
+
     }
 }
 

+ 21 - 20
assets/module_take_goblet/Script/Component/OutArea.ts

@@ -21,12 +21,28 @@ export class OutArea extends Component {
 
     }
 
-    // 排列杯子
-    arrangeCups() {
-        const startX = 40; //水杯的宽度一半
+    // 修改排列方法为异步
+    async arrangeCups() {
+        const startX = 40;
         const spacing = 80;
-        this.cups.forEach((cup, index) => {
-            cup.setPosition(startX + index * spacing, 0, 0);
+
+        // 过滤掉已销毁的节点
+        const validCups = this.cups.filter(cup => cup.isValid);
+
+        await new Promise<void>(resolve => {
+            let completed = 0;
+            validCups.forEach((cup, index) => {
+                const targetX = startX + index * spacing;
+                tween(cup)
+                    .to(0.3, { position: new Vec3(targetX, 0, 0) }, { easing: 'sineOut' })
+                    .call(() => {
+                        if (++completed === validCups.length) resolve();
+                    })
+                    .start();
+            });
+
+            // 处理无动画的情况
+            if (validCups.length === 0) resolve();
         });
     }
 
@@ -43,21 +59,6 @@ export class OutArea extends Component {
     getCups() {
         return this.cups;
     }
-
-    // 添加带动画排列方法
-    arrangeCupsWithAnimation() {
-        const startX = -200; // 从左侧开始
-        const spacing = 120; // 正间距向右排列
-
-        this.cups.forEach((cup, index) => {
-            const targetPos = new Vec3(startX + index * spacing, 0, 0);
-            if (!cup.position.equals(targetPos)) {
-                tween(cup)
-                    .to(0.3, { position: targetPos }, { easing: 'sineOut' })
-                    .start();
-            }
-        });
-    }
 }
 
 

+ 5 - 18
assets/module_take_goblet/Script/LevelAction.ts

@@ -197,7 +197,7 @@ export class LevelAction extends Component {
                 tempCupComp.fill(color);
             } else {
                 const cocktailCup = targetNode.getComponent(CocktailCup)!;
-                cocktailCup.addLayer(color);
+                await cocktailCup.addLayer(color); // 等待添加水层流程完成
             }
 
             this.hideCurrentWaterLayer(originCup);
@@ -217,21 +217,17 @@ export class LevelAction extends Component {
 
     // 添加等待区杯子到调酒区
     private async addWaitCupToOutArea() {
-        // 获取等待区杯子(保持原始顺序)
+        // 原有添加逻辑保持不变
         const waitCups = this.waitArea.cups;
         const outCups = this.outArea.getCups();
-
-        // 根据调酒区状态决定添加数量
         const needAddCount = outCups.length === 0 ? 2 : 1;
 
-        // 获取需要移动的杯子(从右往左取)
         const movingCups: Node[] = [];
         for (let i = 0; i < needAddCount; i++) {
             if (waitCups.length === 0) break;
             movingCups.push(waitCups.pop()!);
         }
 
-        // 创建新杯子并设置位置
         const newCups: Node[] = [];
         for (const cup of movingCups) {
             const comp = cup.getComponent(CocktailCup)!;
@@ -239,32 +235,23 @@ export class LevelAction extends Component {
             const newCup = instantiate(prefab);
             const newComp = newCup.getComponent(CocktailCup)!;
 
-            // 保持原属性
             newComp.cupColor = comp.cupColor;
             newComp.reset();
 
-            // 设置初始位置
             const targetX = outCups.length === 0 ?
                 (newCups.length === 0 ? -125 : -45) :
                 -45;
             newCup.setPosition(new Vec3(targetX, 0, 0));
 
             newCups.push(newCup);
-            cup.destroy(); // 销毁原杯子
+            cup.destroy();
         }
 
-        // 添加新杯子到调酒区
         const outNodes = this.outArea.node.getChildByName('OutNodes')!;
         newCups.forEach(cup => cup.setParent(outNodes));
 
-        // 所有杯子向右平移80
-        this.outArea.getCups().forEach(cup => {
-            tween(cup)
-                .by(0.3, { position: new Vec3(80, 0, 0) }, { easing: 'sineOut' })
-                .start();
-        });
-
-        this.waitArea.getCups().forEach(cup => {
+        // 执行统一平移
+        this.outArea.getCups().concat(this.waitArea.getCups()).forEach(cup => {
             tween(cup)
                 .by(0.3, { position: new Vec3(80, 0, 0) }, { easing: 'sineOut' })
                 .start();