UI_Main.ts 5.0 KB

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