OriginCup.ts 7.7 KB

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