SelectScene.ts 6.2 KB

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