浏览代码

生成原浆杯 逻辑添加 记录初始位置

woso_javan 3 月之前
父节点
当前提交
0431f2e49c

+ 6 - 3
assets/module_take_goblet/Script/Component/OriginCup.ts

@@ -15,6 +15,9 @@ export class OriginCup extends Component {
     @property(Node)
     waters: Node = null!;  //水节点
 
+    @property({ type: Number, tooltip: "水层数量" })
+    waterLayers: number = 5; // 改为可配置项
+
     start() {
         this.node.on(Node.EventType.TOUCH_END, () => {
             EventDispatcher.instance.emit(GameEvent.EVENT_CLICK_ORIGIN_CUP, this);
@@ -26,12 +29,12 @@ export class OriginCup extends Component {
     }
 
     destroyOriginCup() {
-        // 播放消失动画
+        const originalPos = this.node.position.clone();
         tween(this.node)
-            .to(0.3, { scale: new Vec3(0, 0, 0) })
+            .to(0.3, { scale: Vec3.ZERO })
             .call(() => {
                 this.node.destroy();
-                EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED);
+                EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.node);
             })
             .start();
     }

+ 66 - 2
assets/module_take_goblet/Script/LevelAction.ts

@@ -27,6 +27,8 @@ export class LevelAction extends Component {
     @property(Node)
     goblets: Node = null!;  //原浆区
 
+    private originCupPositions = new Map<Node, Vec3>(); // 记录原浆杯初始位置
+
     start() {
         this.registerListener();
         this.generateInitialCups(); // 自动初始化
@@ -38,10 +40,14 @@ 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); // 传递被销毁的节点
+        }, this);
     }
 
     unregisterListener() {
         EventDispatcher.instance.off(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
+        EventDispatcher.instance.off(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.spawnNewOriginCup, this);
     }
 
     private async generateInitialCups() {
@@ -91,6 +97,7 @@ export class LevelAction extends Component {
         const outCups = this.outArea.getCups() as Node[];
         const waitCups = this.waitArea.getCups() as Node[];
 
+        // DOTO 取前7个杯子颜色后期修改
         const allCups = [...outCups, ...waitCups].slice(0, 7);
         const colors = allCups.map(cup => {
             const comp = cup.getComponent(CocktailCup);
@@ -109,6 +116,9 @@ export class LevelAction extends Component {
                     waterNode.active = true;
                 }
             }
+
+            // 在生成初始原浆杯时记录位置
+            this.originCupPositions.set(originCupNode, originCupNode.position.clone());
         });
     }
 
@@ -217,8 +227,6 @@ export class LevelAction extends Component {
                 .call(() => resolve())
                 .start();
         });
-
-        // 移除返回原位的逻辑
     }
 
     // 新增辅助方法
@@ -229,5 +237,61 @@ export class LevelAction extends Component {
             activeWaters[topIndex].active = false;
         }
     }
+
+    private async spawnNewOriginCup(destroyedCup: Node) {
+        // 获取被销毁杯子的初始位置
+        const targetPos = this.originCupPositions.get(destroyedCup) || Vec3.ZERO;
+
+        // 创建新原浆杯
+        const height = TakeGobletGlobalInstance.instance.generateOriginCupHeight();
+        const prefab = await TakeGobletGlobalInstance.instance.loadAsyncOriginCup(height);
+        const newCup = instantiate(prefab);
+
+        // 设置初始位置(屏幕左侧)
+        const uiTransform = this.node.getComponent(UITransform)!;
+        newCup.setPosition(-uiTransform.width / 2, 0, 0);
+        this.goblets.addChild(newCup);
+
+        // 记录新杯子的初始位置
+        this.originCupPositions.set(newCup, targetPos);
+
+        // 移动动画到原位置
+        tween(newCup)
+            .to(0.5, { position: targetPos })
+            .start();
+
+        // DOTO 获取颜色配置
+        const colors = this.getAvailableColors(5); // 获取前5个颜色
+        this.setupOriginCupColors(newCup, colors);
+    }
+
+    // 获取可用颜色
+    private getAvailableColors(count: number): WaterColors[] {
+        const outCups = this.outArea.getCups();
+        const waitCups = this.waitArea.getCups();
+        const allCups = [...outCups, ...waitCups].slice(0, count);
+
+        return allCups.map(cup => {
+            const comp = cup.getComponent(CocktailCup);
+            return comp ? comp.cupColor : WaterColors.Blue;
+        });
+    }
+
+    // 设置原浆杯颜色
+    private setupOriginCupColors(cupNode: Node, colors: WaterColors[]) {
+        const originCup = cupNode.getComponent(OriginCup)!;
+        const waters = originCup.waters.children;
+
+        // 暂时写死5层水
+        const waterCount = 5;
+        for (let i = 0; i < waterCount; i++) {
+            const waterNode = waters[i];
+            if (!waterNode) continue;
+
+            const water = waterNode.getComponent(Water)!;
+            water.color = colors[Math.floor(Math.random() * colors.length)];
+            waterNode.active = true;
+        }
+    }
 }
 

+ 28 - 0
assets/module_take_goblet/Script/TakeGobletGlobalInstance.ts

@@ -38,6 +38,34 @@ export class TakeGobletGlobalInstance {
         })
     }
 
+    /** 生成原浆杯高度*/
+    public generateOriginCupHeight(): CupHeight {
+        //DOTO 配置中获取概率
+        return CupHeight.Three;
+    }
+
+    /** 异步加载调酒杯预设*/
+    async loadAsyncOriginCup(height: CupHeight): Promise<Prefab> {
+        return new Promise((resolve, reject) => {
+            const bundle = assetManager.getBundle(resLoader.gameBundleName);
+            if (!bundle) {
+                console.error("module_nut is null!");
+                reject();
+            }
+
+            let numStr = 'Two';
+            if (height == CupHeight.Three) {
+                numStr = 'Three';
+            } else if (height == CupHeight.Four) {
+                numStr = 'Four';
+            }
+
+            resLoader.loadAsync(resLoader.gameBundleName, `Prefabs/Cup/Original/Original${numStr}`, Prefab).then((prefab: Prefab) => {
+                resolve(prefab);
+            })
+        })
+    }
+
     public levels: Node = null;
 
     // 初始化调酒杯配置