SelectScene.ts 5.7 KB

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