UI_Game.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { instantiate, UITransform, Node, tween, v3, Button, Vec3, Sprite } from "cc";
  2. import { GameUILayers, gui, ui_base } from "../../../core/ui/ui";
  3. import { UIWaiting } from "../../../module_basic/ui_waiting/UIWaiting";
  4. import { UIAlert } from "../../../module_basic/ui_alert/UIAlert";
  5. import { Layout_Game } from "./Layout_Game";
  6. import { ModuleDef } from "../../../scripts/ModuleDef";
  7. import { button_sound, GameLink, GameState } from "../../game/GameLink";
  8. import { blockView } from "../../game/block/blockView";
  9. import { UI_win } from "../win/UI_win";
  10. import { UI_lose } from "../lose/UI_lose";
  11. import { UI_Set } from "../set/UI_set";
  12. import { ch } from "../../../ch/ch";
  13. import { UI_ad_relive } from "../ad_relive/UI_ad_relive";
  14. import { UI_treasure } from "../treasure/UI_treasure";
  15. import { data_type } from "../../game/Player";
  16. import ch_audio from "../../../ch/audio/audio";
  17. import { FMSStart } from "../../process/FMS";
  18. export class UI_Game extends ui_base {
  19. public lt: Layout_Game;
  20. public static lt: Layout_Game;
  21. private time: number = 0;
  22. private flag:boolean=true;//初始化倒计时
  23. constructor() {
  24. super(ModuleDef.Link, 'ui/game/UI_Game', GameUILayers.GAME, Layout_Game);
  25. }
  26. protected onCreated(): void {
  27. ch.audio.stop();
  28. this.lt = this.getLayout<Layout_Game>();
  29. UI_Game.lt = this.lt;
  30. this.lt.nd_ts.node.active = false;
  31. this.init();
  32. this.onButtonEvent(this.lt.set_btn, this.on_set, this);
  33. this.onButtonEvent(this.lt.treasure_btn, this.on_ts, this);
  34. }
  35. //
  36. protected onDispose(): void {
  37. this.lt = null;
  38. UI_Game.lt = null;
  39. GameLink.getInst().evt.off(GameLink.getInst().evt.key.game_end, this.on_game_end, this);
  40. GameLink.getInst().evt.off(GameLink.getInst().evt.key.game_start, this.on_game_start, this);
  41. GameLink.getInst().evt.off(GameLink.getInst().evt.key.game_wait, this.on_game_wait, this);
  42. const lv = GameLink.getInst().lv;
  43. lv.evt.off(lv.evt.key.life_change, this.on_life_change, this);
  44. lv.evt.off(lv.evt.key.dir_change, this.on_dir_change, this);
  45. }
  46. //
  47. private async init() {
  48. const w: number = 75;
  49. const h: number = 75;
  50. GameLink.getInst().map.setWH(w, h, this.lt.map_root.worldPosition.x, this.lt.map_root.worldPosition.y);
  51. GameLink.getInst().evt.on(GameLink.getInst().evt.key.game_end, this.on_game_end, this);
  52. GameLink.getInst().evt.on(GameLink.getInst().evt.key.game_start, this.on_game_start, this);
  53. GameLink.getInst().evt.on(GameLink.getInst().evt.key.game_wait, this.on_game_wait, this);
  54. const lv = GameLink.getInst().lv;
  55. lv.evt.on(lv.evt.key.life_change, this.on_life_change, this);
  56. lv.evt.on(lv.evt.key.dir_change, this.on_dir_change, this);
  57. //
  58. this.on_game_start();
  59. }
  60. private async on_game_start() {
  61. const lv = GameLink.getInst().lv;
  62. this._life = 0;
  63. for (let i = 0; i < this.lt.left_sp.length; i++) this.lt.left_sp[i].node.active = false;
  64. this.on_life_change(lv.life);
  65. this.on_dir_change(lv.dir);
  66. this.lt.lv_txt.string = `第${lv.floor}层-${lv.lv}关`;
  67. //
  68. const bs = GameLink.getInst().getAllBlock();
  69. for (let i = 0; i < bs.length; i++) {
  70. const node = instantiate(this.lt.block_view);
  71. node.setParent(this.lt.map_root);
  72. const pos = GameLink.getInst().map.gridToPosV2(bs[i].cx, bs[i].cy);
  73. bs[i].SetPos(pos.x, pos.y);
  74. node.getComponent(blockView).set_block(bs[i]);
  75. }
  76. //
  77. GameLink.getInst().blockShowUp();
  78. ch.audio.playOneShot('sounds/sfx_show_up');
  79. }
  80. private async on_game_wait() {
  81. ch.audio.playOneShot('sounds/sfx_show');
  82. gui.scale_shake_anim(this.lt.dir_root, 0.8, 0.4, 4);
  83. await gui.delay_second(0.2);
  84. ch.audio.play('sounds/bgm');
  85. }
  86. //
  87. private async on_game_end(win: boolean) {
  88. if (win) {
  89. const lv = GameLink.getInst().lv;
  90. if (lv.lv == 1) {
  91. this.lt.nd_ts.node.active = true;
  92. this.lt.nd_ts.setAnimation(0, 'nd_animation_1', false);
  93. await gui.delay_second(2);
  94. GameLink.getInst().game_start();
  95. } else {
  96. gui.show(UI_win);
  97. }
  98. } else {
  99. GameLink.getInst().vibrateLong();
  100. gui.show(UI_ad_relive);
  101. /*const ret = await UIAlert.show({ title: '提示', content: '是否看广告复活', ok: '确定', cancel: '退出', show: true });
  102. if (ret) {
  103. const see = await chsdk.playRewardAd('relive');
  104. if (see) {
  105. GameLink.getInst().relife();
  106. } else {
  107. this.on_game_end(false);
  108. }
  109. } else {
  110. gui.show(UI_lose);
  111. }*/
  112. }
  113. }
  114. //
  115. private async on_set(btn: Button) {
  116. if (GameLink.getInst().state != GameState.wait) return;
  117. GameLink.getInst().pause();
  118. button_sound();
  119. const ui = await gui.show(UI_Set, true);
  120. const ret = await ui.await_choose();
  121. GameLink.getInst().resume();
  122. if (ret == 'restart') {
  123. GameLink.getInst().game_start();
  124. } else if (ret == 'remain') {
  125. GameLink.getInst().to_main();
  126. }
  127. }
  128. private async on_ts(btn: Button) {
  129. if (GameLink.getInst().player.get_bx_time() <= 0) {
  130. button_sound();
  131. await gui.show(UI_treasure, true);
  132. }
  133. }
  134. private _life: number = 0;
  135. private async on_life_change(life: number) {
  136. if (this._life > life) {
  137. //if (GameLink.getInst().ver_b)
  138. gui.shake_anim(this.node, 0.1, 3, 5);
  139. ch.audio.playOneShot('sounds/sfx_fail');
  140. GameLink.getInst().vibrateShort();
  141. }
  142. this._life = life;
  143. for (let i = 0; i < this.lt.left_sp.length; i++) {
  144. const b = life > i;
  145. if (this.lt.left_sp[i].node.active != b) {
  146. if (b) {
  147. this.lt.left_sp[i].node.active = true;
  148. gui.scale_anim(this.lt.left_sp[i].node, 0.1, 1, 1);
  149. gui.scale_shake_anim(this.lt.left_sp[i].node, 0.1, 0.2, 2);
  150. } else {
  151. await gui.scale_anim(this.lt.left_sp[i].node, 0.15, 1.1, 0.1);
  152. this.lt.left_sp[i].node.active = b;
  153. }
  154. }
  155. /* const b = life <= i;
  156. this.lt.left_sp[i].node.active = true;
  157. if (this.lt.left_sp[i].grayscale != b) {
  158. await gui.scale_shake_anim(this.lt.left_sp[i].node, 0.2, 0.2, 2);
  159. this.lt.left_sp[i].grayscale = b;
  160. }*/
  161. }
  162. }
  163. private on_dir_change(dir: number): void {
  164. if (dir == 0) {
  165. this.lt.dir_root.active = false;
  166. } else {
  167. this.lt.dir_root.active = true;
  168. for (let i = 0; i < this.lt.dir_types.length; i++) {
  169. this.lt.dir_types[i].active = i === dir - 1;
  170. }
  171. gui.scale_shake_anim(this.lt.dir_root, 0.8, 0.4, 4);
  172. }
  173. }
  174. //
  175. //protected onUpdate(dt:number):void{
  176. //}
  177. onUpdate(dt: number) {
  178. if(GameLink.getInst().FMS.Current instanceof FMSStart&&GameLink.getInst().state!=GameState.end)
  179. {
  180. if (GameLink.getInst().player.data.get(data_type.bx_time) > 0) {
  181. this.lt.effect_1.node.active=false;
  182. this.lt.effect_2.node.active=false;
  183. if (this.flag) {
  184. let t = GameLink.getInst().player.data.get(data_type.bx_time);
  185. this.lt.color.fillRange = -(1 - (t / 300));
  186. this.lt.time_txt.string = "0" + Math.floor(t / 60).toString() + ":" + ((t % 60) < 10 ? "0" + (t % 60).toString() : (t % 60).toString());
  187. }
  188. this.lt.bg.node.active = false;
  189. this.lt.des.node.active = false;
  190. this.lt.time_txt.node.active = true;
  191. this.time += dt;
  192. if (this.time > 1) {
  193. GameLink.getInst().player.data.sub(data_type.bx_time, 1);
  194. let t = GameLink.getInst().player.data.get(data_type.bx_time);
  195. GameLink.getInst().player.set_bx_time(t);
  196. this.lt.color.fillRange = -(1 - (t / 300));
  197. this.lt.time_txt.string = "0" + Math.floor(t / 60).toString() + ":" + ((t % 60) < 10 ? "0" + (t % 60).toString() : (t % 60).toString());
  198. this.time = 0;
  199. }
  200. }
  201. else {
  202. this.lt.bg.node.active = true;
  203. this.lt.des.node.active = true;
  204. this.lt.time_txt.node.active = false;
  205. this.lt.color.fillRange=-1;
  206. this.lt.effect_1.node.active=true;
  207. this.lt.effect_2.node.active=true;
  208. this.flag=true;
  209. }
  210. }
  211. }
  212. }