OriginCup.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. import { tgxUITips } from 'db://assets/core_tgx/tgx';
  10. import { GameUtil } from '../GameUtil';
  11. const { ccclass, property, executeInEditMode } = _decorator;
  12. //原浆杯状态 默认 抬起 倒水
  13. export enum OriginCupState {
  14. Default,
  15. Up,
  16. PourWater
  17. }
  18. /** 原浆酒杯组件脚本*/
  19. @ccclass('OriginCup')
  20. @executeInEditMode
  21. export class OriginCup extends Component {
  22. @property(sp.Skeleton)
  23. cupSkeleton: sp.Skeleton = null!; //杯骨骼
  24. @property({ type: Enum(CupHeight), displayName: '杯高度' })
  25. cupHeight: CupHeight = CupHeight.Two
  26. @property(Node)
  27. waters: Node = null!; //水节点
  28. @property(Node)
  29. marks: Node = null!; //mark节点
  30. @property(Node)
  31. freeze: Node = null!; //冰冻节点
  32. get freezeActive() {
  33. return this.freeze.active;
  34. }
  35. set freezeActive(value: boolean) {
  36. this.freeze.active = value;
  37. }
  38. start() {
  39. this.cupSkeleton = this.node.getChildByName('Cup')?.getComponent(sp.Skeleton)!;
  40. this.node.on(Node.EventType.TOUCH_END, () => {
  41. if (this.freezeActive) {
  42. tgxUITips.show('冰冻中,无法操作!');
  43. return;
  44. }
  45. EventDispatcher.instance.emit(GameEvent.EVENT_CLICK_ORIGIN_CUP, this);
  46. });
  47. }
  48. async spawnNewOriginCup(height: CupHeight, targetPos: Vec3, colors: WaterColors[]): Promise<void> {
  49. return new Promise((resolve) => {
  50. if (!this.node) return;
  51. const levelAction = this.node.parent?.parent?.getComponent(LevelAction)!;
  52. // 设置初始位置(屏幕左侧)
  53. const uiTransform = levelAction.node.getComponent(UITransform)!;
  54. this.node.setPosition(-uiTransform.width / 2, 0, 0);
  55. this.node.getComponent(OriginCup)!.cupHeight = height;
  56. levelAction.originArea?.addChild(this.node);
  57. // console.log(`spawnNewOriginCup 设置初始位置:${this.node.position}`);
  58. this.setupOriginCupColors(this.node, colors);
  59. this.setupFrozen();
  60. // 记录新杯子的初始位置
  61. levelAction.originCupPositions.set(this.node.uuid, targetPos);
  62. // 移动动画到原位置
  63. tween(this.node)
  64. .to(0.5, { position: targetPos })
  65. .call(() => {
  66. const markCount = levelAction.originArea?.getComponent(OriginArea)?.getTotalMarkCount();
  67. console.log(`mark总数:${markCount}`);
  68. LevelManager.instance.levelModel.createQuestionCount = markCount;
  69. resolve();
  70. })
  71. .start();
  72. });
  73. }
  74. // 设置原浆杯颜色
  75. private setupOriginCupColors(cupNode: Node, colors: WaterColors[]) {
  76. const levelAction = this.node.parent?.parent?.getComponent(LevelAction)!;
  77. const originCup = cupNode.getComponent(OriginCup)!;
  78. const marks = cupNode.getChildByName('Marks')!;
  79. const waters = originCup.waters.children;
  80. // console.log(`新创建原浆杯,高度: ${originCup.cupHeight}`);
  81. const markCount = levelAction.originArea.getComponent(OriginArea)?.getTotalMarkCount();
  82. const waterCount = originCup.cupHeight;
  83. for (let i = 0; i < waterCount; i++) {
  84. const waterNode = waters[i];
  85. if (!waterNode) continue;
  86. const water = waterNode.getComponent(Water)!;
  87. const mark = TakeGobletGlobalInstance.instance.refreshQuestionWater(markCount);
  88. water.color = colors[Math.floor(Math.random() * colors.length)];
  89. waterNode.active = true;
  90. console.log(`i:${i} -- marks.children[i].name:${marks.children[i].name}`);
  91. if (mark) {
  92. marks.children[i].active = true;
  93. }
  94. }
  95. }
  96. // 设置原浆冰冻
  97. private setupFrozen() {
  98. const levelAction = this.node.parent?.parent?.getComponent(LevelAction)!;
  99. const freezeCount = levelAction.originArea.getComponent(OriginArea)?.getFrozenCupCount();
  100. const freeze = TakeGobletGlobalInstance.instance.refreshFreezeWater(freezeCount);
  101. this.freezeActive = freeze;
  102. }
  103. //解除冰冻
  104. unFreeze() {
  105. const nextIndex = this.freeze.children.filter(n => n.active).length - 1;
  106. console.log(`解冻的索引:${nextIndex}`);
  107. const freezeNode = this.freeze.children[nextIndex];
  108. freezeNode.active = false;
  109. const freezeCount = this.freeze.children.filter(n => n.active).length;
  110. if (freezeCount == 0) {
  111. this.freezeActive = false;
  112. }
  113. }
  114. fillWater() {
  115. const activeWaters = this.waters.children.filter(n => n.active);
  116. const topIndex = this.waters.children.length - activeWaters.length;
  117. if (activeWaters.length >= 0) {
  118. this.playAnimation(OriginCupState.PourWater, topIndex + 1);
  119. }
  120. }
  121. hideCurrentWaterLayer() {
  122. const activeWaters = this.waters.children.filter(n => n.active);
  123. if (activeWaters.length >= 0) {
  124. activeWaters[activeWaters.length - 1].active = false;
  125. }
  126. }
  127. //播放原浆杯倒水动画
  128. playAnimation(state: OriginCupState, index?: number) {
  129. switch (state) {
  130. case OriginCupState.Up:
  131. this.playUpAnimation();
  132. break;
  133. case OriginCupState.PourWater:
  134. this.playPourWaterAnimation(index);
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. playUpAnimation() {
  141. if (!this.cupSkeleton) return;
  142. this.cupSkeleton.setAnimation(0, 'pour_idle', false);
  143. this.waters.children.forEach(water => {
  144. water.getComponent(Water)?.playAnimation(OriginCupState.Up);
  145. });
  146. }
  147. playPourWaterAnimation(index: number) {
  148. if (!this.cupSkeleton) return;
  149. this.cupSkeleton.setAnimation(0, `pour_0${index}`, false);
  150. this.waters.children.forEach(water => {
  151. water.getComponent(Water)?.playAnimation(OriginCupState.PourWater, index);
  152. });
  153. }
  154. setMark(bool: boolean) {
  155. this.marks.active = bool;
  156. }
  157. destroyOriginCup() {
  158. const id = this.node.uuid;
  159. console.log('销毁原浆杯id : ', id);
  160. tween(this.node)
  161. .to(0.3, { scale: Vec3.ZERO })
  162. .call(() => {
  163. EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, id);
  164. this.node.destroy();
  165. })
  166. .start();
  167. }
  168. }