import { _decorator, Component, Sprite, SpriteFrame, Button } from 'cc'; import { DataSystem } from '../../core/data/DataSystem'; import { Http, HttpResponseCode } from '../../core/net/Http'; import { ResourceLoader } from '../../core/resourceManager/ResourceLoader'; import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode'; import { WindowSystem } from '../../core/ui/window/WindowSystem'; import { ConfigData } from '../../data/ConfigData'; import { PublicLogicData } from '../../main/PublicLogicData'; import { FormationData } from '../formation/FormationData'; import { UserHeroData } from '../UserHeroData'; const { ccclass, property, requireComponent } = _decorator; /** * 名人堂项 * @author 袁浩 */ @ccclass('HOFItem') @requireComponent(ResourceLoader) export class HOFItem extends Component { @property({ tooltip: "武将图片列表", type: [Sprite] }) public heroSprites: Sprite[] = []; @property({ tooltip: "阵营图标", type: Sprite }) public campIcon: Sprite; @property({ tooltip: "所有阵营图标", type: [SpriteFrame] }) public campIconList: SpriteFrame[] = []; @property({ tooltip: "阵营背景", type: Sprite }) public campBg: Sprite; @property({ tooltip: "所有阵营背景", type: [SpriteFrame] }) public campBgList: SpriteFrame[] = []; @property({ tooltip: "一键上阵按钮", type: Button }) public autoFormationBtn: Button; private canFormation = false; private formationObj: any; start() { // for (let i = 0; i < this.heroSprites.length; i++) { // this.heroSprites[i].spriteFrame = null; // } // this.campIcon.spriteFrame = null; // this.campBg.spriteFrame = null; } public async onDataChange(data: { camp: number, heros: number[] }, index?: number) { let userHeroData = DataSystem.getData(UserHeroData); let formationData = DataSystem.getData(FormationData); let canFormation = true; this.formationObj = {}; let isExistAllHero = true; for (let i = 0; i < data.heros.length; i++) { let hero = DataSystem.getData(ConfigData).get("general")[data.heros[i]]; let hasHero = !!userHeroData.get(hero.id).server && userHeroData.get(hero.id).server.active; isExistAllHero = isExistAllHero ? this.isExistHero(formationData, i + 1, data.heros) : false; this.heroSprites[i].grayscale = !hasHero; canFormation = canFormation && hasHero; this.formationObj[i + 1] = hero.id; } canFormation = canFormation && !isExistAllHero; this.campIcon.spriteFrame = this.campIconList[data.camp - 1]; this.campBg.spriteFrame = this.campBgList[data.camp - 1]; this.canFormation = canFormation; this.autoFormationBtn.node.active = canFormation; //最后再设置武将图片 for (let i = 0; i < data.heros.length; i++) { let hero = DataSystem.getData(ConfigData).get("general")[data.heros[i]]; this.heroSprites[i].spriteFrame = await this.getComponent(ResourceLoader).load(`images/general/texture/hero_img/${hero.hero_res}/spriteFrame`, SpriteFrame); } console.log("IsExist: " + isExistAllHero); } /**阵位是否存在羁绊武将*/ private isExistHero(formationData: FormationData, formationId: number, heroIdAry: number[]) { let has = false; for (let i = 0; i < heroIdAry.length; i++) { if (formationData.get(formationId) && formationData.get(formationId) == heroIdAry[i]) { has = true; return has; } } return has; } /**阵位是否全部解锁*/ private isUnlockAllFormation() { let unlock = true; let formationData = DataSystem.getData(FormationData); for (let i = 0; i < 5; i++) { if (!formationData.get(i + 1)) { unlock = false; return unlock; } } return unlock; } public async onAutoFormation() { if (!this.canFormation) { return; } if (!this.isUnlockAllFormation()) { WindowSystem.showTips("阵位未全部解锁,无法上阵"); return; } console.log("IsUnlock: " + this.isUnlockAllFormation()); let result = await this.getComponent(Http).send("/api/formation/syncFormation", { formation: this.formationObj }); if (result && result.code == HttpResponseCode.Success) {//阵位改变成功 let formationData = DataSystem.getData(FormationData); formationData.copy(result.data, true); WindowSystem.open("prefabs/ui/hero/heroUI", WindowOpenMode.NotCloseAndCover, [1, 2]); } } }