Main.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { _decorator, EventTouch, Node, Label,Animation, Vec3, EditBox} from 'cc';
  2. import List from '../third/List';
  3. import { Utils } from '../utils/Utils';
  4. import MsgHints from '../utils/MsgHints';
  5. import { userIns } from '../data/UserData';
  6. import { BaseExp } from '../core/base/BaseExp';
  7. import { autoBind } from '../extend/AutoBind';
  8. import { Constants } from '../data/Constants';
  9. import { uiMgr } from '../core/manager/UIManager';
  10. import { TaskItem } from '../items/item/TaskItem';
  11. import { stateMgr } from '../core/manager/StateManager';
  12. import { Game } from '../game/Game';
  13. import i18n from '../core/i18n/runtime-scripts/LanguageData';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('Main')
  16. export class Main extends BaseExp {
  17. @autoBind(EditBox)
  18. public editBox: EditBox = null!;
  19. @autoBind({ type: Label, tooltip: "金币文本" })
  20. public gold_lable: Label;
  21. @autoBind({ type: Label, tooltip: "钻石文本" })
  22. public diamond_lable: Label;
  23. @autoBind({ type: Label, tooltip: "任务描述" })
  24. public task_des_lable: Label;
  25. @autoBind({ type: Label, tooltip: "关卡等级" })
  26. public level_lable: Label;
  27. @autoBind({ type: List, tooltip: "任务数据" })
  28. public task_scrollView: List;
  29. @property({ type: Animation, tooltip: "进入游戏动画" })
  30. public enterAnim: Animation;
  31. @property({ type: [Node], tooltip: "动画位置节点数组" })
  32. public animPosArr: Array<Node> = [];
  33. //所有主要页面
  34. private sceneNames: Array<string> = [Constants.mainUIs.main];
  35. //tabBars数组下标
  36. public _tabBarIndex: number = -1;
  37. public set tabBarIndex(index: number) {
  38. this.cutTabBarIndex(null,index);
  39. }
  40. public get tabBarIndex(){
  41. return this._tabBarIndex;
  42. }
  43. //保存点击的页面
  44. private preIndexs: Array<number> = [0];
  45. //任务数据
  46. private taskDatas: Array<any> = [];
  47. //属性存储初始位置
  48. private originalPositions: Vec3[] = [];
  49. start() {
  50. this.hasAnim = false;
  51. this.closeOnBlank = false;
  52. //存储初始位置到类属性
  53. this.originalPositions = this.animPosArr.map(e => e.position.clone());
  54. uiMgr.panels.set(Constants.mainUIs.main, this.node);
  55. //注册动态变化值
  56. stateMgr.registerUI(Constants.gold, this.gold_lable);
  57. stateMgr.registerUI(Constants.diamond, this.diamond_lable);
  58. //加载数据
  59. this.loadData();
  60. //注册事件
  61. this.node.on(Node.EventType.TOUCH_START, this.playBtn.bind(this), this);
  62. if(!Constants.isDebug){
  63. this.editBox.node.active = false;
  64. }
  65. }
  66. /**
  67. * 加载数据
  68. */
  69. public loadData() {
  70. const data: any = userIns.getCurLevelData();
  71. if(!data)return;
  72. //设置任务描述关卡任务:%{value}
  73. this.task_des_lable.string = i18n.isZh ? data.task : data.task_lang;
  74. this.level_lable.string = i18n("main.关卡任务:%{value}",{value: data.id});
  75. this.taskDatas = []
  76. //拆分并处理每个敌人配置
  77. data.dispose.split('|').forEach((item: string) => {
  78. const [enemyId, count] = item.split('_').map(Number);
  79. const enemyData = Utils.clone(userIns.enemyTable).find((e: any) => e.id === enemyId);
  80. enemyData.count = count;
  81. this.taskDatas.push(enemyData);
  82. });
  83. if(this.taskDatas.length <= 0)return;
  84. this.task_scrollView.numItems = this.taskDatas.length;
  85. }
  86. /**
  87. * 任务数据
  88. * @param item item节点
  89. * @param idx 数据下标
  90. */
  91. public setTaskItemData(item: Node, idx: number) {
  92. const task: any = this.taskDatas[idx];
  93. item.getComponent(TaskItem).init(task);
  94. }
  95. /**
  96. * 开始游戏
  97. */
  98. public playBtn(){
  99. if(Constants.isDebug && !Utils.isNull(this.editBox.string)){
  100. userIns.data.level = parseInt(this.editBox.string);
  101. }
  102. this.enterAnim.play();
  103. this.scheduleOnce(() => {
  104. Game.I.isPause = false;
  105. Game.I.isGameOver = false;
  106. uiMgr.hide(Constants.mainUIs.main,()=>{
  107. uiMgr.show(Constants.popUIs.gunfightShootUI);
  108. });
  109. //加载敌人
  110. Game.I.buildEnemys.loadLevelEnemys();
  111. },0.4)
  112. }
  113. /**
  114. * 点击事件
  115. * @param event 事件
  116. * @param customEventData 数据
  117. */
  118. public onBtnClicked(event: EventTouch, customEventData: any) {
  119. super.onBtnClicked(event,customEventData);
  120. let btnName = event.target.name;
  121. if (btnName === 'set_btn') { // 设置页面
  122. uiMgr.show(Constants.popUIs.settingUI);
  123. }else if (btnName === 'shop_btn') {//商店
  124. uiMgr.show(Constants.popUIs.storeUI);
  125. }else if (btnName === 'arsenal_btn') {//武器库
  126. uiMgr.show(Constants.popUIs.arsenalUI);
  127. }else if(btnName === 'boomerang_btn'){//扔飞镖
  128. uiMgr.show(Constants.popUIs.tossBoomerangUI);
  129. }else if(btnName === 'ads_free_btn'){//免广告按钮
  130. MsgHints.show('免广告按钮');
  131. }else if(btnName === 'bulls_eye_btn'){//准心开始按钮
  132. this.playBtn();
  133. }
  134. }
  135. /**
  136. * 切换游戏主页面
  137. * @param idx
  138. */
  139. public cutTabBarIndex(event: EventTouch,param: any){
  140. let index: number = parseInt(param);
  141. this._tabBarIndex = index;
  142. this.preIndexs.push(index);
  143. //设置层级
  144. let path: string = this.sceneNames[index];
  145. if(path == Constants.mainUIs.main){
  146. this.animPosArr.forEach((e, index) => e.position = this.originalPositions[index]);
  147. }
  148. //加载页面
  149. uiMgr.show(path);
  150. if(this.preIndexs.length > 2){
  151. this.preIndexs.shift()
  152. }
  153. }
  154. }