UI_Main.ts 4.7 KB

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