scene_layer.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { _decorator, AudioSource, Component, instantiate, Node, Prefab, Widget } from 'cc';
  2. import { event_item, scene_item_data } from '../../data/data';
  3. import { scene_page } from './scene_page';
  4. import { scene_page_dir } from './scene_page_dir';
  5. import { ClientEvent } from '../clientEvent';
  6. import { config } from '../config';
  7. import { gameManager } from './gameManager';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('scene_layer')
  10. export class scene_layer extends Component {
  11. @property(Node) content:Node = null;
  12. @property(Prefab) scenePagePrefab:Prefab = null;
  13. @property(Node) scenePageDir:Node = null;
  14. protected mScenePages:scene_item_data[] =[]
  15. private mCurPage:number = 0;
  16. protected start(): void {
  17. this.node.addComponent(AudioSource)
  18. }
  19. public initScene(pages:scene_item_data[],type:number){
  20. ClientEvent.on(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
  21. this.mScenePages = pages;
  22. this.initSceneDir(type)
  23. this.loadAllPage()
  24. }
  25. public unInit(){
  26. this.content.destroyAllChildren()
  27. this.content.removeAllChildren()
  28. this.mScenePages = []
  29. this.mCurPage = 0;
  30. ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
  31. }
  32. getAudioSource(){
  33. if(this.node.getComponent(AudioSource)==null){
  34. this.node.addComponent(AudioSource)
  35. }
  36. return this.node.getComponent(AudioSource)
  37. }
  38. beActive(widgetId:number,event:event_item){
  39. if(event.type===config.event_type.play_sound){
  40. let is_can_play = true;
  41. if(gameManager.Singleton.getLevelData()){
  42. if(gameManager.getUserData().isOpenPeiYin){
  43. is_can_play = true;
  44. }else{
  45. is_can_play = false;
  46. }
  47. }
  48. if(is_can_play){
  49. if(event.event_item_play_sound_data!=null){
  50. this.getAudioSource().clip = gameManager.getCacheSoundByName(event.event_item_play_sound_data.sound_res.url);
  51. this.getAudioSource().play();
  52. this.getAudioSource().volume = 1;
  53. }
  54. }
  55. }
  56. }
  57. changePage(page:number){
  58. this.mCurPage = page;
  59. this.onChangeUpdatePages()
  60. if(this.mScenePages.length>1){
  61. this.scenePageDir.getComponent(scene_page_dir).showAllBtn()
  62. if(this.mCurPage<=0){
  63. this.scenePageDir.getComponent(scene_page_dir).hideUpBtn()
  64. }
  65. if(this.mCurPage==this.mScenePages.length-1){
  66. this.scenePageDir.getComponent(scene_page_dir).hideNextBtn()
  67. }
  68. }
  69. }
  70. onChangeUpdatePages(){
  71. for (let index = 0; index < this.content.children.length; index++) {
  72. const page = this.content.children[index];
  73. page.active = (this.mCurPage == index)
  74. }
  75. }
  76. loadAllPage(){
  77. this.content.removeAllChildren()
  78. for (let index = 0; index < this.mScenePages.length; index++) {
  79. const item_data:scene_item_data = this.mScenePages[index];
  80. let item:Node = instantiate(this.scenePagePrefab);
  81. item.parent = this.content;
  82. item.getComponent(Widget).target = this.content;
  83. item.getComponent(scene_page).initView(item_data)
  84. }
  85. this.changePage(0)
  86. }
  87. onUp(){
  88. let up_page = this.mCurPage -1;
  89. if(up_page>=0){
  90. gameManager.Singleton.onLaunch(()=>{
  91. this.changePage(up_page)
  92. })
  93. }
  94. }
  95. onNext(){
  96. let next_page = this.mCurPage +1;
  97. if(next_page<this.mScenePages.length){
  98. gameManager.Singleton.onLaunch(()=>{
  99. this.changePage(next_page)
  100. })
  101. }
  102. }
  103. initSceneDir(type:number){
  104. if(this.mScenePages.length>1){
  105. this.scenePageDir.getComponent(scene_page_dir).initView(type,this.onUp.bind(this),this.onNext.bind(this))
  106. }else{
  107. this.scenePageDir.getComponent(scene_page_dir).hideAllBtn()
  108. }
  109. }
  110. }