123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- 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';
- 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')
- export class LevelAction extends Component {
- @property(OutArea)
- outArea: OutArea = null!; // 直接引用OutArea组件
- @property(WaitArea)
- waitArea: WaitArea = null!; // 直接引用WaitArea组件
- @property(Node)
- tempCups: Node = null!; //临时杯
- @property(Node)
- goblets: Node = null!; //原浆区
- start() {
- this.registerListener();
- 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() {
- const instance = TakeGobletGlobalInstance.instance;
- const configs = instance.getInitialCupsConfig();
- const allCups: Node[] = [];
- for (const config of configs) {
- for (let i = 0; i < config.count; i++) {
- const prefab = await instance.loadAsyncCocktail(config.height);
- const cupNode = instantiate(prefab);
- const cup = cupNode.getComponent(CocktailCup)!;
- // 设置颜色并重置水位
- cup.cupColor = instance.getRandomColor();
- cup.reset();
- allCups.push(cupNode);
- }
- }
- console.log('allCups: ', allCups.length);
- // 分配初始位置
- allCups.slice(0, 2).forEach(cup => this.outArea.addCup(cup));
- allCups.slice(2).forEach(cup => this.waitArea.addCup(cup));
- // 在分配完调酒杯后添加
- this.generateOriginCups();
- }
- // 当调酒区杯子被消除时调用
- public async handleCupsRemoved(count: number) {
- for (let i = 0; i < count; i++) {
- const cup = this.waitArea.takeCup();
- if (cup) {
- this.outArea.addCup(cup);
- }
- }
- }
- private generateOriginCups() {
- const outCups = this.outArea.getCups() as Node[];
- const waitCups = this.waitArea.getCups() as Node[];
- const allCups = [...outCups, ...waitCups].slice(0, 7);
- const colors = allCups.map(cup => {
- const comp = cup.getComponent(CocktailCup);
- return comp ? comp.cupColor : WaterColors.Blue;
- });
- this.goblets.children.forEach(originCupNode => {
- const originCup = originCupNode.getComponent(OriginCup)!;
- const waters = originCup.waters.children;
- for (let i = 0; i < originCup.cupHeight; i++) {
- const waterNode = waters[i];
- const water = waterNode.getComponent(Water);
- if (water) {
- 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);
- }
- }
- }
|