mainView.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import { SaveUtil } from "../help/SaveUtil";
  2. import EventMgr from "../game/EventMgr";
  3. import { GameEvent } from "../game/GameEvent";
  4. import { GameMode } from "../game/GameMode";
  5. import { GameState } from "../game/GameState";
  6. import Main from "../game/Main";
  7. import { SDKIpt } from "../game/SDKIpt";
  8. import SoundMgr from "../game/SoundMgr";
  9. import rankPanel from "./rankPanel";
  10. import { SDK as sdk} from '../game/sdk';
  11. import Button from "../game/Button";
  12. const {ccclass, property} = cc._decorator;
  13. @ccclass
  14. export default class mainView extends cc.Component {
  15. @property({type:rankPanel})
  16. private rankPanel:rankPanel = null;
  17. /**开始游戏 */
  18. @property(cc.Button)
  19. returnMainView: cc.Button = null;
  20. @property(cc.Button)
  21. returnPlay: cc.Button = null;
  22. @property(cc.Button)
  23. returnReopen: cc.Button = null;
  24. @property(cc.Button)
  25. btnNormalMode: cc.Button = null;
  26. @property(cc.Button)
  27. btnEndlessMode: cc.Button = null;
  28. @property(cc.Button)
  29. btnAudio: cc.Button = null;
  30. @property(cc.Button)
  31. btnMusic: cc.Button = null;
  32. @property(cc.Slider)
  33. sliderAudio:cc.Slider = null
  34. @property(cc.Slider)
  35. sliderMusic:cc.Slider = null
  36. @property(cc.Label)
  37. percentAudioLabel:cc.Label = null
  38. @property(cc.Label)
  39. percentMusicLabel:cc.Label = null
  40. @property(cc.Button)
  41. btnRank: cc.Button = null;
  42. @property(cc.Button)
  43. btnSetting: cc.Button = null;
  44. @property(cc.Sprite)
  45. userAvatar: cc.Sprite = null;
  46. @property(cc.Label)
  47. userName: cc.Label = null;
  48. @property([cc.Node])
  49. private mainNodes: cc.Node[] = [];
  50. // @property(cc.Node)
  51. // private confirnReturnMainPanel: cc.Node = null;
  52. // @property(cc.Button)
  53. // private returnMainConfirn: cc.Button = null;
  54. // @property(cc.Button)
  55. // private returnMainCancel: cc.Button = null;
  56. @property(cc.Node)
  57. private mainPanel: cc.Node = null;
  58. @property(cc.Node)
  59. private pausePanel: cc.Node = null;
  60. @property(cc.Node)
  61. private settingPanel: cc.Node = null;
  62. @property(cc.Node)
  63. private gameRank:cc.Node = null
  64. private get isActive(): boolean { return this.node.active; }
  65. private rankState: number = 0;
  66. private gamePauseIndex: number = 0;
  67. start () {
  68. let me =this;
  69. // this.btnAudio.node.on("click",function(){
  70. // SoundMgr.Instance.ToogleEffectMute();
  71. // me.btnAudio.node.getChildByName("closeTag").active = SoundMgr.Instance.EffMute;
  72. // },this);
  73. // this.btnMusic.node.on("click",function(){
  74. // SoundMgr.Instance.ToogleMusicMute();
  75. // me.btnMusic.node.getChildByName("closeTag").active = SoundMgr.Instance.MusicMute;
  76. // },this);
  77. this.btnRank.node.on("click",function(){
  78. // if(me.rankState % 3 == 0){
  79. // me.rankPanel.node.active = true;
  80. // }else if(me.rankState % 3 == 1){
  81. // me.rankPanel.ToggleShow();
  82. // }else if(me.rankState % 3 == 2){
  83. // me.rankPanel.node.active = false;
  84. // }
  85. // me.rankState++;
  86. this.node.active = false;
  87. this.gameRank.getComponent('gameRank').openRankDialog()
  88. // this.rankPanel.node.active = !this.rankPanel.node.active;
  89. },this);
  90. this.returnMainView.node.on("click",()=>{
  91. // for(let item of this.pausePanel.children){
  92. // console.log(item.getComponent(Button).selected());
  93. // }
  94. this._ButtonPauseActive(this.gamePauseIndex=2)
  95. EventMgr.Instance.dispatch_event(GameEvent.SDK_UI_IPT,SDKIpt.rightFoot);
  96. });
  97. this.returnPlay.node.on("click",function(){
  98. this._ButtonPauseActive(this.gamePauseIndex=0)
  99. EventMgr.Instance.dispatch_event(GameEvent.SDK_UI_IPT,SDKIpt.rightFoot);
  100. },this);
  101. this.returnReopen.node.on("click",function(){
  102. this._ButtonPauseActive(this.gamePauseIndex=1)
  103. EventMgr.Instance.dispatch_event(GameEvent.SDK_UI_IPT,SDKIpt.rightFoot);
  104. },this);
  105. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function(event){
  106. if (!this.isActive) { return; }
  107. switch (event.keyCode) {
  108. case cc.macro.KEY.num4:
  109. if (Main.Ins.State == GameState.inMain){
  110. Main.Ins.Mode = Main.Ins.Mode===GameMode.normal?GameMode.endless:GameMode.normal
  111. this._ButtonMainActive(Number(Main.Ins.Mode!==GameMode.normal));
  112. Main.Ins.Map.changeMap(Number(Main.Ins.Mode===GameMode.normal));
  113. }
  114. if (Main.Ins.State == GameState.inGame) {
  115. this.gamePauseIndex = this.gamePauseIndex>0?this.gamePauseIndex-1:2
  116. this._ButtonPauseActive(this.gamePauseIndex)
  117. }
  118. break;
  119. case cc.macro.KEY.num1:
  120. if (Main.Ins.State == GameState.inMain){
  121. Main.Ins.Mode = Main.Ins.Mode===GameMode.endless?GameMode.normal:GameMode.endless
  122. this._ButtonMainActive(Number(Main.Ins.Mode===GameMode.endless));
  123. Main.Ins.Map.changeMap(Number(Main.Ins.Mode!==GameMode.endless));
  124. }
  125. if (Main.Ins.State == GameState.inGame) {
  126. this.gamePauseIndex = this.gamePauseIndex<2?this.gamePauseIndex+1:0
  127. this._ButtonPauseActive(this.gamePauseIndex)
  128. }
  129. break;
  130. default:
  131. break;
  132. }
  133. }, this);
  134. this.btnNormalMode.node.on("click",function(){
  135. Main.Ins.Mode = GameMode.normal;
  136. this._ButtonMainActive(0);
  137. Main.Ins.Map.changeMap(1);
  138. Main.Ins.PlayGame();
  139. },this);
  140. this.btnEndlessMode.node.on("click",function(){
  141. Main.Ins.Mode = GameMode.endless;
  142. this._ButtonMainActive(1);
  143. Main.Ins.Map.changeMap(0);
  144. Main.Ins.PlayGame();
  145. },this);
  146. // 音效设置点击事件
  147. this.btnSetting.node.on("click",function(){
  148. me.settingPanel.active = true; //显示
  149. },this);
  150. this.settingPanel.getChildByName("settingConfirm").on("click",function(){
  151. me.settingPanel.active = false; //隐藏
  152. },this);
  153. // this.returnMainConfirn.node.on("click",function(){
  154. // EventMgr.Instance.dispatch_event(GameEvent.SDK_UI_IPT,SDKIpt.rightFoot);
  155. // },this);
  156. // this.returnMainCancel.node.on("click",function(){
  157. // EventMgr.Instance.dispatch_event(GameEvent.SDK_UI_IPT,SDKIpt.leftFoot);
  158. // },this);
  159. this.setAudioVolume({progress:Number(SaveUtil.get("music_volume", "1"))},'audio')
  160. this.setAudioVolume({progress:Number(SaveUtil.get("effect_volume", "1"))},'music')
  161. this.setSliderProgress(Number(SaveUtil.get("music_volume", "1")),'audio')
  162. this.setSliderProgress(Number(SaveUtil.get("effect_volume", "1")),'music')
  163. // this.btnAudio.node.getChildByName("closeTag").active = SoundMgr.Instance.EffMute;
  164. // this.btnMusic.node.getChildByName("closeTag").active = SoundMgr.Instance.MusicMute;
  165. EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  166. this.initQuitPanel();
  167. this.rankPanel.Hide();
  168. this._ButtonMainActive(0);
  169. sdk.getUserInfo((info) => {
  170. cc.assetManager.loadRemote<cc.Texture2D>(info['avatar'], function (error, texture) {
  171. me.userAvatar.spriteFrame = new cc.SpriteFrame(texture);
  172. });
  173. me.userName.string = info['nickname'];
  174. });
  175. // sdk.on(CMD.BACK_LEFT.toString(), , this);
  176. }
  177. ON_SDK_UI_IPT(SDK_UI_IPT: GameEvent, ipt: SDKIpt, ON_SDK_UI_IPT: any) {
  178. console.error("mainview 收到 sdk 动作:"+ipt+" 界面是否激活:"+this.isActive + " " + Main.Ins.State);
  179. if (!this.isActive) { return; }
  180. switch (ipt) {
  181. case SDKIpt.left:
  182. if (Main.Ins.State == GameState.inGame) {
  183. this.gamePauseIndex = this.gamePauseIndex>0?this.gamePauseIndex-1:2
  184. this._ButtonPauseActive(this.gamePauseIndex)
  185. }
  186. else if (Main.Ins.State == GameState.inMain) {
  187. Main.Ins.Mode = Main.Ins.Mode===GameMode.normal?GameMode.endless:GameMode.normal
  188. this._ButtonMainActive(Number(Main.Ins.Mode!==GameMode.normal));
  189. Main.Ins.Map.changeMap(Number(Main.Ins.Mode===GameMode.normal));
  190. }
  191. break;
  192. case SDKIpt.right:
  193. if (Main.Ins.State == GameState.inGame) {
  194. this.gamePauseIndex = this.gamePauseIndex<2?this.gamePauseIndex+1:0
  195. this._ButtonPauseActive(this.gamePauseIndex)
  196. }
  197. else if (Main.Ins.State == GameState.inMain) {
  198. Main.Ins.Mode = Main.Ins.Mode===GameMode.endless?GameMode.normal:GameMode.endless
  199. this._ButtonMainActive(Number(Main.Ins.Mode===GameMode.endless));
  200. Main.Ins.Map.changeMap(Number(Main.Ins.Mode!==GameMode.endless));
  201. }
  202. break;
  203. case SDKIpt.rightFoot://主界面确认进入游戏//y
  204. if (Main.Ins.State == GameState.inGame) {
  205. if (this.pausePanel.active == true){ //继续游戏
  206. if(this.gamePauseIndex===0){
  207. Main.Ins.ReturnPlayGame();
  208. this.node.active = false;
  209. }else if(this.gamePauseIndex===1){
  210. Main.Ins.PlayGame();
  211. this.node.active = false;
  212. }else{
  213. Main.Ins.ShowMainView();
  214. }
  215. // this.onReturnPlay();
  216. }
  217. }
  218. else if (Main.Ins.State == GameState.inMain) {
  219. if (Main.Ins.IsGuide) {return; }
  220. Main.Ins.PlayGame();
  221. }
  222. break;
  223. case SDKIpt.leftFoot:
  224. // if (Main.Ins.State == GameState.inGame) {
  225. // if (this.pausePanel.active == true)//暂停返回大厅
  226. // {
  227. // Main.Ins.ShowMainView();
  228. // }
  229. // }
  230. // else
  231. if (Main.Ins.State == GameState.inMain) {
  232. this.openGameQuitDialog()
  233. // EventMgr.Instance.dispatch_event(GameEvent.SDK_QUIT);
  234. }
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. _ButtonMainActive(index:number){
  241. if(index == 0){
  242. this.btnNormalMode.node.getComponent(Button).selected()
  243. this.btnEndlessMode.node.getComponent(Button).unselected();
  244. }else{
  245. this.btnNormalMode.node.getComponent(Button).unselected()
  246. this.btnEndlessMode.node.getComponent(Button).selected();
  247. }
  248. }
  249. _ButtonPauseActive(index:number){
  250. if(index == 0){
  251. this.returnPlay.node.getComponent(Button).selected();
  252. this.returnReopen.node.getComponent(Button).unselected()
  253. this.returnMainView.node.getComponent(Button).unselected()
  254. }else if(index==1){
  255. this.returnPlay.node.getComponent(Button).unselected();
  256. this.returnReopen.node.getComponent(Button).selected();
  257. this.returnMainView.node.getComponent(Button).unselected()
  258. }else{
  259. this.returnPlay.node.getComponent(Button).unselected();
  260. this.returnReopen.node.getComponent(Button).unselected();
  261. this.returnMainView.node.getComponent(Button).selected()
  262. }
  263. }
  264. public Show(){
  265. this.node.active = true;
  266. }
  267. public Hide(){
  268. this.node.active = false;
  269. }
  270. public ShowPauseGame(){
  271. this.node.active = true;
  272. this._ButtonPauseActive(this.gamePauseIndex=0)
  273. // this.returnPlay.node.active = true;
  274. this.setShowMainNodes(false);
  275. this.setShowPauseNodes(true);
  276. }
  277. public ShowMainGame(){
  278. this.node.active = true;
  279. // this.returnPlay.node.active = false;
  280. this.setShowMainNodes(true);
  281. this.setShowPauseNodes(false);
  282. this.btnEndlessMode.interactable = !Main.Ins.IsGuide;
  283. // this.tmpBtnPlay.node.active =cc.sys.isBrowser;//浏览器界面显示
  284. }
  285. public hideMainPause(){
  286. this.setShowMainNodes(false);
  287. this.setShowPauseNodes(false);
  288. }
  289. private setShowMainNodes(isShow:boolean){
  290. for (let i = 0; i < this.mainNodes.length; i++) {
  291. const element = this.mainNodes[i];
  292. element.active = isShow;
  293. }
  294. this.mainPanel.active = isShow;
  295. }
  296. private setShowPauseNodes(isShow:boolean){
  297. // for (let i = 0; i < this.pauseNodes.length; i++) {
  298. // const element = this.pauseNodes[i];
  299. // element.active = isShow;
  300. // }
  301. this.pausePanel.active = isShow;
  302. }
  303. //设置百分比文本与声音大小
  304. private setAudioVolume(event:any,type:string){
  305. let progress:any = event.progress
  306. if(type==='audio'){
  307. this.percentAudioLabel.string = parseInt((progress*100)+'') + '%'
  308. SoundMgr.Instance.set_music_volume(progress)
  309. }
  310. if(type==='music'){
  311. this.percentMusicLabel.string = parseInt((progress*100)+'') + '%'
  312. SoundMgr.Instance.set_effect_volume(progress)
  313. }
  314. }
  315. //改变声音进度条样式
  316. private setSliderProgress(value,type){
  317. if(type==='audio') this.sliderAudio.progress = value
  318. if(type==='music') this.sliderMusic.progress = value
  319. }
  320. //退出游戏
  321. /**退出游戏 */
  322. @property(cc.Node)
  323. private gameQuit:cc.Node = null
  324. @property(cc.Button)
  325. btnQuit: cc.Button = null;
  326. private initQuitPanel(){
  327. this.btnQuit.node.on("click",this.openGameQuitDialog,this);
  328. }
  329. openGameQuitDialog(){
  330. this.node.active = false;
  331. this.gameQuit.getComponent('gameQuit').openDialog()
  332. }
  333. }