import { _decorator, Component, sp, SpriteFrame, Sprite, Label, Node, RichText, CCString } from "cc"; import { BattleData } from "../../battle/BattleData"; import { DataSystem } from "../../core/data/DataSystem"; import { Http, HttpResponseCode } from "../../core/net/Http"; import { PageView } from "../../core/page/PageView"; import { ResourceLoader } from "../../core/resourceManager/ResourceLoader"; import { BitmapFont } from "../../core/ui/BitmapFont"; import { Window } from "../../core/ui/window/Window"; import { WindowOpenMode } from "../../core/ui/window/WindowOpenMode"; import { WindowSystem } from "../../core/ui/window/WindowSystem"; import { StringUtils } from "../../core/utils/StringUtils"; import { ConfigData } from "../../data/ConfigData"; import { HttpErrorCode } from "../../data/HttpErrorCode"; import { UserData } from "../../data/UserData"; import { ReportThinking } from "../../ReportThinking"; import { Formation } from "../formation/Formation"; import { FormationData } from "../formation/FormationData"; import { UserHero, UserHeroData } from "../UserHeroData"; const { ccclass, property, requireComponent } = _decorator; /** * 武将动画数据 * @author 袁浩 */ @ccclass("HeroAniData") export class HeroAniData { @property({ tooltip: "是否循环播放" }) public loop: boolean = true; @property({ tooltip: "动画名称" }) public animation = ""; @property({ tooltip: "动画切换周期(秒)" }) public changeAniInterval: number = 5; } /** * 武将信息 * @author 袁浩 */ @ccclass('HeroInfo') @requireComponent(ResourceLoader) export class HeroInfo extends Component { @property({ tooltip: "武将骨骼动画组件", type: sp.Skeleton }) public body: sp.Skeleton; @property({ tooltip: "星级", type: BitmapFont }) public starBitmapLabel: BitmapFont; @property({ tooltip: "阵营图标", type: Sprite }) public campIcon: Sprite; @property({ tooltip: "所有阵营图标", type: [SpriteFrame] }) public campIconList: SpriteFrame[] = []; @property({ tooltip: "名字", type: Label }) public nameLabel: Label; @property({ tooltip: "等级", type: Label }) public lvLabel: Label; @property({ tooltip: "升级图片", type: SpriteFrame }) public upSpriteFrame: SpriteFrame; @property({ tooltip: "获取图片", type: SpriteFrame }) public getSpriteFrame: SpriteFrame; @property({ tooltip: "获取/升级按钮", type: Sprite }) public getButton: Sprite; @property({ tooltip: "碎片进度", type: Sprite }) public chipProgress: Sprite; @property({ tooltip: "碎片进度文本", type: Label }) public chipLabel: Label; @property({ tooltip: "箭头图标", type: Sprite }) public arrowIcon: Sprite; @property({ tooltip: "在阵形中的节点展示", type: Node }) public inFormationNode: Node; @property({ tooltip: "攻击文本", type: Label }) public attackLabel: Label; @property({ tooltip: "攻速文本", type: Label }) public attackSpeedLabel: Label; @property({ tooltip: "统帅文本", type: Label }) public commandLabel: Label; @property({ tooltip: "技能图标", type: Sprite }) public skillSprite: Sprite; @property({ tooltip: "技能描述", type: RichText }) public descLabel: RichText; @property({ tooltip: "切换的动画序列", type: [HeroAniData] }) public aniList: HeroAniData[] = []; @property({ tooltip: "初始动画索引" }) public aniDefaultIndex: number = 0; private data: UserHero; private userHeroData: UserHeroData; start() { let params = this.getComponent(Window).params; let data = params[0]; this.pullUserHeroData(); this.loadHero(data); } private async pullUserHeroData() { let userHeroData = DataSystem.getData(UserHeroData); // await userHeroData.pull(this.getComponent(Http)); this.userHeroData = userHeroData; } private async loadHero(data: UserHero) { let isOld = this.data && this.data.server.id == data.server.id; this.data = data; let starString = ""; let star = !!data.server ? data.server.star : data.client.initStar; for (let i = 0; i < star; i++) { starString += "0"; } this.starBitmapLabel.string = starString; this.campIcon.spriteFrame = this.campIconList[data.client.camp - 1]; this.nameLabel.string = data.client.name; this.lvLabel.string = `Lv.${data.server.lv}`; let starConfig = DataSystem.getData(ConfigData).get("star")[data.server.star]; if (starConfig) { this.chipProgress.fillRange = Math.min(1, data.server.chip / starConfig.nextReq); this.chipLabel.string = `${data.server.chip}/${starConfig.nextReq}`; if (data.server.chip >= starConfig.nextReq) { this.getButton.spriteFrame = this.upSpriteFrame; } else { this.getButton.spriteFrame = this.getSpriteFrame; } this.getButton.node.active = true; } else {//满级 this.chipProgress.fillRange = 1; this.chipLabel.string = `已满星`; this.getButton.node.active = false; } this.attackLabel.string = this.formateValue(data.client.attack + (data.server.lv - 1) * parseInt(data.client.upRatio.split(",")[data.server.star - 1])); this.attackSpeedLabel.string = this.formateValue(data.client.attSpeed); this.commandLabel.string = this.formateValue(data.client.commander); let skill = DataSystem.getData(ConfigData).get("skill")[data.client.skillID]; this.descLabel.string = StringUtils.getRichText(skill.des); this.inFormationNode.active = !!DataSystem.getData(FormationData).getIDByHero(data.server.id); this.skillSprite.spriteFrame = await this.getComponent(ResourceLoader).load(`images/skill/${skill.icon}/spriteFrame`, SpriteFrame); if (!isOld) {//只有武将编号变化才重新加载骨骼动画 this.body.skeletonData = await this.getComponent(ResourceLoader).load(`spine/hero/${data.client.hero_res}`, sp.SkeletonData); this.body.setAnimation(0, this.aniList[this.aniDefaultIndex].animation, this.aniList[this.aniDefaultIndex].loop); } } private formateValue(num: number) { return num > 10000 ? (num / 1000).toFixed(1) + "k" : num + ""; } public async upStar() { let starConfig = DataSystem.getData(ConfigData).get("star")[this.data.server.star]; if (this.data.server.chip >= starConfig.nextReq) {//碎片足够则升星 let result = await this.getComponent(Http).send("/api/hero/upStar", { id: this.data.server.id }); if (result && result.code == HttpResponseCode.Success) {//升星成功 let oldStar = this.data.server.star; this.data.server.chip = result.data; this.data.server.star++; DataSystem.getData(UserHeroData).set(this.data.client.id, this.data); this.loadHero(this.data); this.setBattleHerosData(this.data.server.id); ReportThinking.hero_levelup(this.data.client.name, starConfig.nextReq, this.data.server.lv, this.data.server.chip, oldStar, oldStar + 1); //打开升星界面 WindowSystem.open("prefabs/ui/hero/upstar", WindowOpenMode.NotCloseAndCover, [this.data, oldStar]); } else if (result && result.code == HttpErrorCode.LessChip) {//碎片不足则重新同步碎片 this.data.server.chip = result.data; DataSystem.getData(UserHeroData).set(this.data.client.id, this.data); } } else {//打开招募界面 WindowSystem.open("prefabs/ui/recruit/recruit", WindowOpenMode.NotCloseAndAdd); } } /**更新战斗武将星级*/ private setBattleHerosData(id: number) { let userData = DataSystem.getData(UserData); for (let i = 1; i < 6; i++) { let battleHeroData = DataSystem.getData(BattleData).heros.get(i); if (battleHeroData && battleHeroData.id && battleHeroData.id == id) { battleHeroData.starLv++; } } } public nextHero() { if (!this.userHeroData) {//如果用户数据没加载完成 return; } let formationData = DataSystem.getData(FormationData); this.loadHero(this.userHeroData.get(formationData.nextHexo(this.data.server.id))); } public preHero() { if (!this.userHeroData) {//如果用户数据没加载完成 return; } let formationData = DataSystem.getData(FormationData); this.loadHero(this.userHeroData.get(formationData.preHero(this.data.server.id))); } private lastTime: number = 0; update(dt: number) { this.lastTime += dt; if (this.lastTime > this.aniList[this.aniDefaultIndex].changeAniInterval) { this.playNextAni(); } if (DataSystem.watch(UserHeroData, this.data.client.id)) { this.loadHero(DataSystem.getData(UserHeroData).get(this.data.client.id)); } } private playNextAni() { this.lastTime = 0; this.aniDefaultIndex++; if (this.aniDefaultIndex >= this.aniList.length) { this.aniDefaultIndex = 0; } this.body.setAnimation(0, this.aniList[this.aniDefaultIndex].animation, this.aniList[this.aniDefaultIndex].loop); this.body.setCompleteListener(() => { if (!this.aniList[this.aniDefaultIndex].loop) { this.playNextAni(); } }); } public changeHero() { let windowList = WindowSystem.getWindowList(); let formationIsOpen = false; for (let i = 0; i < windowList.length; i++) { const _window = windowList[i]; let pageView = _window.getComponent(PageView); if (pageView && pageView.pages[1] && pageView.pages[1].getComponent(Formation)) { formationIsOpen = true; pageView.selectedIndex = 1; this.getComponent(Window).close(); } } !formationIsOpen && WindowSystem.open("prefabs/ui/hero/heroUI", WindowOpenMode.CloseAndAdd, [1]); } }