UI_Main.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 { 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. const layout = this.getLayout<Layout_Main>();
  32. layout.Container = find('Container').getComponent(Container_Manager);
  33. this.onButtonEvent(layout.Pause_Btn, async (button: any) => {
  34. ch_audio.getInstance().playOneShot('sound/click_Btn');
  35. //打开设置界面
  36. gui.show(UI_Settings);
  37. this.pause();
  38. }, this);
  39. this.onButtonEvent(layout.Rules_Btn, async (button: any) => {
  40. ch_audio.getInstance().playOneShot('sound/click_Btn');
  41. //打开游戏说明
  42. gui.show(UI_GameRules);
  43. }, this);
  44. //显示关卡数
  45. layout.Level.string = '第' + (Hall.getInstance().player.get_max_floor() + 1) + '关';
  46. //显示剩余方块
  47. layout.Remain_Cube_Des.string = '剩余:' + gui.get(UI_Idioms).idioms.length * 2;
  48. //开始倒计时
  49. this.new_level();
  50. let level = Hall.getInstance().player.get_max_floor();
  51. layout.schedule(() => {
  52. if (this.running) {
  53. if (layout.time != 0) {
  54. layout.time -= 1;
  55. let minute = Math.floor(layout.time / 60);
  56. let second = layout.time % 60;
  57. layout.Count_Down_Des.string = (minute >= 10 ? minute : '0' + minute) + ':' + (second >= 10 ? second : '0' + second);
  58. }
  59. else {
  60. if (layout.Container.idioms.length > 0) {
  61. gui.show(UI_TimesOver);
  62. this.running = false;
  63. }
  64. }
  65. if (layout.Container.idioms.length <= 0) {
  66. console.log('恭喜你,通关成功');
  67. Hall.getInstance().player.set_pass_level(1);
  68. Hall.getInstance().player.set_max_floor(Hall.getInstance().player.get_max_floor() + 1);
  69. Hall.getInstance().player.setDirty();
  70. Hall.getInstance().player.save_rank_floor();
  71. Hall.getInstance().player.save();
  72. gui.show(UI_Win);
  73. this.running = false;
  74. }
  75. }
  76. }, 1.0);
  77. this.evt.on(this.evt.key.pause, this.pause, this);
  78. this.evt.on(this.evt.key.resume, this.resume, this);
  79. this.evt.on(this.evt.key.next_level, this.new_level, this);
  80. this.evt.on(this.evt.key.update_remain, this.remain, this);
  81. this.evt.on(this.evt.key.update_ui_idioms, this.ui_idioms, this);
  82. }
  83. new_level() {
  84. let level = Hall.getInstance().player.get_max_floor();
  85. const layout = this.getLayout<Layout_Main>();
  86. if(level==0){
  87. layout.Eliminate_Btn.node.active=false;
  88. layout.Shuffle_Btn.node.active=false;
  89. layout.Empty_Btn.node.active=false;
  90. }else{
  91. layout.Eliminate_Btn.node.active=true;
  92. layout.Shuffle_Btn.node.active=true;
  93. layout.Empty_Btn.node.active=true;
  94. }
  95. layout.time = layout.Container.time;
  96. let minute = Math.floor(layout.Container.time / 60);
  97. let second = layout.Container.time % 60;
  98. layout.Count_Down_Des.string = (minute >= 10 ? minute : '0' + minute) + ':' + (second >= 10 ? second : '0' + second);
  99. layout.Remain_Cube_Des.string = '剩余:' + gui.get(UI_Idioms).idioms.length * 2;
  100. layout.Level.string = '第' + (Hall.getInstance().player.get_max_floor() + 1) + '关';
  101. if(level==0){
  102. layout.Hands.forEach(node=>{node.active=true;});
  103. }else{
  104. layout.Hands.forEach(node=>{node.active=false;});
  105. }
  106. this.running = true;
  107. ch_audio.getInstance().play('sound/bgm');
  108. }
  109. remain() {
  110. const layout = this.getLayout<Layout_Main>();
  111. layout.Remain_Cube_Des.string = '剩余:' + gui.get(UI_Idioms).idioms.length * 2;
  112. }
  113. ui_idioms() {
  114. gui.get(UI_Idioms).init();
  115. }
  116. pause() {
  117. this.running = false;
  118. }
  119. resume() {
  120. this.running = true;
  121. }
  122. protected onDispose(): void {
  123. const layout = this.getLayout<Layout_Main>();
  124. layout.unscheduleAllCallbacks();
  125. }
  126. }
  127. export function ani_ui(node: Node, end: number = 1.0): void {
  128. gui.scale_elasticOut_anim(node, 1.2, 0.5, end);
  129. }