|
@@ -1,4 +1,4 @@
|
|
|
-import { _decorator, BoxCollider2D, Button, CircleCollider2D, Collider2D, Component, find, instantiate, Node, NodeEventType, view } from 'cc';
|
|
|
+import { _decorator, BoxCollider2D, Button, CircleCollider2D, Collider2D, Component, find, instantiate, Node, NodeEventType, tween, view, Vec3, mat4, UITransform } from 'cc';
|
|
|
import { resLoader, ResLoader } from '../../core_tgx/base/ResLoader';
|
|
|
import { CupHeight, TakeGobletGlobalInstance, WaterColors } from './TakeGobletGlobalInstance';
|
|
|
import { OutArea } from './Component/OutArea';
|
|
@@ -6,6 +6,8 @@ import { WaitArea } from './Component/WaitArea';
|
|
|
import { CocktailCup } from './Component/CocktailCup';
|
|
|
import { OriginCup } from './Component/OriginCup';
|
|
|
import { Water } from './Component/Water';
|
|
|
+import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
|
|
|
+import { GameEvent } from './Enum/GameEvent';
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
@ccclass('LevelAction')
|
|
@@ -28,7 +30,16 @@ export class LevelAction extends Component {
|
|
|
this.generateInitialCups(); // 自动初始化
|
|
|
}
|
|
|
|
|
|
+ onDestroy() {
|
|
|
+ this.unregisterListener();
|
|
|
+ }
|
|
|
+
|
|
|
registerListener() {
|
|
|
+ EventDispatcher.instance.on(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
|
|
|
+ }
|
|
|
+
|
|
|
+ unregisterListener() {
|
|
|
+ EventDispatcher.instance.off(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
|
|
|
}
|
|
|
|
|
|
private async generateInitialCups() {
|
|
@@ -86,11 +97,76 @@ export class LevelAction extends Component {
|
|
|
const waterNode = waters[i];
|
|
|
const water = waterNode.getComponent(Water);
|
|
|
if (water) {
|
|
|
- water.color = colors[Math.floor(Math.random() * colors.length)];
|
|
|
+ water.initColor(colors[Math.floor(Math.random() * colors.length)]);
|
|
|
waterNode.active = true;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ public async handlePourOriginCup(originCup: OriginCup) {
|
|
|
+ // 如果子节点0是底层,需要反转顺序
|
|
|
+ const colors: WaterColors[] = [];
|
|
|
+ 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)!;
|
|
|
+ colors.push(water.color);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理每个颜色层(从顶层开始)
|
|
|
+ for (const color of colors) {
|
|
|
+ const targetCup = this.findTargetCupInOutArea(color);
|
|
|
+ if (!targetCup) {
|
|
|
+ console.log(`未找到颜色为${WaterColors[color]}的调酒杯,停止倒水`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ await this.pourAnimation(originCup.node, targetCup.node, color);
|
|
|
+
|
|
|
+ // 隐藏当前处理的水层(顶层)
|
|
|
+ const activeWaters = originCup.waters.children.filter(n => n.active);
|
|
|
+ if (activeWaters.length > 0) {
|
|
|
+ const topIndex = activeWaters.length - 1;
|
|
|
+ activeWaters[topIndex].active = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private findTargetCupInOutArea(color: WaterColors): CocktailCup | null {
|
|
|
+ // 严格过滤调酒区杯子
|
|
|
+ const validCups = this.outArea.getCups()
|
|
|
+ .map(node => node.getComponent(CocktailCup))
|
|
|
+ .filter(cup =>
|
|
|
+ cup !== null &&
|
|
|
+ cup.cupColor === color &&
|
|
|
+ !cup.isFull()
|
|
|
+ ) as CocktailCup[];
|
|
|
+
|
|
|
+ // 优先选择剩余容量最小的杯子(后进先满)
|
|
|
+ return validCups.sort((a, b) => a.remainingCapacity - b.remainingCapacity)[0] || null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async pourAnimation(origin: Node, target: Node, color: WaterColors) {
|
|
|
+ // 使用UITransform转换坐标
|
|
|
+ const targetWorldPos = target.worldPosition;
|
|
|
+ const originParent = origin.parent!;
|
|
|
+ const localPos = originParent.getComponent(UITransform)!.convertToNodeSpaceAR(targetWorldPos);
|
|
|
+ localPos.y += 150; // Y轴偏移
|
|
|
+
|
|
|
+ await new Promise<void>(resolve => {
|
|
|
+ tween(origin)
|
|
|
+ .to(0.3, { position: localPos })
|
|
|
+ .call(() => resolve())
|
|
|
+ .start();
|
|
|
+ });
|
|
|
+
|
|
|
+ // 添加水层并设置颜色
|
|
|
+ const cocktailCup = target.getComponent(CocktailCup);
|
|
|
+ if (cocktailCup && !cocktailCup.isFull()) {
|
|
|
+ cocktailCup.addLayer(color);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|