OriginCup.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { _decorator, CCInteger, Component, Enum, find, Node, sp, tween, UITransform, Vec3 } from 'cc';
  2. import { CupHeight, TakeGobletGlobalInstance, WaterColors } from '../TakeGobletGlobalInstance';
  3. import { LevelAction } from '../LevelAction';
  4. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  5. import { GameEvent } from '../Enum/GameEvent';
  6. import { Water } from './Water';
  7. import { OriginArea } from './OriginArea';
  8. import { LevelManager } from '../Manager/LevelMgr';
  9. const { ccclass, property, executeInEditMode } = _decorator;
  10. //原浆杯状态 默认 抬起 倒水
  11. export enum OriginCupState {
  12. Default,
  13. Up,
  14. PourWater
  15. }
  16. /** 原浆酒杯组件脚本*/
  17. @ccclass('OriginCup')
  18. @executeInEditMode
  19. export class OriginCup extends Component {
  20. @property(sp.Skeleton)
  21. cupSkeleton: sp.Skeleton = null!; //杯骨骼
  22. @property({ type: Enum(CupHeight), displayName: '杯高度' })
  23. cupHeight: CupHeight = CupHeight.Two
  24. @property(Node)
  25. waters: Node = null!; //水节点
  26. @property(Node)
  27. marks: Node = null!; //mark节点
  28. start() {
  29. this.cupSkeleton = this.node.getChildByName('Cup')?.getComponent(sp.Skeleton)!;
  30. this.node.on(Node.EventType.TOUCH_END, () => {
  31. EventDispatcher.instance.emit(GameEvent.EVENT_CLICK_ORIGIN_CUP, this);
  32. });
  33. }
  34. async moveToTargetPos(height: CupHeight, targetPos: Vec3, colors: WaterColors[]): Promise<void> {
  35. return new Promise((resolve) => {
  36. if (!this.node) return;
  37. console.log(`this.node.parent:${this.node.parent.name}`);
  38. const levelAction = this.node.parent?.parent?.getComponent(LevelAction)!;
  39. // 设置初始位置(屏幕左侧)
  40. const uiTransform = levelAction.node.getComponent(UITransform)!;
  41. this.node.setPosition(-uiTransform.width / 2, 0, 0);
  42. this.node.getComponent(OriginCup)!.cupHeight = height;
  43. levelAction.originArea?.addChild(this.node);
  44. // console.log(`spawnNewOriginCup 设置初始位置:${this.node.position}`);
  45. this.setupOriginCupColors(this.node, colors);
  46. // 记录新杯子的初始位置
  47. levelAction.originCupPositions.set(this.node.uuid, targetPos);
  48. // 移动动画到原位置
  49. tween(this.node)
  50. .to(0.5, { position: targetPos })
  51. .call(() => {
  52. const markCount = levelAction.originArea?.getComponent(OriginArea)?.getTotalMarkCount();
  53. console.log(`mark总数:${markCount}`);
  54. LevelManager.instance.levelModel.createQuestionCount = markCount;
  55. resolve();
  56. })
  57. .start();
  58. });
  59. }
  60. // 设置原浆杯颜色
  61. private setupOriginCupColors(cupNode: Node, colors: WaterColors[]) {
  62. const levelAction = this.node.parent?.parent?.getComponent(LevelAction)!;
  63. const originCup = cupNode.getComponent(OriginCup)!;
  64. const marks = cupNode.getChildByName('Marks')!;
  65. const waters = originCup.waters.children;
  66. // console.log(`新创建原浆杯,高度: ${originCup.cupHeight}`);
  67. const markCount = levelAction.originArea.getComponent(OriginArea)?.getTotalMarkCount();
  68. const waterCount = originCup.cupHeight;
  69. for (let i = 0; i < waterCount; i++) {
  70. const waterNode = waters[i];
  71. if (!waterNode) continue;
  72. const water = waterNode.getComponent(Water)!;
  73. const mark = TakeGobletGlobalInstance.instance.refreshQuestionWater(markCount);
  74. water.color = colors[Math.floor(Math.random() * colors.length)];
  75. waterNode.active = true;
  76. console.log(`i:${i} -- marks.children[i].name:${marks.children[i].name}`);
  77. if (mark) {
  78. marks.children[i].active = true;
  79. marks.children[i].getComponent(Water)!.color = WaterColors.Black;
  80. }
  81. }
  82. }
  83. //播放动画根据状态
  84. playAnimation(state: OriginCupState, index?: number) {
  85. switch (state) {
  86. case OriginCupState.Up:
  87. this.playUpAnimation();
  88. break;
  89. case OriginCupState.PourWater:
  90. this.playPourWaterAnimation(index);
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. playUpAnimation() {
  97. if (!this.cupSkeleton) return;
  98. this.cupSkeleton.setAnimation(0, 'pour_idle', false);
  99. this.waters.children.forEach(water => {
  100. water.getComponent(Water)?.playAnimation(OriginCupState.Up);
  101. });
  102. }
  103. playPourWaterAnimation(index: number) {
  104. if (!this.cupSkeleton) return;
  105. this.cupSkeleton.setAnimation(0, `pour_0${index}`, false);
  106. this.waters.children.forEach(water => {
  107. water.getComponent(Water)?.playAnimation(OriginCupState.PourWater, index);
  108. });
  109. }
  110. setMark(bool: boolean) {
  111. this.marks.active = bool;
  112. }
  113. destroyOriginCup() {
  114. const id = this.node.uuid;
  115. console.log('销毁原浆杯id : ', id);
  116. tween(this.node)
  117. .to(0.3, { scale: Vec3.ZERO })
  118. .call(() => {
  119. EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, id);
  120. this.node.destroy();
  121. })
  122. .start();
  123. }
  124. }