Browse Source

修复重新加载 报错问题

woso_javan 3 months ago
parent
commit
16adeb636e

+ 26 - 16
assets/module_take_goblet/Script/Component/CocktailCup.ts

@@ -1,6 +1,8 @@
-import { _decorator, Color, Component, Enum, Node, Sprite } from 'cc';
+import { _decorator, Color, Component, Enum, Node, Sprite, tween, Vec3 } from 'cc';
 import { CupHeight, WaterColorHex, WaterColors } from '../TakeGobletGlobalInstance';
 import { Water } from './Water';
+import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
+import { GameEvent } from '../Enum/GameEvent';
 const { ccclass, property, executeInEditMode } = _decorator;
 
 /** 调酒杯组件脚本*/
@@ -42,20 +44,16 @@ export class CocktailCup extends Component {
     }
 
     // 添加水层
-    addLayer(color?: WaterColors) {
-        if (this.currentLayers >= this.cupHeight) return;
-
-        // 从下往上激活水层(假设waters子节点0是最底层)
-        const waterNode = this.waters.children[this.currentLayers];
-
+    addLayer(color: WaterColors) {
+        const nextIndex = this.waters.children.filter(n => n.active).length;
+        const waterNode = this.waters.children[nextIndex];
         if (waterNode) {
-            if (color) {
-                const water = waterNode.getComponent(Water);
-                if (water) water.color = color;
-            }
-
+            const water = waterNode.getComponent(Water)!;
+            water.color = color;
             waterNode.active = true;
-            this.currentLayers++;
+
+            // 添加后检查是否满杯
+            this.checkAndDestroy();
         }
     }
 
@@ -65,8 +63,20 @@ export class CocktailCup extends Component {
         this.currentLayers = 0;
     }
 
-    isFull(): boolean {
-        return this.currentLayers >= this.cupHeight;
+    get isFull(): boolean {
+        return this.waters.children.every(node => node.active);
+    }
+
+    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();
+        }
     }
 
     public get currentColor(): WaterColors {
@@ -74,7 +84,7 @@ export class CocktailCup extends Component {
     }
 
     public get remainingCapacity(): number {
-        return this.cupHeight - this.currentLayers;
+        return this.cupHeight - this.waters.children.filter(n => n.active).length;
     }
 }
 

+ 1 - 1
assets/module_take_goblet/Script/Component/OriginCup.ts

@@ -34,7 +34,7 @@ export class OriginCup extends Component {
             .to(0.3, { scale: Vec3.ZERO })
             .call(() => {
                 this.node.destroy();
-                EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.node);
+                EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.node.uuid);
             })
             .start();
     }

+ 3 - 0
assets/module_take_goblet/Script/Enum/GameEvent.ts

@@ -24,4 +24,7 @@ export class GameEvent {
 
     /** 原浆杯销毁*/
     static readonly EVENT_ORIGIN_CUP_DESTROYED = 'EVENT_ORIGIN_CUP_DESTROYED';
+
+    /** 调酒杯销毁*/
+    static readonly EVENT_COCKTAIL_CUP_DESTROYED = 'EVENT_COCKTAIL_CUP_DESTROYED';
 }

+ 51 - 11
assets/module_take_goblet/Script/LevelAction.ts

@@ -27,11 +27,11 @@ export class LevelAction extends Component {
     @property(Node)
     goblets: Node = null!;  //原浆区
 
-    private originCupPositions = new Map<Node, Vec3>(); // 记录原浆杯初始位置
+    private originCupPositions = new Map<string, Vec3>(); // 改用唯一ID记录
 
     start() {
         this.registerListener();
-        this.generateInitialCups(); // 自动初始化
+        this.generateInitialCups();
     }
 
     onDestroy() {
@@ -40,14 +40,19 @@ export class LevelAction extends Component {
 
     registerListener() {
         EventDispatcher.instance.on(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
-        EventDispatcher.instance.on(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, (cupNode: Node) => {
-            this.spawnNewOriginCup(cupNode); // 传递被销毁的节点
+        EventDispatcher.instance.on(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, (uuid: string) => {
+            const cupNode = find(uuid) as Node;
+            if (cupNode) {
+                this.spawnNewOriginCup(cupNode);
+            }
         }, this);
+        EventDispatcher.instance.on(GameEvent.EVENT_COCKTAIL_CUP_DESTROYED, this.handleCupDestroyed, this);
     }
 
     unregisterListener() {
         EventDispatcher.instance.off(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
         EventDispatcher.instance.off(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.spawnNewOriginCup, this);
+        EventDispatcher.instance.off(GameEvent.EVENT_COCKTAIL_CUP_DESTROYED, this.handleCupDestroyed, this);
     }
 
     private async generateInitialCups() {
@@ -118,7 +123,9 @@ export class LevelAction extends Component {
             }
 
             // 在生成初始原浆杯时记录位置
-            this.originCupPositions.set(originCupNode, originCupNode.position.clone());
+            const id = originCupNode.uuid; // 使用节点唯一ID
+            this.originCupPositions.set(id, originCupNode.position.clone());
+            // console.log('在生成初始原浆杯时记录位置: ', originCupNode.position);
         });
     }
 
@@ -131,12 +138,12 @@ export class LevelAction extends Component {
             .filter(({ comp }) =>
                 comp &&
                 comp.cupColor === color &&
-                !comp.isFull()
+                !comp.isFull
             );
 
         if (validCups.length === 0) return null;
 
-        const sorted = validCups.sort((a, b) => a.comp.remainingCapacity - b.comp.remainingCapacity);
+        const sorted = validCups.sort((a, b) => (a.comp.remainingCapacity ?? 0) - (b.comp.remainingCapacity ?? 0));
         return sorted[0];
     }
 
@@ -146,7 +153,7 @@ export class LevelAction extends Component {
         for (let i = originCup.waters.children.length - 1; i >= 0; i--) {
             const waterNode = originCup.waters.children[i];
             if (waterNode.active) {
-                const water = waterNode.getComponent(Water)!;
+                const water = waterNode.getComponent(Water);
                 colors.push(water.color);
             }
         }
@@ -159,7 +166,12 @@ export class LevelAction extends Component {
 
             // 调酒区未找到,查找暂存区
             if (!targetNode) {
-                const tempCup = this.tempCups.getComponent(TempCups)!.findAvailableTempCup();
+                const tempCupsComp = this.tempCups.getComponent(TempCups);
+                if (!tempCupsComp) {
+                    console.error('TempCups component not found!');
+                    continue;
+                }
+                const tempCup = tempCupsComp.findAvailableTempCup();
                 if (tempCup) {
                     targetNode = tempCup.node;
                     targetIsTemp = true;
@@ -240,7 +252,8 @@ export class LevelAction extends Component {
 
     private async spawnNewOriginCup(destroyedCup: Node) {
         // 获取被销毁杯子的初始位置
-        const targetPos = this.originCupPositions.get(destroyedCup) || Vec3.ZERO;
+        const id = destroyedCup.uuid;
+        const targetPos = this.originCupPositions.get(id) || Vec3.ZERO;
 
         // 创建新原浆杯
         const height = TakeGobletGlobalInstance.instance.generateOriginCupHeight();
@@ -253,7 +266,7 @@ export class LevelAction extends Component {
         this.goblets.addChild(newCup);
 
         // 记录新杯子的初始位置
-        this.originCupPositions.set(newCup, targetPos);
+        this.originCupPositions.set(newCup.uuid, targetPos);
 
         // 移动动画到原位置
         tween(newCup)
@@ -293,5 +306,32 @@ export class LevelAction extends Component {
             waterNode.active = true;
         }
     }
+
+    private handleCupDestroyed(destroyedCup: Node) {
+        // 从outArea移除被销毁的杯子
+        this.outArea.removeCup(destroyedCup);
+        // 补充新杯子到等待区
+        this.generateNewCupToWaitArea();
+    }
+
+    private async generateNewCupToWaitArea() {
+        const configs = TakeGobletGlobalInstance.instance.getInitialCupsConfig();
+        const randomConfig = configs[Math.floor(Math.random() * configs.length)];
+
+        const prefab = await TakeGobletGlobalInstance.instance.loadAsyncCocktail(randomConfig.height);
+        const newCup = instantiate(prefab);
+        const cupComp = newCup.getComponent(CocktailCup)!;
+
+        cupComp.cupColor = TakeGobletGlobalInstance.instance.getRandomColor();
+        cupComp.reset();
+
+        this.waitArea.addCup(newCup);
+    }
+
+    // 新增重置方法
+    public resetLevel() {
+        this.originCupPositions.clear();
+        this.generateInitialCups();
+    }
 }