import { _decorator, Component, Node, instantiate, Prefab, Label } from 'cc'; import { DataSystem } from '../../core/data/DataSystem'; import { ResourceLoader } from '../../core/resourceManager/ResourceLoader'; import { Sound } from '../../core/sound/Sound'; import { ConfigData } from '../../data/ConfigData'; import { BattleData } from '../BattleData'; import { MissionData } from '../MissionData'; import { BattleUIData } from './BattleUIData'; import { MissionBanner } from './MissionBanner'; const { ccclass, property } = _decorator; @ccclass('BattleUI') export class BattleUI extends Component { @property({ type: ResourceLoader, tooltip: "资源加载器" }) res: ResourceLoader; @property({ type: Node, tooltip: "UI父级" }) uiLayer: Node; @property({ type: Node, tooltip: "Boss图标" }) bossIcon: Node; @property({ type: Label, tooltip: "关卡文本" }) missionLabel: Label; @property({ type: Sound, tooltip: "BGM" }) bgm: Sound; private missionConfig: any; private data: BattleData; start() { DataSystem.createData(BattleUIData); this.missionConfig = DataSystem.getData(ConfigData).get("mission"); this.data = DataSystem.getData(BattleData); } update() { DataSystem.watch(BattleData, "mission") && this.changeMission(); DataSystem.watch(MissionData, "_dontShowResult") && this.showPvpBgm(); } private lastBgm = -1; /**当关卡发生变化时 */ private async changeMission() { let isBoss = this.missionConfig[this.data.mission]["type"] == 2; this.bossIcon.active = isBoss; this.missionLabel.string = `第${this.data.mission}关`; /**创建条幅 */ let banner = instantiate(await this.res.load('prefabs/battle/ui/missionBanner', Prefab)); banner.setParent(this.uiLayer); banner.getComponent(MissionBanner).setData(this.data.mission, this.missionConfig[this.data.mission]["type"] == 2); let bgmIndex = isBoss ? 1 : 0; if (this.lastBgm != bgmIndex && !DataSystem.getData(MissionData).isInPvp) { this.lastBgm = bgmIndex; this.bgm.play(bgmIndex); } } private showPvpBgm(): void { if (DataSystem.getData(MissionData).isInPvp) { this.bgm.play(1); } else { let bgmIndex = this.missionConfig[this.data.mission]["type"] == 2 ? 1 : 0; this.lastBgm = bgmIndex; this.bgm.play(bgmIndex); } } }