Main.ts 7.8 KB

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