MarshalSkillBar.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { _decorator, Component, Node, Sprite, Label, sp, SpriteFrame, Vec3, Animation } from 'cc';
  2. import { BattleData } from '../battle/BattleData';
  3. import { DataSystem } from '../core/data/DataSystem';
  4. import { Http } from '../core/net/Http';
  5. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  6. import { CountDown } from '../core/ui/CountDown';
  7. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  8. import { WindowSystem } from '../core/ui/window/WindowSystem';
  9. import { Utils } from '../core/utils/Utils';
  10. import { ConfigData } from '../data/ConfigData';
  11. import { UserData } from '../data/UserData';
  12. import { Guide, GuideType } from '../guide/Guide';
  13. import { FormationData } from '../hero/formation/FormationData';
  14. import { MarshalData } from '../marshal/MarshalData';
  15. const { ccclass, property } = _decorator;
  16. @ccclass('MarshalSkillBar')
  17. export class MarshalSkillBar extends Component {
  18. @property({ type: Http, displayName: "Http组件", tooltip: "Http组件" }) http: Http = null;
  19. @property({ type: ResourceLoader, displayName: "资源加载组件", tooltip: "资源加载组件" }) res: ResourceLoader = null;
  20. @property({ type: Node, displayName: "统帅技能节点", tooltip: "统帅技能节点" }) nodeMarshal: Node = null;
  21. @property({ type: Sprite, displayName: "技能头像", tooltip: "技能头像" }) imgSkill: Sprite = null;
  22. @property({ type: Label, displayName: "技能名称", tooltip: "技能名称" }) txtName: Label = null;
  23. @property({ type: Node, displayName: "冷却节点", tooltip: "冷却节点" }) nodeCooling: Node = null;
  24. @property({ type: Sprite, displayName: "等级进度图", tooltip: "等级进度图" }) progressLv: Sprite = null;
  25. @property({ type: Node, displayName: "倒计时指针节点", tooltip: "倒计时指针节点" }) progressNeedle: Node = null;
  26. @property({ type: Label, displayName: "文本冷却时间", tooltip: "文本冷却时间" }) txtTimer: Label = null;
  27. @property({ type: CountDown, displayName: "倒计时组件", tooltip: "倒计时组件" }) countDown: CountDown = null;
  28. @property({ type: sp.Skeleton, displayName: "冷却完成动画", tooltip: "冷却完成动画" }) spineCool: sp.Skeleton = null;
  29. @property({ type: Node, displayName: "气泡", tooltip: "气泡" }) nodeTip: Node = null;
  30. private cfg: any;
  31. private remaindTime = 0;
  32. private isCanClick = false;
  33. onLoad() {
  34. this.cfg = DataSystem.getData(ConfigData).get("serverConfig");
  35. }
  36. async start() {
  37. this.updateMarshalSkillCool();
  38. }
  39. private async updateMarshalSkillCool() {
  40. let formationData = DataSystem.getData(FormationData);
  41. let unlockLevel = parseInt(DataSystem.getData(ConfigData).get("serverConfig").embattle_open.split(",")[4])
  42. let isUnlock = DataSystem.getData(UserData).level >= unlockLevel;
  43. let isExistMarshal = isUnlock && formationData.has(5) && formationData.get(5) != null && formationData.get(5) > 0;
  44. this.nodeMarshal.active = isExistMarshal;
  45. this.nodeTip.active = isUnlock && formationData.has(5) && formationData.get(5) == null;
  46. !isExistMarshal && this.nodeTip.getComponent(Animation).play();
  47. if (!isExistMarshal) return;
  48. let marshalData = DataSystem.getData(MarshalData);
  49. if (!marshalData.marshalSkillData) await marshalData.init();
  50. if (marshalData.defaultId <= 0) return;
  51. let date = new Date();
  52. let detlaTime = date.getTime() - marshalData.skillCoolingTime;
  53. if (detlaTime >= this.cfg.skillCD * 1000) {
  54. this.remaindTime = 0;
  55. } else {
  56. this.remaindTime = this.cfg.skillCD - detlaTime * 0.001;
  57. }
  58. this.txtTimer.string = this.remaindTime > 0 ? Utils.formatCountDown(this.remaindTime * 1000) : "";
  59. this.remaindTime == 0 && this.countDown.stop();
  60. if (!this.countDown.running && this.remaindTime) { this.countDown.value = this.remaindTime; this.countDown.play(); }
  61. let skillData = DataSystem.getData(ConfigData).get("skill")[marshalData.defaultId.toString()];
  62. //let skillData = marshalData.skillData[marshalData.defaultId];
  63. this.progressLv.fillRange = marshalData.marshalSkillData.lvMarshalSkill.get(marshalData.defaultId) / 3;
  64. this.txtName.string = skillData.name;
  65. this.nodeCooling.active = this.remaindTime > 0;
  66. this.progressNeedle.angle = 360 / this.cfg.skillCD * this.remaindTime;
  67. this.isCanClick = this.remaindTime == 0;
  68. this.imgSkill.spriteFrame = await this.res.load<SpriteFrame>("images/skill/" + skillData.icon + "/spriteFrame", SpriteFrame);
  69. }
  70. update() {
  71. this.watchFormation(5);
  72. this.watchDefaultSkill();
  73. this.watchDefaultSkillCd();
  74. }
  75. watchFormation(formationID: number) {
  76. if (DataSystem.watch(FormationData, formationID)) {
  77. this.updateMarshalSkillCool();
  78. }
  79. }
  80. watchDefaultSkill() {
  81. DataSystem.watch(MarshalData, "defaultId") && this.updateDefaultSkill();
  82. }
  83. watchDefaultSkillCd() {
  84. if (DataSystem.watch(MarshalData, "skillCoolingTime")) {
  85. this.updateMarshalSkillCool();
  86. this.remaindTime == 0 && DataSystem.getData(FormationData).get(5) > 0 && this.playAni();
  87. }
  88. }
  89. /**更新默认技能*/
  90. async updateDefaultSkill() {
  91. let marshalData = DataSystem.getData(MarshalData);
  92. let skillData = DataSystem.getData(ConfigData).get("skill")[marshalData.defaultId.toString()];
  93. //let skillData = marshalData.skillData[marshalData.defaultId];
  94. this.imgSkill.spriteFrame = await this.res.load<SpriteFrame>("images/skill/" + skillData.icon + "/spriteFrame", SpriteFrame);
  95. this.progressLv.fillRange = marshalData.marshalSkillData.lvMarshalSkill.get(marshalData.defaultId) / 3;
  96. this.txtName.string = skillData.name;
  97. }
  98. public updateTxtTime(progress) {
  99. this.txtTimer.string = Utils.formatCountDown(progress * 1000);
  100. this.progressNeedle.angle = 360 / this.cfg.skillCD * progress;
  101. }
  102. public finishCooling() {
  103. this.remaindTime = 0;
  104. this.nodeCooling.active = false;
  105. this.isCanClick = true;
  106. this.txtTimer.string = "";
  107. this.playAni();
  108. }
  109. private async playAni() {
  110. this.spineCool.node.setScale(Vec3.ONE);
  111. this.spineCool.setAnimation(0, "animation", false);
  112. await new Promise<void>(resolve => { setTimeout(() => resolve(), (this.spineCool.findAnimation("animation").duration + 0.1) * 1000) });
  113. this.spineCool.node.setScale(Vec3.ZERO);
  114. }
  115. private async onClickSkill() {
  116. Guide.close(GuideType.Marshal);
  117. if (!this.isCanClick) {
  118. WindowSystem.open("prefabs/ui/marshalStage/marshalSkillSwitch", WindowOpenMode.NotCloseAndAdd);
  119. return;
  120. }
  121. let marshalData = DataSystem.getData(MarshalData);
  122. marshalData.skillCoolingTime = (new Date()).getTime();
  123. localStorage.setItem("MarshalCoolTime", marshalData.skillCoolingTime.toString());
  124. this.updateMarshalSkillCool();
  125. //技能效果 TODO
  126. let skillId = marshalData.defaultId - 110000;
  127. DataSystem.getData(BattleData).useLeaderSkill = { id: skillId, lv: marshalData.marshalSkillData.lvMarshalSkill.get(marshalData.defaultId) };
  128. let result = await this.http.send("/api/task/AddCommanderSkillTimes");
  129. console.log("addCommander: " + result.code);
  130. }
  131. private onClickSwitchSkill() {
  132. WindowSystem.open("prefabs/ui/marshalStage/marshalSkillSwitch", WindowOpenMode.NotCloseAndAdd);
  133. }
  134. }