SelectScene.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { _decorator, Component, Node ,Button,director, PageView, NodeEventType, Prefab, instantiate, Animation, Vec3} from 'cc';
  2. import { GameInfo } from './GameInfo';
  3. import { resMgr } from './Frames/ResourcesMgr';
  4. import { dataMgr } from './Frames/DataManager';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('SelectScene')
  7. export class SelectScene extends Component {
  8. @property(Node)
  9. pageView: Node = null;
  10. @property(Prefab)
  11. effectPrefab: Prefab = null;
  12. @property(Node)
  13. selectLv: Node = null;
  14. @property(Node)
  15. selectTheme: Node = null;
  16. currentPageIndex:number = null;
  17. totalPages:number = null;
  18. //解锁的主题数
  19. unLockedTheme:number = null;
  20. //锁的图标
  21. locked: Node = null;
  22. btnRight: Node = null;
  23. btnLeft: Node = null;
  24. start() {
  25. // console.log("resMgr:" + resMgr)
  26. // console.log("dataMgr:" + dataMgr.getAllDataByName("GameResPath"));
  27. //总页数
  28. this.totalPages = this.pageView.getComponent(PageView).getPages().length;
  29. //当前索引值
  30. this.currentPageIndex = this.pageView.getComponent(PageView).getCurrentPageIndex();
  31. const buttonConfigs = [
  32. {name: "SelectTheme/UI_SelectTheme/BtnHome" , handler : this.onBtnHome},
  33. {name: "UIPublic/BtnHelp" , handler : this.onBtnHelp},
  34. {name: "SelectTheme/UI_SelectTheme/BtnLeft" , handler : this.onBtnLeft},
  35. {name: "SelectTheme/UI_SelectTheme/BtnRight" , handler : this.onBtnRight}
  36. ]
  37. buttonConfigs.forEach(config =>{
  38. let button = this.node.getChildByPath(config.name);
  39. if(button){
  40. button.on(Button.EventType.CLICK,config.handler,this);
  41. } else{
  42. console.warn(`Button not found: ${config.name}`);
  43. }
  44. //button.on(Button.EventType.CLICK,config.handler,this);
  45. })
  46. // let BtnHome = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnHome");
  47. // BtnHome.on(Button.EventType.CLICK,this.onBtnHome,this);
  48. // let BtnHelp = this.node.getChildByPath("UIPublic/BtnHelp");
  49. // BtnHelp.on(Button.EventType.CLICK,this.onBtnHelp,this);
  50. // let BtnLeft = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnLeft");
  51. // BtnLeft.on(Button.EventType.CLICK,this.onBtnLeft,this);
  52. // let BtnRight = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnRight");
  53. // BtnRight.on(Button.EventType.CLICK,this.onBtnRight,this);
  54. let touchArea = this.node.getChildByPath("SelectTheme/TouchArea");
  55. touchArea.on(NodeEventType.TOUCH_START,this.onTouchStart,this);
  56. touchArea.on(NodeEventType.TOUCH_END,this.onTouchEnd,this);
  57. this.node.on(Node.EventType.MOUSE_DOWN,this.onMouseTouchStart,this);
  58. this.unLockedTheme = GameInfo.Instance.UnLockTheme;
  59. this.locked = this.node.getChildByPath('SelectTheme/UI_SelectTheme/Locked');
  60. this.btnRight = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnRight");
  61. this.btnLeft = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnLeft");
  62. }
  63. //返回开始游戏场景
  64. onBtnHome(){
  65. director.loadScene("StartScene")
  66. }
  67. //帮助按钮
  68. onBtnHelp(){
  69. if(!this.node.getChildByName('Help').active){
  70. this.node.getChildByName("Help").active = true;
  71. }
  72. }
  73. onBtnLeft(){
  74. this.currentPageIndex = Math.max(0, this.currentPageIndex - 1);
  75. this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
  76. }
  77. onBtnRight(){
  78. if (this.currentPageIndex === this.totalPages - 1) {
  79. this.currentPageIndex = this.totalPages - 1; // 保持在最后一页
  80. } else {
  81. this.currentPageIndex = Math.min(this.currentPageIndex + 1, this.totalPages -1);
  82. }
  83. this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
  84. }
  85. onTouchStart(){
  86. GameInfo.Instance.CurTheme = this.currentPageIndex + 1;
  87. if(this.locked.active){
  88. console.log("当前主题未解锁");
  89. }
  90. }
  91. onTouchEnd(){
  92. //当前主题未解锁不可进入选关
  93. if(this.locked.active){
  94. return;
  95. }
  96. //当前主题解锁了,则可以进入选关
  97. if(!this.selectLv.active){
  98. this.selectLv.active = true;
  99. if(this.selectTheme.active){
  100. this.selectTheme.active = false;
  101. }
  102. }
  103. }
  104. //点击特效
  105. onMouseTouchStart(event){
  106. const touchPos = event.getUILocation();
  107. const effectNode = instantiate(this.effectPrefab);
  108. this.node.addChild(effectNode);
  109. //effectNode.setWorldPosition(new Vec3(touchPos.x,touchPos.y));
  110. effectNode.setPosition(new Vec3(touchPos.x - 480,touchPos.y - 320));
  111. const effectCom = effectNode.getComponent(Animation);
  112. if(effectCom){
  113. effectCom.play();
  114. }
  115. this.scheduleOnce(()=>{ effectNode.destroy(); },0.5);
  116. }
  117. update(deltaTime: number) {
  118. this.updateButton();
  119. }
  120. private updateButton(){
  121. if(this.btnLeft && this.btnRight){
  122. if(this.currentPageIndex === this.totalPages - 1 ){
  123. this.btnLeft.active = true;
  124. this.btnRight.active = false;
  125. }
  126. else if(this.currentPageIndex === 0){
  127. this.btnLeft.active = false;
  128. this.btnRight.active = true;
  129. }else{
  130. this.btnLeft.active = true;
  131. this.btnRight.active = true;
  132. }
  133. }
  134. if(this.locked){
  135. if(this.currentPageIndex < this.unLockedTheme){
  136. if(this.locked.active){
  137. this.locked.active = false;
  138. }
  139. } else {
  140. if(!this.locked.active){
  141. this.locked.active = true;
  142. }
  143. }
  144. }
  145. }
  146. }