sceneManager.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { _decorator, assetManager, AudioClip, AudioSource, Component, Node } from 'cc';
  2. import { attributes_data, scene_item_data, task_data } from '../../data/data';
  3. import { config } from '../config';
  4. import { game_run } from './game_run';
  5. import { gameManager } from './gameManager';
  6. import { tools } from '../tools';
  7. import { ClientEvent } from '../clientEvent';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('sceneManager')
  10. export class sceneManager extends Component {
  11. private mSceneTask :task_data = null;
  12. private mPageList: scene_item_data[] = []; //每个分页场景
  13. protected mCurPage:number = 0; //当前场景分页
  14. private mGameManager:gameManager = null;
  15. private mSceneData: scene_item_data = null;
  16. public init(gm:gameManager){
  17. this.mGameManager = gm;
  18. ClientEvent.off(config.EventRun.TOGGLE_YIN_YUE,this.updateStatus.bind(this),this)
  19. ClientEvent.on(config.EventRun.TOGGLE_YIN_YUE,this.updateStatus.bind(this),this)
  20. }
  21. public updateStatus(){
  22. if(!gameManager.getUserData().isOpenYinYue){
  23. this.getSound().stop()
  24. }else{
  25. this.getSound().loop = true;
  26. this.getSound().play()
  27. }
  28. }
  29. public getSceneTask(){
  30. return this.mSceneTask;
  31. }
  32. public playMusic(path:string, loop: boolean){
  33. if(path.length<=0){
  34. return
  35. }
  36. let call_back = (err: any, clip: AudioClip)=> {
  37. if(!err){
  38. gameManager.mp3_cache.set(path,clip)
  39. this.playClip(clip, loop);
  40. }
  41. }
  42. if(gameManager.mp3_cache.get(path)){
  43. let clip = gameManager.mp3_cache.get(path);
  44. call_back(null,clip)
  45. }else{
  46. assetManager.loadRemote(path, call_back );
  47. }
  48. }
  49. public playClip (clip: AudioClip, loop:boolean=true) {
  50. this.getSound().stop()
  51. this.getSound().loop = loop;
  52. this.getSound().clip = clip;
  53. if(gameManager.getUserData().isOpenYinYue){
  54. this.getSound().play()
  55. }
  56. }
  57. public stopMusic(){
  58. this.getSound().pause()
  59. }
  60. public play_music(){
  61. if(this.getSound().clip!=null){
  62. this.playClip(this.getSound().clip);
  63. }else{
  64. this.play_sys_music()
  65. }
  66. }
  67. public play_sys_music() {
  68. gameManager.Singleton.getSceneManager().playMusic(gameManager.getSysData().content.sys_piped_music,true)
  69. }
  70. getSound(){
  71. if(this.node.getComponent(AudioSource)!=null){
  72. return this.node.getComponent(AudioSource)
  73. }
  74. return this.node.addComponent(AudioSource)
  75. }
  76. public getSceneData(){
  77. // if(this.mSceneData.att==null){
  78. // if(this.mSceneData.page_list[0].page_list.length>0){
  79. // return this.mSceneData.page_list[0].page_list[0];
  80. // }
  81. // return this.mSceneData.page_list[0];
  82. // }
  83. let data = this.mSceneData.page_list[0];
  84. // while (data.page_list.length>0) {
  85. // data = data.page_list[0]
  86. // }
  87. return data;
  88. }
  89. public GetPageList():scene_item_data[]{
  90. return this.mPageList;
  91. }
  92. public startLevelGame(data: scene_item_data){
  93. this.mSceneTask = data._task_data;
  94. this.mPageList = data.page_list;
  95. this.mSceneData = data;
  96. if(data._task_data==null){
  97. return tools.showToast("场景任务没有配置!")
  98. }
  99. if(this.mPageList.length<=0){
  100. throw "不可以为空"
  101. }else{
  102. this.mGameManager.loadSceneTask(data._task_data)
  103. this.mGameManager.loadScene(this.mPageList,data.type)
  104. for (let index = 0; index < data.page_list.length; index++) {
  105. const element = data.page_list[index];
  106. this.mGameManager.loadUi(gameManager.getUIWidgetList(element))
  107. this.mGameManager.loadQuestion(gameManager.getQuestionwidgetList(element))
  108. this.mGameManager.loadTextSound(gameManager.getTextSoundWidgetList(element))
  109. this.mGameManager.initCountDownList(gameManager.getCountDownWidgetList(element))
  110. }
  111. this.mGameManager.initTaskUi(data._task_data)
  112. }
  113. }
  114. }