TempCup.ts 4.2 KB

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