OriginCup.ts 7.7 KB

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