import { _decorator, clamp01, Component, Label, Node, Sprite, SpriteFrame } from 'cc'; import { MonsterType } from '../../manager/MonsterDataManager'; import { GEvent } from 'db://assets/mx/module/event/GEvent'; const { ccclass, property } = _decorator; @ccclass('MonsterBlood') export class MonsterBlood extends Component { @property({type: SpriteFrame}) bossProSpFrame: SpriteFrame[] = []; smallMonsterProgress: Node = null; midBossProgress: Node = null; bossProgress: Node = null; bossProgressBottom: Node = null; leftHpAccount: Node = null; protected onLoad(): void { this.bossProgress = this.node.find("bossProgress"); this.smallMonsterProgress = this.node.find("smallMonsterProgress"); this.midBossProgress = this.node.find("midBossProgress"); this.bossProgressBottom = this.node.find("bossProgressBottom"); this.leftHpAccount = this.node.find("leftHpAccount"); } Data: ITableMonster = null; updateBloodBar(hp: number, maxHp: number, shieldHp = 0, shieldMaxHp = 0) { if (this.Data.type == MonsterType.SmallMonster) { this.node.active = hp < maxHp || (shieldMaxHp > 0 && shieldHp > 0); } if (!this.node.active) return; if (this.smallMonsterProgress && this.smallMonsterProgress.active) { this.smallMonsterProgress.getComponent(Sprite).fillRange = clamp01(hp / maxHp) } if (this.midBossProgress) { this.midBossProgress.getComponent(Sprite).fillRange = clamp01(hp / maxHp) } //显示boss血条(分成15段) if ((this.Data.type == MonsterType.Boss ||this.Data.type == MonsterType.BigBoss) && this.bossProSpFrame.length > 0) { // 边界:hp 为 0 时不能再把当前段显示为满格,否则会出现“死亡瞬间回一管血”的错觉 if (hp <= 0) { this.bossProgress.getComponent(Sprite).spriteFrame = this.bossProSpFrame[0]; this.bossProgress.getComponent(Sprite).fillRange = 0; this.bossProgressBottom.active = false; if (this.leftHpAccount) { this.leftHpAccount.getComponent(Label).string = `x0`; } GEvent.Ins.emit(GEvent.UpdateBossBlood, 0, 0, 0, this.bossProSpFrame); return; } //每段血量 let length = this.Data.HPCount; let perHp = maxHp / length //当前血量所在段数 let curPeriodAccount = Math.floor(hp / perHp) let curPeriodHp = Math.floor(hp % perHp) let haveShowFullHp = length - curPeriodAccount; if (curPeriodHp == 0) curPeriodHp = perHp; if (haveShowFullHp <= 0) haveShowFullHp = 4; this.bossProgress.getComponent(Sprite).spriteFrame = this.bossProSpFrame[haveShowFullHp % 5] this.bossProgress.getComponent(Sprite).fillRange = clamp01(curPeriodHp / perHp); if (curPeriodAccount == 0) { this.bossProgressBottom.active = false } else { this.bossProgressBottom.active = true this.bossProgressBottom.getComponent(Sprite).spriteFrame = this.bossProSpFrame[(haveShowFullHp + 1) % 5] } if (this.leftHpAccount) { this.leftHpAccount.getComponent(Label).string = `x${curPeriodAccount}` } GEvent.Ins.emit(GEvent.UpdateBossBlood, curPeriodAccount, haveShowFullHp, clamp01(curPeriodHp / perHp), this.bossProSpFrame) } } }