import { _decorator, Component, Node, Label, Toggle } from 'cc'; import { DataSystem } from '../../core/data/DataSystem'; import { Http, HttpResponseCode } from '../../core/net/Http'; import { ResourceLoader } from '../../core/resourceManager/ResourceLoader'; import { BitmapFont } from '../../core/ui/BitmapFont'; import List from '../../core/ui/virtualList/List'; import { WindowSystem } from '../../core/ui/window/WindowSystem'; import { ConfigData } from '../../data/ConfigData'; import { platform } from '../../data/jsb/platform'; import { UserData } from '../../data/UserData'; import { FormationData } from '../../hero/formation/FormationData'; import { UserHeroData } from '../../hero/UserHeroData'; import { ReportThinking } from '../../ReportThinking'; import { FightData, FightItemData, FriendData } from '../FightData'; import { FightItem } from '../item/FightItem'; import { FightPlaceItem } from '../item/FightPlaceItem'; const { ccclass, property } = _decorator; @ccclass('FightUI') export class FightUI extends Component { @property({ type: ResourceLoader, displayName: "资源加载组件", tooltip: "资源加载组件" }) res: ResourceLoader = null; @property({ type: Http, displayName: "Http组件", tooltip: "Http组件" }) http: Http = null; @property({ type: Label, displayName: '文本_总秒伤', tooltip: "文本_总秒伤" }) txtTotalAttack: Label = null; @property({ type: BitmapFont, displayName: '今日剩余切磋次数', tooltip: "今日剩余切磋次数" }) txtFightNum: BitmapFont = null; @property({ type: [FightPlaceItem], displayName: '站位', tooltip: "站位" }) fightPlaceItems: FightPlaceItem[] = []; @property({ type: List, displayName: '列表', tooltip: "列表" }) public list: List; private isLoaded: boolean = false; private listData: FightItemData[]; private toggleIndex = 0; async start() { console.log("Data: ", this.listData); await this.loadData(); this.loadItemData(1); this.updatePlaceItem(); this.updateInfo(); console.log("UserData: ", DataSystem.getData(UserData)); //console.log("FightUI World Pos: (" + this.node.worldPosition.x + "," + this.node.worldPosition.y + ")"); } update() { DataSystem.watch(FightData, "totalAttack") && this.updateInfo(); DataSystem.watch(FightData, "pvpData") && this.updateInfo(); (DataSystem.watch(FightData, "pvpData") || DataSystem.watch(FightData, "beAttackBonusList")) && this.loadItemData(this.toggleIndex == 0 ? 1 : 2); this.watchRevenge(); this.watchAttackRedbag(); } private watchAttackRedbag() { if (DataSystem.watch(FightData, "attackBonusList")) { console.log("Atk Redbag: ", DataSystem.getData(FightData).attackBonusList); this.loadItemData(this.toggleIndex == 0 ? 1 : 2); } } /**观察复仇数据变化*/ private watchRevenge() { if (DataSystem.watch(FightData, "revengeSuccessEnemyId")) { if (DataSystem.getData(FightData).revengeSuccessEnemyId > 0) { let fightData = DataSystem.getData(FightData); for (let i = 0; i < fightData.beAttackBonusList.length; i++) { if (fightData.beAttackBonusList[i].enemyID == fightData.revengeSuccessEnemyId) { fightData.beAttackBonusList[i].revenged = true; } } fightData.revengeSuccessEnemyId = -1; this.toggleIndex == 1 && this.loadItemData(2); } } } /**标签切换时*/ onToggle(toggle: Toggle) { this.toggleIndex = toggle._toggleContainer.toggleItems.indexOf(toggle); this.loadItemData(toggle._toggleContainer.toggleItems.indexOf(toggle) + 1); } /**更新武将位置信息显示*/ private updatePlaceItem() { let fightData = DataSystem.getData(FightData); let formationData = DataSystem.getData(FormationData); let userHeroData = DataSystem.getData(UserHeroData); let userData = DataSystem.getData(UserData); let cfg = DataSystem.getData(ConfigData).get("battle"); let selfHeroId = formationData.get(5) ? formationData.get(5) : -1; let selfAttack = selfHeroId > 0 ? userHeroData.get(selfHeroId).client.attack : 0; for (let i = 0; i < 4; i++) { if (selfHeroId < 0) selfHeroId = formationData.get(i + 1) ? formationData.get(i + 1) : -1; selfAttack += formationData.get(i + 1) ? userHeroData.get(formationData.get(i + 1)).client.attack : 0; } let selfData: FriendData = { id: 0, heroID: selfHeroId, attack: selfAttack, campLv: userData.campLv, avator: userData.avator, nickName: userData.nickName, contribution: userData.contribution }; this.fightPlaceItems[0].init(selfData); for (let i = 1; i < this.fightPlaceItems.length; i++) { let friendData = fightData.friendDataList[i - 1] ? fightData.friendDataList[i - 1] : null; friendData = friendData ? (friendData.campLv >= cfg.unlockLv ? friendData : null) : null; this.fightPlaceItems[i].init(friendData); } } private updateInfo() { this.txtTotalAttack.string = DataSystem.getData(FightData).totalAttack > 10000 ? (DataSystem.getData(FightData).totalAttack / 1000).toFixed(1) + "k" : DataSystem.getData(FightData).totalAttack + ""; this.txtFightNum.string = (DataSystem.getData(FightData).pvpData.times + DataSystem.getData(FightData).pvpData.videoTimes) + ""; } /**加载数据*/ private async loadData() { let fightData = DataSystem.getData(FightData); await fightData.pull(this.http); this.isLoaded = true; console.log("FightData: " + JSON.stringify(fightData)); } /**加载列表数据 * @param itemType item类型 1 切磋 2 复仇 */ private async loadItemData(itemType: number) { if (this.isLoaded) { let fightData = DataSystem.getData(FightData); let tempListData = []; let attackRbList = fightData.attackBonusList.concat(); let beAttackRbList = fightData.beAttackBonusList.concat(); let cfg = DataSystem.getData(ConfigData).get("battle"); if (itemType == 1) {//切磋 let randomPlayer = new FightItemData(); randomPlayer.id = 0; randomPlayer.attack = 0; randomPlayer.avator = ""; randomPlayer.isRandomPlayer = true; tempListData.push(randomPlayer); for (let i = 0; i < fightData.friendDataList.length; i++) { if (fightData.friendDataList[i].campLv >= cfg.unlockLv) { let tempFriendData = new FightItemData(); tempFriendData.attack = fightData.friendDataList[i].attack; tempFriendData.avator = fightData.friendDataList[i].avator; tempFriendData.id = fightData.friendDataList[i].id; tempFriendData.itemType = itemType; tempFriendData.nickName = fightData.friendDataList[i].nickName; tempFriendData.redbagType = -1; let isHaveAttackRb = false, isHaveBeAttackRb = false; for (let j = 0; j < fightData.attackBonusList.length; j++) { if (fightData.attackBonusList[j].enemyID == tempFriendData.id) { isHaveAttackRb = true; tempFriendData.redbagType = 1; tempFriendData.redbagKey = fightData.attackBonusList[j].key; tempFriendData.redbagData = fightData.attackBonusList[j]; tempFriendData.canOpenTime = fightData.attackBonusList[j].canOpenTime; attackRbList.splice(j, 1); break; } } if (!isHaveAttackRb) { for (let j = 0; j < fightData.beAttackBonusList.length; j++) { if (fightData.beAttackBonusList[j].enemyID == tempFriendData.id) { isHaveBeAttackRb = true; tempFriendData.redbagType = 2; tempFriendData.redbagKey = fightData.beAttackBonusList[j].key; tempFriendData.redbagData = fightData.beAttackBonusList[j]; tempFriendData.canOpenTime = fightData.beAttackBonusList[j].canOpenTime; beAttackRbList.splice(j, 1); break; } } } tempListData.push(tempFriendData); } } for (let i = 0; i < attackRbList.length; i++) { let tempFriendData = new FightItemData(); tempFriendData.attack = attackRbList[i].attack; tempFriendData.avator = attackRbList[i].avator; tempFriendData.id = attackRbList[i].enemyID; tempFriendData.itemType = itemType; tempFriendData.nickName = attackRbList[i].nickName; tempFriendData.redbagType = 1; tempFriendData.redbagKey = attackRbList[i].key; tempFriendData.redbagData = attackRbList[i]; tempFriendData.canOpenTime = attackRbList[i].canOpenTime; tempListData.push(tempFriendData); } for (let i = 0; i < beAttackRbList.length; i++) { if (beAttackRbList[i].openID <= 0) { let tempFriendData = new FightItemData(); tempFriendData.attack = beAttackRbList[i].attack; tempFriendData.avator = beAttackRbList[i].avator; tempFriendData.id = beAttackRbList[i].enemyID; tempFriendData.itemType = itemType; tempFriendData.nickName = beAttackRbList[i].nickName; tempFriendData.redbagType = 2; tempFriendData.redbagKey = beAttackRbList[i].key; tempFriendData.redbagData = beAttackRbList[i]; tempFriendData.canOpenTime = beAttackRbList[i].canOpenTime; tempListData.push(tempFriendData); } } } else {//复仇 for (let i = 0; i < beAttackRbList.length; i++) { if (!beAttackRbList[i].revenged) { if (!this.isExistRevengeEnemy(tempListData, beAttackRbList[i].enemyID)) { let tempFriendData = new FightItemData(); tempFriendData.attack = beAttackRbList[i].attack; tempFriendData.avator = beAttackRbList[i].avator; tempFriendData.id = beAttackRbList[i].enemyID; tempFriendData.itemType = itemType; tempFriendData.nickName = beAttackRbList[i].nickName; tempFriendData.redbagType = -1; tempFriendData.canOpenTime = null; tempListData.push(tempFriendData); } } } } this.listData = tempListData; this.list.numItems = this.listData.length; console.log("Type: " + itemType); console.log("List: ", this.listData); } else { this.listData = []; this.list.numItems = 0; } } /**复仇列表是否已存在该敌人*/ private isExistRevengeEnemy(listData: any[], enemyId: number) { for (let i = 0; i < listData.length; i++) { if (listData[i].id == enemyId) return true; } return false; } /**当列表渲染时*/ public onListRender(item: Node, index: number) { if (this.listData && this.listData.length > 0) { item.getComponent(FightItem).onDataChange(this.listData[index], index); } } /**招募好友*/ private async onClickShare() { //邀请 let result = await this.http.send("/api/app/share", {}); if (result.code == 0) { ReportThinking.invite(); platform.wxShare(result.data.url, result.data.title, result.data.descroption); } else { WindowSystem.showTips("分享信息获取失败,请稍后再试"); } } }