RecruitItem.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { _decorator, Component, Node, Sprite, Label, sp, Vec3, Tween, SpriteFrame, Animation, isValid, EventHandler } from 'cc';
  2. import { ResourceLoader } from '../../core/resourceManager/ResourceLoader';
  3. import { Sound } from '../../core/sound/Sound';
  4. import { UITween } from '../../core/ui/tween/UITween';
  5. import { Hero_Client } from '../../hero/UserHeroData';
  6. const { ccclass, property } = _decorator;
  7. /**
  8. * 招募武将卡牌项item
  9. * @author 郑聂华
  10. */
  11. @ccclass('RecruitItem')
  12. export class RecruitItem extends Component {
  13. @property({ type: ResourceLoader, displayName: "资源加载组件", tooltip: "资源加载组件" }) res: ResourceLoader = null;
  14. @property({ type: Node, displayName: "动画震动节点", tooltip: "动画震动节点" }) animNode: Node = null;
  15. @property({ type: Node, displayName: "按钮_开奖", tooltip: "按钮_开奖" }) btnOpen: Node = null;
  16. @property({ type: Node, displayName: "已打开页面", tooltip: "已打开页面" }) nodeOpened: Node = null;
  17. @property({ type: Sprite, displayName: "图_武将", tooltip: "已打开页面" }) imgHero: Sprite = null;
  18. @property({ type: Sprite, displayName: "图_阵营", tooltip: "图_阵营" }) imgCamp: Sprite = null;
  19. @property({ type: Sprite, displayName: "图_背光单抽", tooltip: "图_背光单抽" }) imgLightOne: Sprite = null;
  20. @property({ type: Sprite, displayName: "图_背光十连抽橙红", tooltip: "图_背光十连抽橙红" }) imgLightTenOR: Sprite = null;
  21. @property({ type: Sprite, displayName: "图_背光开启紫", tooltip: "图_背光开启紫" }) imgLightOpenedP: Sprite = null;
  22. @property({ type: Sprite, displayName: "图_背光开启橙红", tooltip: "图_背光开启橙红" }) imgLightOpenedOr: Sprite = null;
  23. @property({ type: Sprite, displayName: "图_武将背景", tooltip: "图_武将背景" }) imgBgHero: Sprite = null;
  24. @property({ type: Sprite, displayName: "图_品质框", tooltip: "图_品质框" }) imgRect: Sprite = null;
  25. @property({ type: Sprite, displayName: "图_名称背板", tooltip: "图_名称背板" }) imgBgName: Sprite = null;
  26. @property({ type: Label, displayName: "文本_名称", tooltip: "文本_名称" }) txtName: Label = null;
  27. @property({ type: [Sprite], displayName: "星级", tooltip: "星级" }) stars: Sprite[] = [];
  28. @property({ type: Node, displayName: "提示_已拥有节点", tooltip: "提示_已拥有节点" }) nodeHasHeroNode: Node = null;
  29. @property({ type: Label, displayName: "文本_碎片数", tooltip: "文本_碎片数" }) txtSlice: Label = null;
  30. @property({ type: sp.Skeleton, displayName: "动画_紫色烟雾", tooltip: "动画_紫色烟雾" }) spinePurpleSmoke: sp.Skeleton = null;
  31. @property({ type: sp.Skeleton, displayName: "动画_蓄能", tooltip: "动画_蓄能" }) spineEnerygy: sp.Skeleton = null;
  32. @property({ type: sp.Skeleton, displayName: "动画_爆炸", tooltip: "动画_爆炸" }) spineBoom: sp.Skeleton = null;
  33. @property({ type: sp.Skeleton, displayName: "动画_星星", tooltip: "动画_星星" }) spineStar: sp.Skeleton = null;
  34. @property({ type: UITween, displayName: "动画_拎起", tooltip: "动画_拎起" }) tweenGetUp: UITween = null;
  35. @property({ type: UITween, displayName: "动画_开启节点", tooltip: "动画_开启节点" }) tweenBtnOpen: UITween = null;
  36. @property({ type: UITween, displayName: "动画_开启后节点", tooltip: "动画_开启后节点" }) tweenNodeOpened: UITween = null;
  37. @property({ type: Animation, displayName: "动画_震动", tooltip: "动画_震动" }) animShock: Animation = null;
  38. @property({ type: Sound, displayName: "音效组件", tooltip: "音效组件" }) sound: Sound = null;
  39. private initPos: Vec3;
  40. public recruitEndBack: EventHandler;
  41. public isOpened: boolean = false;
  42. public isRecruitEnd: boolean = false;
  43. /**抽卡类型 1 单抽 2 十连抽*/
  44. private drawType: number = 1;
  45. /**武将品质 1 蓝 2 紫 3 橙 4 红 依次增高 2 紫色烟雾动画 34 蓄力 爆炸 星星动画*/
  46. private color: number = 1;
  47. /**是否为碎片*/
  48. private isSlice: boolean = false;
  49. /**开启全部参数 isOpenAll 是否为一次性开启全部 isFirst 所有卡的第一张 isSameFirst 同品种卡的第一张*/
  50. private openParam: { isOpenAll: boolean, isFirst: boolean, isSameFirst: boolean };
  51. onLoad() {
  52. this.initPos = new Vec3(this.node.position.x, this.node.position.y, this.node.position.z);
  53. this.init();
  54. }
  55. /**
  56. * 初始化
  57. * @param type 抽卡类型 1 单抽 2 十连抽
  58. */
  59. init(type: number = 2) {
  60. this.isOpened = this.isRecruitEnd = false;
  61. Tween.stopAllByTarget(this.node);
  62. Tween.stopAllByTarget(this.btnOpen);
  63. Tween.stopAllByTarget(this.nodeOpened);
  64. this.node.setPosition(this.initPos);
  65. this.animNode.setPosition(0, 0);
  66. this.btnOpen.setScale(Vec3.ONE);
  67. this.nodeOpened.setScale(new Vec3(0, 1, 1));
  68. this.imgCamp.node.active = false;
  69. this.nodeHasHeroNode.active = false;
  70. this.imgLightOne.node.active = type == 1;
  71. this.imgLightTenOR.node.active = false;
  72. this.spinePurpleSmoke.node.setScale(new Vec3(0, 0, 1));//.active = false;
  73. this.spineEnerygy.node.setScale(new Vec3(0, 0, 1));//.node.active = false;
  74. this.spineBoom.node.setScale(new Vec3(0, 0, 1));//.scale= .active = false;
  75. this.spineStar.node.setScale(new Vec3(0, 0, 1));//.active = false;
  76. }
  77. /**
  78. * 抽卡
  79. * @param hero 武将
  80. * @param type 抽卡类型 1 单抽 2 十连抽
  81. * @param isSlice 是否是碎片
  82. * @param num 碎片数量
  83. * @param openParam 开启全部参数
  84. */
  85. async openDraw(hero: Hero_Client, type: number, isSlice: boolean, num: number = 0, openParam = { isOpenAll: false, isFirst: false, isSameFirst: false }) {
  86. if (this.isOpened) return; this.isOpened = true;
  87. this.drawType = type; this.color = hero.color; this.isSlice = isSlice; this.openParam = openParam; //this.sliceNum = num;
  88. this.imgLightOne.node.active = type == 1;
  89. this.imgLightTenOR.node.active = type == 2 && hero.color > 2;
  90. this.imgLightOpenedP.node.active = hero.color == 2;
  91. this.imgLightOpenedOr.node.active = hero.color > 2;
  92. this.txtName.string = hero.name;
  93. this.txtSlice.string = num + "";
  94. for (let i = 0; i < this.stars.length; i++) { this.stars[i].grayscale = hero.initStar < i + 1 }
  95. this.imgHero.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/hero_img/" + hero.hero_res + "/spriteFrame", SpriteFrame);
  96. this.imgCamp.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/icon_camp_" + hero.camp + "/spriteFrame", SpriteFrame);
  97. this.imgRect.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/kapai_kuang" + hero.color + "/spriteFrame", SpriteFrame);
  98. this.imgBgHero.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/kapi_bg" + hero.color + "/spriteFrame", SpriteFrame);
  99. this.imgBgName.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/kapai_name" + hero.color + "/spriteFrame", SpriteFrame);
  100. this.playOpenAni();
  101. }
  102. /**播放打开动画*/
  103. private playOpenAni() {
  104. //判断是否为橙红 需要有个蓄力Tween;
  105. if (this.color == 1 || this.color == 2) { this.tweenBtnOpen.play(); if (!this.openParam.isOpenAll || this.openParam.isFirst) this.scheduleOnce(() => { this.sound.play(0) }, 0.5); }
  106. else { this.tweenGetUp.play(); }
  107. }
  108. private async playSpinePurple() {
  109. let scale = this.drawType == 1 ? 1.5 : 1;
  110. this.spinePurpleSmoke.node.setScale(new Vec3(scale, scale, 1));//active = true;
  111. this.spinePurpleSmoke.setAnimation(0, "animation", false);
  112. await new Promise<void>(resolve => { setTimeout(() => resolve(), (this.spinePurpleSmoke.findAnimation("animation").duration + 0.1) * 1000) });
  113. isValid(this.node) && (this.spinePurpleSmoke.node.setScale(new Vec3(0, 0, 1)))//;active = false)
  114. }
  115. /**蓄力spine*/
  116. private async playSpineEnerygy() {
  117. let scale = this.drawType == 1 ? 2.731 : 1;
  118. this.spineEnerygy.node.setScale(new Vec3(scale, scale, 1));//.active = true;
  119. this.spineEnerygy.setAnimation(0, "animation", true);
  120. await new Promise<void>(resolve => { setTimeout(() => resolve(), (this.spineEnerygy.findAnimation("animation").duration + 0.1) * 1000) });
  121. }
  122. /**爆炸spine*/
  123. private playSpineBoom() {
  124. let scale = this.drawType == 1 ? 1.5 : 1;
  125. this.spineEnerygy.clearTrack(0);
  126. this.spineEnerygy.node.setScale(new Vec3(0, 0, 1));//.active = false;
  127. this.spineBoom.node.setScale(new Vec3(scale, scale, 1));//.active = true;
  128. this.spineBoom.setAnimation(0, "animation", false);
  129. if (!this.openParam.isOpenAll || this.openParam.isSameFirst) this.sound.play(5);
  130. }
  131. /**星星spine*/
  132. private async playSpineStar() {
  133. let scale = this.drawType == 1 ? 2.731 : 1;
  134. this.spineStar.node.setScale(new Vec3(scale, scale, 1));//.active = true;
  135. this.spineStar.setAnimation(0, "animation", false);
  136. await new Promise<void>(resolve => { setTimeout(() => resolve(), (this.spineStar.findAnimation("animation").duration + 0.1) * 1000) });
  137. if (!isValid(this.node)) return; this.spineStar.node.active = false;
  138. }
  139. private onCompleteNodeShock() { //震动后
  140. this.animShock.stop();
  141. this.animNode.setPosition(0, 0);
  142. this.playSpineBoom();
  143. this.tweenBtnOpen.play();
  144. if (!this.openParam.isOpenAll || this.openParam.isFirst) this.scheduleOnce(() => { this.sound.play(0) }, 0.5);
  145. }
  146. private onCompleteNodeSetUp() { //拎起后
  147. this.animShock.play();
  148. this.playSpineEnerygy();
  149. this.scheduleOnce(this.onCompleteNodeShock, 1.5);
  150. if (!this.openParam.isOpenAll || this.openParam.isSameFirst) this.sound.play(4);
  151. }
  152. /**卡牌关闭动画完成*/
  153. private onCompleteBtnOpen() {
  154. this.nodeOpened.setScale(new Vec3(0, 1, 1));
  155. this.nodeOpened.active = true;
  156. this.tweenNodeOpened.play();
  157. this.color == 2 && this.playSpinePurple();
  158. if (!this.openParam.isOpenAll || this.openParam.isSameFirst) {
  159. if (this.color == 1 || this.color == 2) { if (!this.isSlice) this.sound.play(1); }
  160. else { if (!this.isSlice) this.sound.play(2); }
  161. }
  162. }
  163. /**卡牌展开动画完成*/
  164. private onCompleteNodeOpened() {
  165. this.imgCamp.node.active = true;
  166. this.nodeHasHeroNode.active = this.isSlice;
  167. this.color > 2 && this.playSpineStar();
  168. this.isRecruitEnd = true;
  169. this.recruitEndBack.emit(null);
  170. if (!this.openParam.isOpenAll || this.openParam.isSameFirst)
  171. this.isSlice && this.sound.play(3);
  172. }
  173. }