UI_Main.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { _decorator, find, Node } from "cc";
  2. import { ch } from "../../../ch/ch";
  3. import { GameUILayers, gui } from "../../../core/ui/ui";
  4. import ui_base from "../../../core/ui/ui_base";
  5. import { ModuleDef } from "../../../Scripts/ModuleDef";
  6. import { UI_GameRules } from "../UI_GameRules/UI_GameRules";
  7. import { UI_Idioms } from "../UI_Idioms/UI_Idioms";
  8. import { UI_Settings } from "../UI_Settings/UI_Settings";
  9. import { UI_Win } from "../UI_Win/UI_Win";
  10. import { Layout_Main } from "./Layout_Main";
  11. import { Container_Manager } from "../../game/Container_Manager";
  12. import { UI_TimesOver } from "../UI_TimesOver/UI_TimesOver";
  13. import ch_audio from "../../../ch/audio/audio";
  14. import { GameState, Hall } from "../../hall/Hall";
  15. const { ccclass, property } = _decorator;
  16. interface event_protocol {
  17. pause(): void;
  18. resume(): void;
  19. update_remain(): void;
  20. update_ui_idioms(): void;
  21. next_level(): void;
  22. }
  23. @ccclass('UI_Main')
  24. export class UI_Main extends ui_base {
  25. public evt = ch.get_new_event<event_protocol>();
  26. running = true;
  27. constructor() {
  28. super(ModuleDef.GAME, 'ui/UI_Main/Main', GameUILayers.GAME, Layout_Main);
  29. }
  30. protected async onCreated() {
  31. Hall.getInstance().gameState = GameState.gameing;
  32. const layout = this.getLayout<Layout_Main>();
  33. layout.Container = find('Container').getComponent(Container_Manager);
  34. this.onButtonEvent(layout.Pause_Btn, async (button: any) => {
  35. if (layout.Container.canTouch) {
  36. ch_audio.getInstance().playOneShot('sound/click_Btn');
  37. //打开设置界面
  38. gui.show(UI_Settings);
  39. this.pause();
  40. }
  41. }, this);
  42. this.onButtonEvent(layout.Rules_Btn, async (button: any) => {
  43. ch_audio.getInstance().playOneShot('sound/click_Btn');
  44. //打开游戏说明
  45. gui.show(UI_GameRules);
  46. }, this);
  47. //显示关卡数
  48. layout.Level.string = '第' + (Hall.getInstance().player.get_max_floor() + 1) + '关';
  49. //显示剩余方块
  50. layout.Remain_Cube_Des.string = '剩余:' + gui.get(UI_Idioms).idioms.length * 2;
  51. //开始倒计时
  52. this.new_level();
  53. let level = Hall.getInstance().player.get_max_floor();
  54. layout.schedule(() => {
  55. if (this.running) {
  56. if (layout.time != 0) {
  57. layout.time -= 1;
  58. let minute = Math.floor(layout.time / 60);
  59. let second = layout.time % 60;
  60. layout.Count_Down_Des.string = (minute >= 10 ? minute : '0' + minute) + ':' + (second >= 10 ? second : '0' + second);
  61. }
  62. else {
  63. if (layout.Container.idioms.length > 0) {
  64. gui.show(UI_TimesOver);
  65. this.running = false;
  66. }
  67. }
  68. if (layout.Container.idioms.length <= 0) {
  69. console.log('恭喜你,通关成功');
  70. Hall.getInstance().player.set_pass_level(1);
  71. Hall.getInstance().player.set_max_floor(Hall.getInstance().player.get_max_floor() + 1);
  72. Hall.getInstance().player.setDirty();
  73. Hall.getInstance().player.save_rank_floor();
  74. Hall.getInstance().player.save();
  75. gui.show(UI_Win);
  76. this.running = false;
  77. }
  78. }
  79. }, 1.0);
  80. this.evt.on(this.evt.key.pause, this.pause, this);
  81. this.evt.on(this.evt.key.resume, this.resume, this);
  82. this.evt.on(this.evt.key.next_level, this.new_level, this);
  83. this.evt.on(this.evt.key.update_remain, this.remain, this);
  84. this.evt.on(this.evt.key.update_ui_idioms, this.ui_idioms, this);
  85. }
  86. new_level() {
  87. let level = Hall.getInstance().player.get_max_floor();
  88. const layout = this.getLayout<Layout_Main>();
  89. if(level==0){
  90. layout.Eliminate_Btn.node.active=false;
  91. layout.Shuffle_Btn.node.active=false;
  92. layout.Empty_Btn.node.active=false;
  93. }else{
  94. layout.Eliminate_Btn.node.active=true;
  95. layout.Shuffle_Btn.node.active=true;
  96. layout.Empty_Btn.node.active=true;
  97. }
  98. layout.time = layout.Container.time;
  99. let minute = Math.floor(layout.Container.time / 60);
  100. let second = layout.Container.time % 60;
  101. layout.Count_Down_Des.string = (minute >= 10 ? minute : '0' + minute) + ':' + (second >= 10 ? second : '0' + second);
  102. layout.Remain_Cube_Des.string = '剩余:' + gui.get(UI_Idioms).idioms.length * 2;
  103. layout.Level.string = '第' + (Hall.getInstance().player.get_max_floor() + 1) + '关';
  104. if(Hall.getInstance().firstEnter){
  105. layout.Hands.forEach(node=>{node.active=true;});
  106. }else{
  107. layout.Hands.forEach(node=>{node.active=false;});
  108. }
  109. this.running = true;
  110. ch_audio.getInstance().play('sound/bgm');
  111. }
  112. remain() {
  113. const layout = this.getLayout<Layout_Main>();
  114. layout.Remain_Cube_Des.string = '剩余:' + gui.get(UI_Idioms).idioms.length * 2;
  115. }
  116. ui_idioms() {
  117. gui.get(UI_Idioms).init();
  118. }
  119. pause() {
  120. this.running = false;
  121. Hall.getInstance().gameState = GameState.wait;
  122. }
  123. resume() {
  124. this.running = true;
  125. Hall.getInstance().gameState = GameState.gameing;
  126. }
  127. protected onDispose(): void {
  128. const layout = this.getLayout<Layout_Main>();
  129. layout.unscheduleAllCallbacks();
  130. }
  131. }
  132. export function ani_ui(node: Node, end: number = 1.0): void {
  133. gui.scale_elasticOut_anim(node, 1.2, 0.5, end);
  134. }