import { _decorator, Component, Toggle, Node, EventTouch, SystemEventType, Sprite, SpriteFrame, Label, Animation, RichText, ToggleContainer } from "cc"; import { DataSystem } from "../../core/data/DataSystem"; import { Http, HttpResponseCode } from "../../core/net/Http"; import { ResourceLoader } from "../../core/resourceManager/ResourceLoader"; import { Sound } from "../../core/sound/Sound"; import { DragSource } from "../../core/ui/drag/DragSource"; import { DragSystem } from "../../core/ui/drag/DragSystem"; import { DragTarget } from "../../core/ui/drag/DragTarget"; import List from "../../core/ui/virtualList/List"; import { WindowSystem } from "../../core/ui/window/WindowSystem"; import { StringUtils } from "../../core/utils/StringUtils"; import { ConfigData } from "../../data/ConfigData"; import { HttpErrorCode } from "../../data/HttpErrorCode"; import { PublicLogicData } from "../../main/PublicLogicData"; import { UserHeroData } from ".././UserHeroData"; import { FormationData } from "./FormationData"; import { FormationHeroItem } from "./FormationHeroItem"; import { FormationOne } from "./FormationOne"; const { ccclass, property, requireComponent } = _decorator; /** * 阵位 * @author 袁浩 */ @ccclass('Formation') @requireComponent(Http) export class Formation extends Component { @property({ tooltip: "武将列表", type: List }) public list: List; @property({ tooltip: "所有的阵位", type: [FormationOne] }) public formationOneList: FormationOne[] = []; @property({ tooltip: "八卦图标", type: Sprite }) public baguaIcon: Sprite; @property({ tooltip: "八卦动画", type: Animation }) public baguaAni: Animation; @property({ tooltip: "太极动画", type: Animation }) public taiji: Animation; @property({ tooltip: "攻击加成文本", type: Label }) public attackLabel: Label; @property({ tooltip: "暴击加成文本", type: Label }) public criLabel: Label; @property({ tooltip: "阵营加成文本", type: RichText }) public nationLabel: RichText; @property({ tooltip: "阵营选项卡", type: ToggleContainer }) public campToggleContainer: ToggleContainer; private heroList: any[]; start() { for (let i = 0; i < this.formationOneList.length; i++) {//因为cocos预制体组件属性覆盖的BUG,不得不耦合本组件和单个阵位组件 this.formationOneList[i].formation = this; } this.list.node.on(SystemEventType.TOUCH_CANCEL, this.onTouchEnd, this); this.list.node.on(SystemEventType.TOUCH_END, this.onTouchEnd, this); this.initUI(DataSystem.getData(FormationData)); this.loadHeros(DataSystem.getData(PublicLogicData).enterFormationType == 1 ? this.getCampByHasHero() : this.getCampByMarshalHero()); } private async initUI(formationData: FormationData) { // await formationData.pull(this.getComponent(Http)); let fetter = formationData.getFetter(); let fetterConfig = DataSystem.getData(ConfigData).get("fetter")[fetter.fetterID]; let baguaIcon = !fetterConfig ? 'bagua1' : fetterConfig.icon; this.taiji.node.active = this.baguaAni.node.active = !!fetterConfig; this.attackLabel.string = `+${fetter.attackPer / 100}%`; this.criLabel.string = `+${fetter.cri / 100}%`; if (fetter.nationID) { let buffConfig = DataSystem.getData(ConfigData).get("buff")[fetter.nationID]; this.nationLabel.string = StringUtils.getRichText(buffConfig.des2); } else { this.nationLabel.string = ""; } this.baguaIcon.spriteFrame = await this.getComponent(ResourceLoader).load(`images/general/texture/${baguaIcon}/spriteFrame`, SpriteFrame); } private onTouchEnd(event: EventTouch) { DragSystem.onTouchEnd(event); } /** * 当阵位页面显示 */ public onShow() { this.loadHeros(DataSystem.getData(PublicLogicData).enterFormationType ? this.getCampByHasHero() : this.getCampByMarshalHero()); for (let i = 0; i < this.formationOneList.length; i++) { this.formationOneList[i].fixSpine(); } this.initUI(DataSystem.getData(FormationData)); } /** * 当点击阵营项 * @param toggle */ public onCamp(toggle: Toggle) { this.loadHeros(toggle._toggleContainer.toggleItems.indexOf(toggle) + 1); } /** * 获取有武将的阵营 * @returns */ private getCampByHasHero() { let userHeroData = DataSystem.getData(UserHeroData); for (let i = 1; i < 5; i++) { for (let j = 0; j < userHeroData.haveHeros.length; j++) { if (userHeroData.get(userHeroData.haveHeros[j]).client.camp == i) { return i; } } } return 1; } /** * 获取统帅武将阵营 若没有返回1 */ private getCampByMarshalHero() { let userHeroData = DataSystem.getData(UserHeroData); let formationData = DataSystem.getData(FormationData); if (formationData.get(5)) return userHeroData.get(formationData.get(5)).client.camp; else return 1; } /** * 根据阵营加载已拥有英雄数据并展示到UI * @param camp 阵营 */ public async loadHeros(camp: number) { this.campToggleContainer.toggleItems[camp - 1].isChecked = true; let heroList = []; let userHeroData = DataSystem.getData(UserHeroData); // await userHeroData.pull(this.getComponent(Http)); for (let i = 0; i < userHeroData.haveHeros.length; i++) { const hero = userHeroData.get(userHeroData.haveHeros[i]); if (camp == 0 || hero.client.camp == camp) { heroList.push(hero.client.id); } } this.heroList = userHeroData.sort(heroList, camp, heroList); this.list.numItems = this.heroList.length; } /** * 当列表项渲染 * @param item 项节点 * @param index 项索引 */ public onListRender(item: Node, index: number) { item.getComponent(FormationHeroItem).onDataChange(this.heroList[index], index); } /** * 当放下拖拽 * @param source */ public async onDrop(source: DragSource, target: DragTarget) { let oldFormationOne = source.getComponent(FormationOne); let heroData = (source.getComponent(FormationHeroItem) || source.getComponent(FormationOne)).data; if (!heroData) { return; } let formatoinID = 0; for (let i = 0; i < this.formationOneList.length; i++) { const formationOne = this.formationOneList[i]; if (formationOne.getComponent(DragTarget) == target) { formatoinID = formationOne.id;//目标阵位 break; } } let formationData = DataSystem.getData(FormationData); let formationObj: { [key: number]: number } = formationData.object; for (let key in formationObj) { if (formationObj[key] == heroData.client.id) {//如果别的阵位有此武将 formationObj[key] = formationObj[formatoinID]; } } oldFormationOne && (formationObj[oldFormationOne.id] = formationObj[formatoinID]);//如果是阵位拖拽到阵位上并且目标阵位有武将 formationObj[formatoinID] = heroData.client.id;//设置阵位武将 this.getComponent(Sound).play();//播放音效 let result = await this.getComponent(Http).send("/api/formation/syncFormation", { formation: formationObj }); if (result && result.code == HttpResponseCode.Success) {//阵位改变成功 formationData.copy(result.data, true); this.initUI(formationData); } else {//如果修改失败则还原阵位 if (result.code == HttpErrorCode.LessQuality) { WindowSystem.showTips("武将品质不足,无法上阵统帅位"); } formationData.copy(formationData.object, true); } } }