TempCup.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { _decorator, color, Color, Component, Enum, Node, sp } from 'cc';
  2. import { CupHeight, WaterColorHex, WaterColors } from '../TakeGobletGlobalInstance';
  3. import { Water } from './Water';
  4. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  5. import { GameEvent } from '../Enum/GameEvent';
  6. import { GameUtil } from '../GameUtil';
  7. import { TakeGobletAudioMgr } from '../Manager/TakeGobletAudioMgr';
  8. const { ccclass, property } = _decorator;
  9. //暂存杯状态 默认 接水 倒水
  10. export enum TempCupState {
  11. Default,
  12. Pick,
  13. PourWater
  14. }
  15. @ccclass('TempCup')
  16. export class TempCup extends Component {
  17. @property({ type: Enum(CupHeight), displayName: '杯高度' })
  18. cupHeight: CupHeight = CupHeight.One
  19. @property(Node)
  20. waters: Node = null!; //水节点
  21. @property(sp.Skeleton)
  22. colorB: sp.Skeleton = null!; //水流骨骼
  23. @property(sp.Skeleton)
  24. cupSkeleton: sp.Skeleton = null!; //杯骨骼
  25. @property(Node)
  26. adNode: Node = null!;
  27. private _currentColor: WaterColors = WaterColors.Blue;
  28. private _isFull: boolean = false;
  29. get isFull(): boolean {
  30. return this._isFull;
  31. }
  32. get currentColor(): WaterColors {
  33. return this._currentColor;
  34. }
  35. get iconAd(): boolean {
  36. return this.adNode.active;
  37. }
  38. start() {
  39. this.node.on(Node.EventType.TOUCH_END, () => {
  40. this.onTouchEndAdCup();
  41. });
  42. }
  43. private onTouchEndAdCup() {
  44. if (this.iconAd) {
  45. this.adNode.active = false;
  46. console.log('关闭广告图标@@@@@');
  47. }
  48. }
  49. /**暂存杯 接水动画*/
  50. async fill(color: WaterColors) {
  51. if (this._isFull) return;
  52. const waterNode = this.waters.children[0];
  53. if (waterNode && waterNode.getComponent(sp.Skeleton)) {
  54. waterNode.active = true;
  55. this._currentColor = color;
  56. let cupColor = new Color(WaterColorHex[color]);
  57. let waterSpine = waterNode.getComponent(sp.Skeleton)!;
  58. // console.log(`暂存水的颜色:${WaterColorHex[color]}`)
  59. this.colorB.color = cupColor;
  60. waterSpine.color = cupColor;
  61. this.playAnimation(TempCupState.Pick);
  62. this._isFull = true;
  63. await GameUtil.delay(0.5);
  64. }
  65. }
  66. playAnimation(state: TempCupState) {
  67. let animaStr = state === TempCupState.Pick ? 'pick_01' :
  68. state === TempCupState.PourWater ? 'pour_01' : 'idle';
  69. const waterNode = this.waters.children[0];
  70. if (waterNode && waterNode.getComponent(sp.Skeleton)) {
  71. let waterSpine = waterNode.getComponent(sp.Skeleton)!;
  72. waterSpine.setAnimation(0, animaStr, false);
  73. this.colorB.setAnimation(0, animaStr, false);
  74. this.cupSkeleton.setAnimation(0, animaStr, false);
  75. }
  76. }
  77. //设置spine 时间缩放率
  78. setSpineTimeScale(timeScale: number = 1) {
  79. if (!this.node) return;
  80. const spineComponents = this.node.getComponentsInChildren(sp.Skeleton);
  81. spineComponents.forEach(spine => {
  82. spine.timeScale = timeScale;
  83. });
  84. }
  85. /**暂存杯 倒水动画*/
  86. pour() {
  87. TakeGobletAudioMgr.playOneShot(TakeGobletAudioMgr.getMusicIdName(4), 1.0);
  88. this.setSpineTimeScale(1.5);
  89. this.playAnimation(TempCupState.PourWater);
  90. }
  91. reset() {
  92. this._currentColor = WaterColors.White;
  93. let cupColor = new Color(WaterColorHex[this.currentColor]);
  94. let waterNode = this.waters.children[0];
  95. let waterSpine = waterNode.getComponent(sp.Skeleton)!;
  96. this.colorB.color = cupColor;
  97. waterSpine.color = cupColor;
  98. this._isFull = false;
  99. this.playAnimation(TempCupState.Default);
  100. }
  101. public getColors(): WaterColors[] {
  102. return [this.currentColor];
  103. }
  104. public isEmpty(): boolean {
  105. return this.waters.children.every(node => !node.active);
  106. }
  107. }