sceneManager.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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);
  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) {
  50. this.getSound().stop()
  51. this.getSound().loop = true;
  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. gameManager.Singleton.getSceneManager().playMusic(gameManager.getSysData().content.sys_piped_music,true)
  65. }
  66. }
  67. getSound(){
  68. if(this.node.getComponent(AudioSource)!=null){
  69. return this.node.getComponent(AudioSource)
  70. }
  71. return this.node.addComponent(AudioSource)
  72. }
  73. public getSceneData(){
  74. // if(this.mSceneData.att==null){
  75. // if(this.mSceneData.page_list[0].page_list.length>0){
  76. // return this.mSceneData.page_list[0].page_list[0];
  77. // }
  78. // return this.mSceneData.page_list[0];
  79. // }
  80. let data = this.mSceneData.page_list[0];
  81. // while (data.page_list.length>0) {
  82. // data = data.page_list[0]
  83. // }
  84. return data;
  85. }
  86. public startLevelGame(data: scene_item_data){
  87. this.mSceneTask = data._task_data;
  88. this.mPageList = data.page_list;
  89. this.mSceneData = data;
  90. if(data._task_data==null){
  91. return tools.showToast("场景任务没有配置!")
  92. }
  93. if(this.mPageList.length<=0){
  94. throw "不可以为空"
  95. }else{
  96. this.stopMusic()
  97. this.mGameManager.loadSceneTask(data._task_data)
  98. this.mGameManager.loadScene(this.mPageList,data.type)
  99. for (let index = 0; index < data.page_list.length; index++) {
  100. const element = data.page_list[index];
  101. this.mGameManager.loadUi(gameManager.getUIWidgetList(element))
  102. this.mGameManager.loadQuestion(gameManager.getQuestionwidgetList(element))
  103. this.mGameManager.loadTextSound(gameManager.getTextSoundWidgetList(element))
  104. this.mGameManager.initCountDownList(gameManager.getCountDownWidgetList(element))
  105. }
  106. this.mGameManager.initTaskUi(data._task_data)
  107. }
  108. }
  109. }