UI_Game.ts 8.6 KB

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