SelectScene.ts 5.8 KB

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