OriginCup.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. }
  93. }
  94. }
  95. // 设置原浆冰冻
  96. private setupFrozen() {
  97. const levelAction = this.node.parent?.parent?.getComponent(LevelAction)!;
  98. const freezeCount = levelAction.originArea.getComponent(OriginArea)?.getFrozenCupCount();
  99. const freeze = TakeGobletGlobalInstance.instance.refreshFreezeWater(freezeCount);
  100. this.freezeActive = freeze;
  101. }
  102. //解除冰冻
  103. unFreeze() {
  104. const nextIndex = this.freeze.children.filter(n => n.active).length - 1;
  105. console.log(`解冻的索引:${nextIndex}`);
  106. const freezeNode = this.freeze.children[nextIndex];
  107. freezeNode.active = false;
  108. const freezeCount = this.freeze.children.filter(n => n.active).length;
  109. if (freezeCount == 0) {
  110. this.freezeActive = false;
  111. }
  112. }
  113. //播放动画根据状态
  114. playAnimation(state: OriginCupState, index?: number) {
  115. switch (state) {
  116. case OriginCupState.Up:
  117. this.playUpAnimation();
  118. break;
  119. case OriginCupState.PourWater:
  120. this.playPourWaterAnimation(index);
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126. playUpAnimation() {
  127. if (!this.cupSkeleton) return;
  128. this.cupSkeleton.setAnimation(0, 'pour_idle', false);
  129. this.waters.children.forEach(water => {
  130. water.getComponent(Water)?.playAnimation(OriginCupState.Up);
  131. });
  132. }
  133. playPourWaterAnimation(index: number) {
  134. if (!this.cupSkeleton) return;
  135. this.cupSkeleton.setAnimation(0, `pour_0${index}`, false);
  136. this.waters.children.forEach(water => {
  137. water.getComponent(Water)?.playAnimation(OriginCupState.PourWater, index);
  138. });
  139. }
  140. setMark(bool: boolean) {
  141. this.marks.active = bool;
  142. }
  143. destroyOriginCup() {
  144. const id = this.node.uuid;
  145. console.log('销毁原浆杯id : ', id);
  146. tween(this.node)
  147. .to(0.3, { scale: Vec3.ZERO })
  148. .call(() => {
  149. EventDispatcher.instance.emit(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, id);
  150. this.node.destroy();
  151. })
  152. .start();
  153. }
  154. }