BattleUI.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { _decorator, Component, Node, instantiate, Prefab, Label } from 'cc';
  2. import { DataSystem } from '../../core/data/DataSystem';
  3. import { ResourceLoader } from '../../core/resourceManager/ResourceLoader';
  4. import { Sound } from '../../core/sound/Sound';
  5. import { ConfigData } from '../../data/ConfigData';
  6. import { BattleData } from '../BattleData';
  7. import { MissionData } from '../MissionData';
  8. import { BattleUIData } from './BattleUIData';
  9. import { MissionBanner } from './MissionBanner';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('BattleUI')
  12. export class BattleUI extends Component {
  13. @property({ type: ResourceLoader, tooltip: "资源加载器" }) res: ResourceLoader;
  14. @property({ type: Node, tooltip: "UI父级" }) uiLayer: Node;
  15. @property({ type: Node, tooltip: "Boss图标" }) bossIcon: Node;
  16. @property({ type: Label, tooltip: "关卡文本" }) missionLabel: Label;
  17. @property({ type: Sound, tooltip: "BGM" }) bgm: Sound;
  18. private missionConfig: any;
  19. private data: BattleData;
  20. start() {
  21. DataSystem.createData(BattleUIData);
  22. this.missionConfig = DataSystem.getData(ConfigData).get("mission");
  23. this.data = DataSystem.getData(BattleData);
  24. }
  25. update() {
  26. DataSystem.watch(BattleData, "mission") && this.changeMission();
  27. DataSystem.watch(MissionData, "_dontShowResult") && this.showPvpBgm();
  28. }
  29. private lastBgm = -1;
  30. /**当关卡发生变化时 */
  31. private async changeMission() {
  32. let isBoss = this.missionConfig[this.data.mission]["type"] == 2;
  33. this.bossIcon.active = isBoss;
  34. this.missionLabel.string = `第${this.data.mission}关`;
  35. /**创建条幅 */
  36. let banner = instantiate(await this.res.load<Prefab>('prefabs/battle/ui/missionBanner', Prefab));
  37. banner.setParent(this.uiLayer);
  38. banner.getComponent(MissionBanner).setData(this.data.mission, this.missionConfig[this.data.mission]["type"] == 2);
  39. let bgmIndex = isBoss ? 1 : 0;
  40. if (this.lastBgm != bgmIndex && !DataSystem.getData(MissionData).isInPvp) {
  41. this.lastBgm = bgmIndex;
  42. this.bgm.play(bgmIndex);
  43. }
  44. }
  45. private showPvpBgm(): void {
  46. if (DataSystem.getData(MissionData).isInPvp) {
  47. this.bgm.play(1);
  48. } else {
  49. let bgmIndex = this.missionConfig[this.data.mission]["type"] == 2 ? 1 : 0;
  50. this.lastBgm = bgmIndex;
  51. this.bgm.play(bgmIndex);
  52. }
  53. }
  54. }