import { _decorator, Component, Node, Sprite, Label, TERRAIN_HEIGHT_BASE, UITransform, ImageAsset, SpriteFrame, Texture2D } from 'cc'; import { RewardVideoSystem } from '../../ad/RewardVideoSystem'; import { DataSystem } from '../../core/data/DataSystem'; import { Http, HttpResponseCode } from '../../core/net/Http'; import { HttpSystem } from '../../core/net/HttpSystem'; import { ResourceLoader } from '../../core/resourceManager/ResourceLoader'; import { BitmapFont } from '../../core/ui/BitmapFont'; import { CountDown } from '../../core/ui/CountDown'; import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode'; import { WindowSystem } from '../../core/ui/window/WindowSystem'; import { Utils } from '../../core/utils/Utils'; import { ConfigData } from '../../data/ConfigData'; import { ISJSB } from '../../data/jsb/platform'; import { UserData } from '../../data/UserData'; import { UserHeroData } from '../../hero/UserHeroData'; import { PVPCardData } from '../../pvp/PVP'; import { RecruitData } from '../../recruit/RecruitData'; import { UpdateRecruit } from '../../recruit/ui/UpdateRecruit'; import { ReportThinking } from '../../ReportThinking'; import { FightData, FightItemData } from '../FightData'; const { ccclass, property } = _decorator; /**切磋 复仇Item*/ @ccclass('FightItem') export class FightItem extends Component { @property({ type: ResourceLoader, displayName: "资源加载组件", tooltip: "资源加载组件" }) res: ResourceLoader = null; @property({ type: Http, displayName: "Http组件", tooltip: "Http组件" }) http: Http = null; @property({ type: Sprite, displayName: '头像', tooltip: "头像" }) imgHead: Sprite = null; @property({ type: Node, displayName: '随机玩家头像', tooltip: "随机玩家头像" }) imgHeadRandom: Node = null; @property({ type: Label, displayName: '文本昵称', tooltip: "文本昵称" }) txtNickName: Label = null; @property({ type: BitmapFont, displayName: '文本秒伤', tooltip: "文本秒伤" }) txtAttack: BitmapFont = null; @property({ type: Node, displayName: '按钮红包', tooltip: "按钮红包" }) btnRedbag: Node = null; @property({ type: Node, displayName: '按钮查看', tooltip: "按钮查看" }) btnCheck: Node = null; @property({ type: Node, displayName: '按钮切磋', tooltip: "按钮切磋" }) btnFight: Node = null; @property({ type: Node, displayName: '按钮复仇', tooltip: "按钮复仇" }) btnRevenge: Node = null; @property({ type: Node, displayName: '红包节点', tooltip: "红包节点" }) redbagNode: Node = null; @property({ type: Node, displayName: '广告图标', tooltip: "广告图标" }) adfight: Node = null; @property({ type: BitmapFont, displayName: '文本倒计时', tooltip: "文本倒计时" }) txtTime: BitmapFont = null; @property({ type: CountDown, displayName: '倒计时组件', tooltip: "倒计时组件" }) countDown: CountDown = null; private remaindTime = 0; private fightData: FightItemData; private isCanClick = true; /**当数据改动时*/ public async onDataChange(fightItemData: FightItemData, index: number) { this.fightData = fightItemData; this.imgHeadRandom.active = fightItemData.isRandomPlayer; if (fightItemData.isRandomPlayer) { this.txtNickName.string = `随机玩家`; this.txtAttack.string = `???`; this.btnRedbag.active = false; this.btnCheck.active = false; this.btnFight.active = true; this.btnRevenge.active = false; this.redbagNode.active = false; this.adfight.active = DataSystem.getData(FightData).pvpData.times <= 0; } else { let curTime = HttpSystem.serverTime * 0.001; this.remaindTime = (fightItemData.itemType == 1 && fightItemData.redbagType != -1 && fightItemData.canOpenTime > curTime) ? fightItemData.canOpenTime - curTime : 0; this.txtNickName.string = `${fightItemData.nickName}`; this.txtAttack.string = `${fightItemData.attack > 10000 ? (fightItemData.attack / 1000).toFixed(1) + "k" : fightItemData.attack + ""}`; this.btnRedbag.active = fightItemData.itemType == 1 && fightItemData.redbagType != -1; this.btnCheck.active = fightItemData.itemType == 1 ? fightItemData.canOpenTime > curTime : false; this.btnFight.active = fightItemData.itemType == 1 && fightItemData.redbagType == -1; this.btnRevenge.active = fightItemData.itemType == 2; this.redbagNode.active = fightItemData.itemType == 1 && fightItemData.redbagType != -1; this.txtTime.string = (!fightItemData.canOpenTime || fightItemData.canOpenTime <= curTime) ? "" : Utils.formatCountDown((fightItemData.canOpenTime - curTime) * 1000) + ""; if (this.remaindTime > 0) { this.countDown.stop(); this.countDown.value = this.remaindTime; this.countDown.play() } this.adfight.active = fightItemData.itemType == 1 && fightItemData.redbagType == -1 && DataSystem.getData(FightData).pvpData.times <= 0; } if (ISJSB() && !fightItemData.isRandomPlayer && fightItemData.avator) { let img = await this.res.loadRemote(fightItemData.avator, { ext: ".png" }); const spriteFrame = new SpriteFrame(); const texture = new Texture2D(); texture.image = img; spriteFrame.texture = texture; this.imgHead.spriteFrame = spriteFrame; } } public updateTxtTime(progress) { this.txtTime.string = Utils.formatCountDown(progress * 1000); } public completeTime() { this.remaindTime = 0; this.txtTime.string = ""; this.btnCheck.active = false; this.btnRedbag.active = true; } private getPVPParam(data: any) { let g1: Array = []; let g2: Array = []; let key: number = parseInt(data.key); let baseHit = DataSystem.getData(ConfigData).get("serverConfig").pvpBaseDamge; let buff = DataSystem.getData(ConfigData).get("buff"); let myHeros: any[] = data.myHeroDatas.heroDatas; let myFHeros: any[] = data.myFriendHeroDatas; let enemyHeros: any[] = data.enemyHeroDatas.heroDatas; let enemyFHeros: any[] = data.enemyFriendHeroDatas; let userData = DataSystem.getData(UserHeroData); for (let i = 0; i < myHeros.length; i++) { let tempHero = userData.get(myHeros[i].id).client; let temp = new PVPCardData(myHeros[i].id, myHeros[i].lv, myHeros[i].star, tempHero.color, tempHero.camp, data.myHeroDatas.avator); g1.push(temp); } for (let i = 0; i < myFHeros.length; i++) { let tempHero = userData.get(myFHeros[i].heroData.id).client; let temp = new PVPCardData(myFHeros[i].heroData.id, myFHeros[i].heroData.lv, myFHeros[i].heroData.star, tempHero.color, tempHero.camp, myFHeros[i].avator); g1.push(temp); } for (let i = 0; i < enemyHeros.length; i++) { let tempHero = userData.get(enemyHeros[i].id).client; let temp = new PVPCardData(enemyHeros[i].id, enemyHeros[i].lv, enemyHeros[i].star, tempHero.color, tempHero.camp, data.enemyHeroDatas.avator); g2.push(temp); } for (let i = 0; i < enemyFHeros.length; i++) { let tempHero = userData.get(enemyFHeros[i].heroData.id).client; let temp = new PVPCardData(enemyFHeros[i].heroData.id, enemyFHeros[i].heroData.lv, enemyFHeros[i].heroData.star, tempHero.color, tempHero.camp, enemyFHeros[i].avator); g2.push(temp); } return { g1: g1, g2: g2, key: key, baseHit: baseHit, buff: buff }; } /**获取奖励 * @param rewardType 奖励类型 除了 money diamond bonus prestige 之外为武将碎片id * @param numData 数量 */ private getReward(rewardType: string, numData: number) { console.log("btnRedbag World Pos: (" + this.btnRedbag.worldPosition.x + "," + this.btnRedbag.worldPosition.y + ")"); let type = 0; switch (rewardType) { case "money": type = 1; break; case "diamond": type = 2; break; case "bonus": type = 3; break; //case "prestige": type = 7; break; case "prestige": type = 4; break; default: //rewardType 为碎片id /**更新武将数据*/ DataSystem.getData(UserHeroData).addChip(parseInt(rewardType), numData); let hero = DataSystem.getData(UserHeroData).get(parseInt(rewardType)); let heroData = { id: hero.server.id, star: hero.server.star, chip: hero.server.chip, lv: hero.server.lv, percent: hero.server.percent }; this.getComponent(UpdateRecruit).addRecruitResults([{ chip: numData, hero: heroData }]); if (DataSystem.getData(RecruitData).recruitResults && DataSystem.getData(RecruitData).recruitResults.length > 0) { WindowSystem.open("prefabs/ui/recruit/recruitResult", WindowOpenMode.NotCloseAndCover); } break; } if (type != 0) { WindowSystem.open("prefabs/ui/fight/fightRewardUI", WindowOpenMode.NotCloseAndCover, [[{ rewardType: type, num: numData }]]); //WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndCover, [{ type: type, num: numData, position: this.btnRedbag.worldPosition, isWorldPos: true }]); } } /** * PVP准备 * @param type pvp类型 0 随机 1 好友 2 复仇 3 挑战 * @param enemyID 敌人编号 如果是随机 就填0 */ private async PvpReady(type: number, enemyID: number): Promise { let result = await this.http.send("/api/pvp/ReadyPVP", { type: type, enemyID: enemyID }); if (result && result.code == HttpResponseCode.Success) { let param = this.getPVPParam(result.data); console.log("Ready Param: ", param); WindowSystem.open("prefabs/pvp/pvp", WindowOpenMode.NotCloseAndCover, [param]); return true; } else { WindowSystem.showTips("请求失败"); return false; } } /**更新红包数据*/ private updateBonusData() { let fightData = DataSystem.getData(FightData); let rbIndex; if (this.fightData.redbagType == 1) { //刪除攻击红包数据 rbIndex = fightData.attackBonusList.indexOf(this.fightData.redbagData); if (rbIndex != -1) { fightData.attackBonusList.splice(rbIndex, 1); } fightData.attackBonusList = fightData.attackBonusList; console.log("attack rbindex: " + rbIndex); } else if (this.fightData.redbagType == 2) {//修改被攻击红包数据 openID rbIndex = fightData.beAttackBonusList.indexOf(this.fightData.redbagData); fightData.beAttackBonusList[rbIndex].openID = DataSystem.getData(UserData).id; fightData.beAttackBonusList = fightData.beAttackBonusList; console.log("beAttack rbindex: " + rbIndex); } } /**切磋 包括随机*/ //g1: Array, g2: Array, key: number, baseHit: number, buff: any private async onClickFight() { if (!this.isCanClick) return; this.isCanClick = false; console.log("click fight"); let fightData = DataSystem.getData(FightData); if (fightData.pvpData.times > 0) { let result = await this.PvpReady(this.fightData.isRandomPlayer ? 0 : 1, this.fightData.isRandomPlayer ? 0 : this.fightData.id); if (result) { fightData.pvpData.times--; fightData.pvpData = fightData.pvpData; } this.isCanClick = true; } else { //判断观看视频切磋次数 if (fightData.pvpData.videoTimes > 0) { let resultAd = await this.getComponent(Http).send("/api/ad/watchVideoAD"); if (resultAd && resultAd.code == HttpResponseCode.Success) { //可以观看视频 ReportThinking.ad_init('add_pvptimes'); let adData = await RewardVideoSystem.show(); if (adData) { let addPvpTimes = await this.getComponent(Http).send("/api/pvp/AddPVPTimes", { adData: adData.obj }); if (addPvpTimes && addPvpTimes.code == HttpResponseCode.Success) { ReportThinking.ad_end('add_pvptimes'); let result = await this.PvpReady(this.fightData.isRandomPlayer ? 0 : 1, this.fightData.isRandomPlayer ? 0 : this.fightData.id); if (result) { fightData.pvpData.videoTimes--; fightData.pvpData = fightData.pvpData; } this.isCanClick = true; } else { WindowSystem.showTips("请求失败"); this.isCanClick = true; } } else { WindowSystem.showTips("观看视频失败"); this.isCanClick = true; } } else { WindowSystem.showTips("视频请求失败"); this.isCanClick = true; } } else { WindowSystem.showTips("观看视频切磋次数不足"); this.isCanClick = true; } } } /**查看红包*/ private onClickCheck() { console.log("click check"); WindowSystem.open("prefabs/ui/fight/fightRedbagUI", WindowOpenMode.NotCloseAndCover, [this.fightData]); } /**获取红包*/ private async onClickGetRedbag() { if (!this.isCanClick) return; this.isCanClick = false; console.log("click get redbag", this.fightData); let result = await this.http.send("/api/pvp/ReceivePVPBonus", { key: this.fightData.redbagKey }); if (result && result.code == HttpResponseCode.Success) { this.getReward(result.data.rewardType, result.data.rewardNum); this.updateBonusData(); this.isCanClick = true; } else if (result && result.code == 115) { WindowSystem.showTips("已被领取"); this.updateBonusData(); this.isCanClick = true; } else { WindowSystem.showTips("请求失败"); this.isCanClick = true; } } /**复仇*/ private async onClickRevenge() { if (!this.isCanClick) return; this.isCanClick = false; console.log("click revenge"); await this.PvpReady(2, this.fightData.id); this.isCanClick = true; } }