OriginCup.ts 6.4 KB

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