import { _decorator, Component, Sprite, Label, Node, Animation, SpriteFrame, tween, Vec3, UIOpacity, Prefab, instantiate, ProgressBar, ImageAsset, Texture2D, utils } from 'cc'; import { Http } from '../../core/net/Http'; import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils'; import { WindowManager } from '../../core/ui/window/WindowManager'; import { Utils } from '../../core/utils/Utils'; import { ConfigData } from '../../Data/ConfigData'; import { g } from '../../Data/g'; import { ISJSB, platform } from '../../Data/platform'; import { FsUtils } from '../../FsUtils/FsUtils'; import { AddParliclePkBase } from '../../SpecialEffects/AddParliclePkBase'; const { ccclass, property } = _decorator; /**切磋神将 */ @ccclass('BattleRole') export class BattleRole extends Component { @property({ type: Node, tooltip: "底坐" }) bottomSeatNode: Node; @property({ type: Sprite, tooltip: "底红坐光效" }) redLightSprite: Sprite; @property({ type: Sprite, tooltip: "底蓝坐光效" }) blueLightSprite: Sprite; @property({ type: Sprite, tooltip: "神将" }) generalSprite: Sprite; @property({ type: Label, tooltip: "神将等级" }) lvLabel: Label; @property({ type: Node, tooltip: "邀请好友信息" }) inviteNode: Node; @property({ type: Node, tooltip: "用户头像组" }) headNode: Node; @property({ type: Sprite, tooltip: "微信头像" }) userHeadSprite: Sprite; @property({ type: Node, tooltip: "主将特效节点" }) mainGeneralAnimationNode: Node; @property({ type: Node, tooltip: "空闲状态时用户名字组" }) userNameFreeNode: Node; @property({ type: Label, tooltip: "用户名" }) userNameLabel: Label; @property({ type: Animation, tooltip: "神将台动画" }) animation: Animation; @property({ tooltip: "是否播放动画" }) isPlayAaimation: Boolean = true; @property({ type: Http, tooltip: "消息控制" }) http: Http; @property({ tooltip: "我方血条", type: ProgressBar }) ourHpProgress: ProgressBar; @property({ type: ProgressBar, tooltip: "敌方血条" }) emenyHpProgress: ProgressBar; /** 显示状态 */ private _showState: number; /**战斗状态 */ private _battleState: number; /**好友神将数据 */ private generalData: any;//{id: number, lv: number, nickName: string, avator: string}好友I, /**神将基础数据 */ public generalBaseData: any; /**是否是我方神将 */ public isOur = true; public speed: number; public HP: number = 0; private delay: number = 1;//执行间隔 private time = 0;//当前时间 private lastTime = 0;//上次执行时间点 public startMove = false;//是自动否移动 start() { if (this.isPlayAaimation) { this.scheduleOnce(() => { this.animation.play(); }, Math.random()); } } /** * 设置神将数据 * @param value 用户神将数据{avator,id,lv,nickName} * @param state 显示状态 * @param isOur 是否是我方神将 */ public async setData(value: any, state: number = 0, isOur: boolean = true) { this.isOur = isOur; this.generalData = value; if (state > 0 || value.lv > 0) { value.lv = value.lv || 1; this.generalBaseData = ConfigData.configMap.get("generalBase")[value.lv]; this.speed = this.generalBaseData.speed; this.HP = this.generalBaseData.HP; let img: SpriteFrame = await ResourcesUtils.load("Roles/" + value.lv + "/spriteFrame", SpriteFrame, this.node); this.generalSprite.spriteFrame = img; this.lvLabel.string = value.lv + ''; this.generalSprite.node.active = true; } if (value.id > 0 && state == 0 && value.lv == 0) { value.lv = value.lv || 1; let img: SpriteFrame = await ResourcesUtils.load("Roles/" + value.lv + "/spriteFrame", SpriteFrame, this.node); this.generalSprite.spriteFrame = img; this.lvLabel.string = '0'; this.generalSprite.node.active = true; } if (ISJSB && this.generalData.avator) { let img = await ResourcesUtils.loadRemote(this.generalData.avator, { ext: ".png" }, this); const spriteFrame = new SpriteFrame(); const texture = new Texture2D(); texture.image = img; spriteFrame.texture = texture; this.userHeadSprite.spriteFrame = spriteFrame; } this.showState = state; } public setHp(value: number) { if (!value) return; this.HP = (this.HP * 100 - value * 100) / 100; this.HP = this.HP < 0 ? 0 : this.HP; let hpProgress = Number(FsUtils.toFixed(this.HP / this.generalBaseData.HP, 2)); if (this.isOur) { this.ourHpProgress.progress = hpProgress; } else { this.emenyHpProgress.progress = hpProgress; } } public set showState(value: number) { this._showState = value; switch (value) { case BattleRoleState.Free: if (this.generalData.id != 0) { this.userNameFreeNode.active = this.headNode.active = true; } this.inviteNode.active = !this.headNode.active; if (this.headNode.active) { this.userNameFreeNode.getChildByName("名字").getComponent(Label).string = this.generalData.nickName; } break; case BattleRoleState.Ready: this.setReadyView(); break; case BattleRoleState.Fight: this.setReadyView(); this.showHp(); break; } } public async showHp() { this.ourHpProgress.node.active = this.isOur; this.emenyHpProgress.node.active = !this.isOur;; this.ourHpProgress.progress = 1; this.emenyHpProgress.progress = 1; } public get showState() { return this._showState; } public set battleState(value: number) { if (this._battleState != value) { this._battleState = value; } } public get battleState() { return this._battleState; } /**设置神将显示 */ private async setReadyView() { this.userNameFreeNode.active = false; if (this.generalData.id) {//==0普通神将 this.headNode.active = true; this.userNameFreeNode.active = true; if (this.generalData.id == g.userData.id || this.generalData.id == g.battleData.enemyID) {//主将 let effect = await ResourcesUtils.load("Prefabs/SpecialEffects/底座特效", null, this.mainGeneralAnimationNode); let effectNode = instantiate(effect); let effectScript = effectNode.getComponent(AddParliclePkBase); effectNode.parent = this.mainGeneralAnimationNode; this.bottomSeatNode.setScale(new Vec3(1.4, 1.4, 0)); this.generalSprite.node.setScale(new Vec3(1, 1, 1)); if (this.generalData.id == g.battleData.enemyID) { this.userNameLabel.string = this.generalData.nickName; effectScript.setData(0); } else { effectScript.setData(1); this.userNameLabel.string = g.userData.nickName; } } else {// 好友主将 this.userNameLabel.string = this.generalData.nickName; } } this.redLightSprite.node.setScale(new Vec3(1.2, 1, 1)); this.blueLightSprite.node.setScale(new Vec3(1.2, 1, 1)); this.redLightSprite.node.active = !this.isOur; this.blueLightSprite.node.active = this.isOur; } update(delta: number) { if (this._battleState == BattleRoleState.Move) { this.time += delta; if (this.time - this.lastTime >= this.delay) { this.lastTime += this.delay; this.move(); } } } public move(pos: Vec3 = null) { let targetPos = new Vec3(this.node.position.x + (Math.random() > 0.5 ? +10 : -10), this.node.position.y + (Math.random() > 0.5 ? +10 : -10), 0); tween(this.node).to(1, { position: targetPos }).start(); } public setMove() { this.time = 0; this.lastTime = 0; this.startMove = true; } //邀请 public async onInvite() { let result = await this.http.send("/api/app/share", {}); if (result.code == 0) { platform.wxShare(result.data.url, result.data.title, result.data.descroption); } else { WindowManager.showTips("分享信息获取失败,请稍后再试"); } } } /**0:UI中状态,1:战斗准备,2战斗 */ export enum BattleRoleState { Free, Ready, Move, Fight }