Main.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { _decorator, EventTouch, Node, Label,Animation, Vec3, EditBox, Tween, tween, easing} 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. import { audioMgr } from '../core/manager/AudioManager';
  15. const { ccclass, property } = _decorator;
  16. @ccclass('Main')
  17. export class Main extends BaseExp {
  18. @autoBind(EditBox)
  19. public editBox: EditBox = null!;
  20. @autoBind({ type: Label, tooltip: "金币文本" })
  21. public gold_lable: Label;
  22. @autoBind({ type: Label, tooltip: "钻石文本" })
  23. public diamond_lable: Label;
  24. @autoBind({ type: Label, tooltip: "任务描述" })
  25. public task_des_lable: Label;
  26. @autoBind({ type: Label, tooltip: "关卡等级" })
  27. public level_lable: Label;
  28. @autoBind({ type: List, tooltip: "任务数据" })
  29. public task_scrollView: List;
  30. @property({ type: [Node], tooltip: "武器按钮、飞镖按钮、任务列表界面 动画位置节点数组" })
  31. public animPosArr: Array<Node> = [];
  32. //所有主要页面
  33. private sceneNames: Array<string> = [Constants.mainUIs.main];
  34. //tabBars数组下标
  35. public _tabBarIndex: number = -1;
  36. public set tabBarIndex(index: number) {
  37. this.cutTabBarIndex(null,index);
  38. }
  39. public get tabBarIndex(){
  40. return this._tabBarIndex;
  41. }
  42. //保存点击的页面
  43. private preIndexs: Array<number> = [0];
  44. //任务数据
  45. private taskDatas: Array<any> = [];
  46. //属性存储初始位置
  47. private originalPositions: Vec3[] = [];
  48. //是否重新启动加载地图
  49. private isLoad: boolean = false;
  50. //关卡背景音乐
  51. private audio_BGM: string = '';
  52. start() {
  53. this.hasAnim = false;
  54. this.closeOnBlank = false;
  55. uiMgr.panels.set(Constants.mainUIs.main, this.node);
  56. //注册动态变化值
  57. stateMgr.registerUI(Constants.gold, this.gold_lable);
  58. stateMgr.registerUI(Constants.diamond, this.diamond_lable);
  59. //加载数据
  60. this.loadData();
  61. //注册事件
  62. this.node.on(Node.EventType.TOUCH_START, this.playBtn.bind(this), this);
  63. if(!Constants.isDebug){
  64. this.editBox.node.active = false;
  65. }
  66. }
  67. /**
  68. * 激活时的位置信息
  69. */
  70. protected onEnable(): void {
  71. //存储初始位置到类属性
  72. this.originalPositions = this.animPosArr.map(e => e.position.clone());
  73. }
  74. /**
  75. * 进入的时候的动画
  76. */
  77. public enterAnim(){
  78. const task_view_pos: Vec3 = this.originalPositions[0];
  79. const arsenal_btn_pos: Vec3 = this.originalPositions[1];
  80. const boomerang_btn_pos: Vec3 = this.originalPositions[2];
  81. let posArr:Vec3[] = [
  82. new Vec3(task_view_pos.x,task_view_pos.y + 622,task_view_pos.z),
  83. new Vec3(arsenal_btn_pos.x - 148,arsenal_btn_pos.y,arsenal_btn_pos.z),
  84. new Vec3(boomerang_btn_pos.x + 148,boomerang_btn_pos.y,boomerang_btn_pos.z)
  85. ];
  86. this.animPosArr.forEach((e: Node,i: number) => {
  87. tween(e)
  88. .to(0.7, { position: posArr[i]}, { easing: easing.backOut})
  89. .start();
  90. });
  91. }
  92. /**
  93. * 加载数据
  94. */
  95. public loadData() {
  96. const data: any = userIns.getCurLevelData();
  97. if(!data)return;
  98. //设置任务描述关卡任务:%{value}
  99. this.task_des_lable.string = i18n.isZh ? data.task : data.task_lang;
  100. this.level_lable.string = i18n("main.关卡任务:%{value}",{value: data.id});
  101. this.taskDatas = []
  102. //拆分并处理每个敌人配置
  103. data.dispose.split('|').forEach((item: string) => {
  104. const [enemyId, count] = item.split('_').map(Number);
  105. const enemyData = Utils.clone(userIns.enemyTable).find((e: any) => e.id === enemyId);
  106. enemyData.count = count;
  107. this.taskDatas.push(enemyData);
  108. });
  109. if(this.taskDatas.length <= 0)return;
  110. this.task_scrollView.numItems = this.taskDatas.length;
  111. }
  112. /**
  113. * 任务数据
  114. * @param item item节点
  115. * @param idx 数据下标
  116. */
  117. public setTaskItemData(item: Node, idx: number) {
  118. const task: any = this.taskDatas[idx];
  119. item.getComponent(TaskItem).init(task);
  120. }
  121. /**
  122. * 暂停关卡内的背景音乐
  123. */
  124. public playOrStopBGM(isPlay: boolean){
  125. if(!this.audio_BGM)return;
  126. if(isPlay){
  127. audioMgr.play(this.audio_BGM,true);
  128. }else{
  129. audioMgr.stop(this.audio_BGM);
  130. }
  131. }
  132. /**
  133. * 开始游戏
  134. */
  135. public playBtn(){
  136. if(this.isLoad){
  137. Game.I.loadMap();
  138. }
  139. //播放关卡背景音乐
  140. let bgms:string[] = [Constants.audios.bgm_1,Constants.audios.bgm_2,Constants.audios.bgm_3];
  141. this.audio_BGM = Utils.randomArray(bgms)[0];
  142. audioMgr.play(this.audio_BGM,true);
  143. Game.I.player.restart();
  144. if(Constants.isDebug && !Utils.isNull(this.editBox.string)){
  145. userIns.data.level = parseInt(this.editBox.string);
  146. }
  147. this.isLoad = true;
  148. this.enterAnim();
  149. this.scheduleOnce(() => {
  150. //加载敌人
  151. Game.I.buildEnemys.loadLevelEnemys();
  152. uiMgr.hide(Constants.mainUIs.main,()=>{
  153. uiMgr.show(Constants.popUIs.gunfightShootUI);
  154. });
  155. },0.4)
  156. }
  157. /**
  158. * 点击事件
  159. * @param event 事件
  160. * @param customEventData 数据
  161. */
  162. public onBtnClicked(event: EventTouch, customEventData: any) {
  163. super.onBtnClicked(event,customEventData);
  164. let btnName = event.target.name;
  165. if (btnName === 'set_btn') { // 设置页面
  166. uiMgr.show(Constants.popUIs.settingUI);
  167. }else if (btnName === 'shop_btn') {//商店
  168. uiMgr.show(Constants.popUIs.storeUI);
  169. }else if (btnName === 'arsenal_btn') {//武器库
  170. uiMgr.show(Constants.popUIs.arsenalUI);
  171. }else if(btnName === 'boomerang_btn'){//扔飞镖
  172. uiMgr.show(Constants.popUIs.tossBoomerangUI);
  173. }else if(btnName === 'ads_free_btn'){//免广告按钮
  174. MsgHints.show('免广告按钮');
  175. }else if(btnName === 'bulls_eye_btn'){//准心开始按钮
  176. this.playBtn();
  177. }
  178. }
  179. /**
  180. * 切换游戏主页面
  181. * @param idx
  182. */
  183. public cutTabBarIndex(event: EventTouch,param: any){
  184. let index: number = parseInt(param);
  185. this._tabBarIndex = index;
  186. this.preIndexs.push(index);
  187. //设置层级
  188. let path: string = this.sceneNames[index];
  189. if(path == Constants.mainUIs.main){
  190. this.animPosArr.forEach((e, index) => e.position = this.originalPositions[index]);
  191. }
  192. //加载页面
  193. uiMgr.show(path);
  194. if(this.preIndexs.length > 2){
  195. this.preIndexs.shift()
  196. }
  197. }
  198. }