Main.ts 5.4 KB

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