123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { _decorator, CCInteger, Component, Enum, find, Node, sp, tween, UITransform, Vec3 } from 'cc';
- import { CupHeight, TakeGobletGlobalInstance, WaterColors } from '../TakeGobletGlobalInstance';
- import { LevelAction } from '../LevelAction';
- import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
- import { GameEvent } from '../Enum/GameEvent';
- import { Water } from './Water';
- import { OriginArea } from './OriginArea';
- import { LevelManager } from '../Manager/LevelMgr';
- const { ccclass, property, executeInEditMode } = _decorator;
- //原浆杯状态 默认 抬起 倒水
- export enum OriginCupState {
- Default,
- Up,
- PourWater
- }
- /** 原浆酒杯组件脚本*/
- @ccclass('OriginCup')
- @executeInEditMode
- export class OriginCup extends Component {
- @property(sp.Skeleton)
- cupSkeleton: sp.Skeleton = null!; //杯骨骼
- @property({ type: Enum(CupHeight), displayName: '杯高度' })
- cupHeight: CupHeight = CupHeight.Two
- @property(Node)
- waters: Node = null!; //水节点
- @property(Node)
- marks: Node = null!; //mark节点
- start() {
- this.cupSkeleton = this.node.getChildByName('Cup')?.getComponent(sp.Skeleton)!;
- this.node.on(Node.EventType.TOUCH_END, () => {
- EventDispatcher.instance.emit(GameEvent.EVENT_CLICK_ORIGIN_CUP, this);
- });
- }
- async moveToTargetPos(height: CupHeight, targetPos: Vec3, colors: WaterColors[]): Promise<void> {
- return new Promise((resolve) => {
- if (!this.node) return;
- console.log(`this.node.parent:${this.node.parent.name}`);
- const levelAction = this.node.parent?.parent?.getComponent(LevelAction)!;
- // 设置初始位置(屏幕左侧)
- const uiTransform = levelAction.node.getComponent(UITransform)!;
- this.node.setPosition(-uiTransform.width / 2, 0, 0);
- this.node.getComponent(OriginCup)!.cupHeight = height;
- levelAction.originArea?.addChild(this.node);
- // console.log(`spawnNewOriginCup 设置初始位置:${this.node.position}`);
- this.setupOriginCupColors(this.node, colors);
- // 记录新杯子的初始位置
- levelAction.originCupPositions.set(this.node.uuid, targetPos);
- // 移动动画到原位置
- tween(this.node)
- .to(0.5, { position: targetPos })
- .call(() => {
- const markCount = levelAction.originArea?.getComponent(OriginArea)?.getTotalMarkCount();
- console.log(`mark总数:${markCount}`);
- LevelManager.instance.levelModel.createQuestionCount = markCount;
- resolve();
- })
- .start();
- });
- }
- // 设置原浆杯颜色
- private setupOriginCupColors(cupNode: Node, colors: WaterColors[]) {
- const levelAction = this.node.parent?.parent?.getComponent(LevelAction)!;
- const originCup = cupNode.getComponent(OriginCup)!;
- const marks = cupNode.getChildByName('Marks')!;
- const waters = originCup.waters.children;
- // console.log(`新创建原浆杯,高度: ${originCup.cupHeight}`);
- const markCount = levelAction.originArea.getComponent(OriginArea)?.getTotalMarkCount();
- const waterCount = originCup.cupHeight;
- for (let i = 0; i < waterCount; i++) {
- const waterNode = waters[i];
- if (!waterNode) continue;
- const water = waterNode.getComponent(Water)!;
- const mark = TakeGobletGlobalInstance.instance.refreshQuestionWater(markCount);
- water.color = colors[Math.floor(Math.random() * colors.length)];
- waterNode.active = true;
- console.log(`i:${i} -- marks.children[i].name:${marks.children[i].name}`);
- if (mark) {
- marks.children[i].active = true;
- marks.children[i].getComponent(Water)!.color = WaterColors.Black;
- }
- }
- }
- //播放动画根据状态
- playAnimation(state: OriginCupState, index?: number) {
- switch (state) {
- case OriginCupState.Up:
- this.playUpAnimation();
- break;
- case OriginCupState.PourWater:
- this.playPourWaterAnimation(index);
- break;
- default:
- break;
- }
- }
- playUpAnimation() {
- if (!this.cupSkeleton) return;
- this.cupSkeleton.setAnimation(0, 'pour_idle', false);
- this.waters.children.forEach(water => {
- water.getComponent(Water)?.playAnimation(OriginCupState.Up);
- });
- }
- playPourWaterAnimation(index: number) {
- if (!this.cupSkeleton) return;
- this.cupSkeleton.setAnimation(0, `pour_0${index}`, false);
- this.waters.children.forEach(water => {
- water.getComponent(Water)?.playAnimation(OriginCupState.PourWater, index);
- });
- }
- setMark(bool: boolean) {
- this.marks.active = bool;
- }
- destroyOriginCup() {
- const id = this.node.uuid;
- console.log('销毁原浆杯id : ', id);
- tween(this.node)
- .to(0.3, { scale: Vec3.ZERO })
- .call(() => {
- EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, id);
- this.node.destroy();
- })
- .start();
- }
- }
|