HOFItem.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { _decorator, Component, Sprite, SpriteFrame, Button } from 'cc';
  2. import { DataSystem } from '../../core/data/DataSystem';
  3. import { Http, HttpResponseCode } from '../../core/net/Http';
  4. import { ResourceLoader } from '../../core/resourceManager/ResourceLoader';
  5. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  6. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  7. import { ConfigData } from '../../data/ConfigData';
  8. import { PublicLogicData } from '../../main/PublicLogicData';
  9. import { FormationData } from '../formation/FormationData';
  10. import { UserHeroData } from '../UserHeroData';
  11. const { ccclass, property, requireComponent } = _decorator;
  12. /**
  13. * 名人堂项
  14. * @author 袁浩
  15. */
  16. @ccclass('HOFItem')
  17. @requireComponent(ResourceLoader)
  18. export class HOFItem extends Component {
  19. @property({ tooltip: "武将图片列表", type: [Sprite] })
  20. public heroSprites: Sprite[] = [];
  21. @property({ tooltip: "阵营图标", type: Sprite })
  22. public campIcon: Sprite;
  23. @property({ tooltip: "所有阵营图标", type: [SpriteFrame] })
  24. public campIconList: SpriteFrame[] = [];
  25. @property({ tooltip: "阵营背景", type: Sprite })
  26. public campBg: Sprite;
  27. @property({ tooltip: "所有阵营背景", type: [SpriteFrame] })
  28. public campBgList: SpriteFrame[] = [];
  29. @property({ tooltip: "一键上阵按钮", type: Button })
  30. public autoFormationBtn: Button;
  31. private canFormation = false;
  32. private formationObj: any;
  33. start() {
  34. // for (let i = 0; i < this.heroSprites.length; i++) {
  35. // this.heroSprites[i].spriteFrame = null;
  36. // }
  37. // this.campIcon.spriteFrame = null;
  38. // this.campBg.spriteFrame = null;
  39. }
  40. public async onDataChange(data: { camp: number, heros: number[] }, index?: number) {
  41. let userHeroData = DataSystem.getData(UserHeroData);
  42. let formationData = DataSystem.getData(FormationData);
  43. let canFormation = true;
  44. this.formationObj = {};
  45. let isExistAllHero = true;
  46. for (let i = 0; i < data.heros.length; i++) {
  47. let hero = DataSystem.getData(ConfigData).get("general")[data.heros[i]];
  48. let hasHero = !!userHeroData.get(hero.id).server && userHeroData.get(hero.id).server.active;
  49. isExistAllHero = isExistAllHero ? this.isExistHero(formationData, i + 1, data.heros) : false;
  50. this.heroSprites[i].grayscale = !hasHero;
  51. canFormation = canFormation && hasHero;
  52. this.formationObj[i + 1] = hero.id;
  53. }
  54. canFormation = canFormation && !isExistAllHero;
  55. this.campIcon.spriteFrame = this.campIconList[data.camp - 1];
  56. this.campBg.spriteFrame = this.campBgList[data.camp - 1];
  57. this.canFormation = canFormation;
  58. this.autoFormationBtn.node.active = canFormation;
  59. //最后再设置武将图片
  60. for (let i = 0; i < data.heros.length; i++) {
  61. let hero = DataSystem.getData(ConfigData).get("general")[data.heros[i]];
  62. this.heroSprites[i].spriteFrame = await this.getComponent(ResourceLoader).load(`images/general/texture/hero_img/${hero.hero_res}/spriteFrame`, SpriteFrame);
  63. }
  64. console.log("IsExist: " + isExistAllHero);
  65. }
  66. /**阵位是否存在羁绊武将*/
  67. private isExistHero(formationData: FormationData, formationId: number, heroIdAry: number[]) {
  68. let has = false;
  69. for (let i = 0; i < heroIdAry.length; i++) {
  70. if (formationData.get(formationId) && formationData.get(formationId) == heroIdAry[i]) {
  71. has = true;
  72. return has;
  73. }
  74. }
  75. return has;
  76. }
  77. /**阵位是否全部解锁*/
  78. private isUnlockAllFormation() {
  79. let unlock = true;
  80. let formationData = DataSystem.getData(FormationData);
  81. for (let i = 0; i < 5; i++) {
  82. if (!formationData.get(i + 1)) {
  83. unlock = false;
  84. return unlock;
  85. }
  86. }
  87. return unlock;
  88. }
  89. public async onAutoFormation() {
  90. if (!this.canFormation) {
  91. return;
  92. }
  93. if (!this.isUnlockAllFormation()) {
  94. WindowSystem.showTips("阵位未全部解锁,无法上阵");
  95. return;
  96. }
  97. console.log("IsUnlock: " + this.isUnlockAllFormation());
  98. let result = await this.getComponent(Http).send("/api/formation/syncFormation", { formation: this.formationObj });
  99. if (result && result.code == HttpResponseCode.Success) {//阵位改变成功
  100. let formationData = DataSystem.getData(FormationData);
  101. formationData.copy(result.data, true);
  102. WindowSystem.open("prefabs/ui/hero/heroUI", WindowOpenMode.NotCloseAndCover, [1, 2]);
  103. }
  104. }
  105. }