123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- import { instantiate, UITransform, Node, tween, v3, Button, Vec3, Sprite } from "cc";
- import { GameUILayers, gui, ui_base } from "../../../core/ui/ui";
- import { UIWaiting } from "../../../module_basic/ui_waiting/UIWaiting";
- import { UIAlert } from "../../../module_basic/ui_alert/UIAlert";
- import { Layout_Game } from "./Layout_Game";
- import { ModuleDef } from "../../../scripts/ModuleDef";
- import { button_sound, GameLink, GameState } from "../../game/GameLink";
- import { blockView } from "../../game/block/blockView";
- import { UI_win } from "../win/UI_win";
- import { UI_lose } from "../lose/UI_lose";
- import { UI_Set } from "../set/UI_set";
- import { ch } from "../../../ch/ch";
- import { UI_ad_relive } from "../ad_relive/UI_ad_relive";
- import { UI_treasure } from "../treasure/UI_treasure";
- import { data_type } from "../../game/Player";
- import ch_audio from "../../../ch/audio/audio";
- export class UI_Game extends ui_base {
- public lt: Layout_Game;
- public static lt: Layout_Game;
- private time: number = 0;
- private flag:boolean=true;//初始化倒计时
- constructor() {
- super(ModuleDef.Link, 'ui/game/UI_Game', GameUILayers.GAME, Layout_Game);
- }
- protected onCreated(): void {
- ch.audio.stop();
- this.lt = this.getLayout<Layout_Game>();
- UI_Game.lt = this.lt;
- this.lt.nd_ts.node.active = false;
- this.init();
- this.onButtonEvent(this.lt.set_btn, this.on_set, this);
- this.onButtonEvent(this.lt.treasure_btn, this.on_ts, this);
- }
- //
- protected onDispose(): void {
- this.lt = null;
- UI_Game.lt = null;
- GameLink.getInst().evt.off(GameLink.getInst().evt.key.game_end, this.on_game_end, this);
- GameLink.getInst().evt.off(GameLink.getInst().evt.key.game_start, this.on_game_start, this);
- GameLink.getInst().evt.off(GameLink.getInst().evt.key.game_wait, this.on_game_wait, this);
- const lv = GameLink.getInst().lv;
- lv.evt.off(lv.evt.key.life_change, this.on_life_change, this);
- lv.evt.off(lv.evt.key.dir_change, this.on_dir_change, this);
- }
- //
- private async init() {
- const w: number = 75;
- const h: number = 75;
- GameLink.getInst().map.setWH(w, h, this.lt.map_root.worldPosition.x, this.lt.map_root.worldPosition.y);
- GameLink.getInst().evt.on(GameLink.getInst().evt.key.game_end, this.on_game_end, this);
- GameLink.getInst().evt.on(GameLink.getInst().evt.key.game_start, this.on_game_start, this);
- GameLink.getInst().evt.on(GameLink.getInst().evt.key.game_wait, this.on_game_wait, this);
- const lv = GameLink.getInst().lv;
- lv.evt.on(lv.evt.key.life_change, this.on_life_change, this);
- lv.evt.on(lv.evt.key.dir_change, this.on_dir_change, this);
- //
- this.on_game_start();
- }
- private async on_game_start() {
- const lv = GameLink.getInst().lv;
- this._life = 0;
- for (let i = 0; i < this.lt.left_sp.length; i++) this.lt.left_sp[i].node.active = false;
- this.on_life_change(lv.life);
- this.on_dir_change(lv.dir);
- this.lt.lv_txt.string = `第${lv.floor}层-${lv.lv}关`;
- //
- const bs = GameLink.getInst().getAllBlock();
- for (let i = 0; i < bs.length; i++) {
- const node = instantiate(this.lt.block_view);
- node.setParent(this.lt.map_root);
- const pos = GameLink.getInst().map.gridToPosV2(bs[i].cx, bs[i].cy);
- bs[i].SetPos(pos.x, pos.y);
- node.getComponent(blockView).set_block(bs[i]);
- }
- //
- GameLink.getInst().blockShowUp();
- ch.audio.playOneShot('sounds/sfx_show_up');
- }
- private async on_game_wait() {
- ch.audio.playOneShot('sounds/sfx_show');
- gui.scale_shake_anim(this.lt.dir_root, 0.8, 0.4, 4);
- await gui.delay_second(0.2);
- ch.audio.play('sounds/bgm');
- }
- //
- private async on_game_end(win: boolean) {
- if (win) {
- const lv = GameLink.getInst().lv;
- if (lv.lv == 1) {
- this.lt.nd_ts.node.active = true;
- this.lt.nd_ts.setAnimation(0, 'nd_animation_1', false);
- await gui.delay_second(2);
- GameLink.getInst().game_start();
- } else {
- gui.show(UI_win);
- }
- } else {
- GameLink.getInst().vibrateLong();
- gui.show(UI_ad_relive);
- /*const ret = await UIAlert.show({ title: '提示', content: '是否看广告复活', ok: '确定', cancel: '退出', show: true });
- if (ret) {
- const see = await chsdk.playRewardAd('relive');
- if (see) {
- GameLink.getInst().relife();
- } else {
- this.on_game_end(false);
- }
- } else {
- gui.show(UI_lose);
- }*/
- }
- }
- //
- private async on_set(btn: Button) {
- if (GameLink.getInst().state != GameState.wait) return;
- GameLink.getInst().pause();
- button_sound();
- const ui = await gui.show(UI_Set, true);
- const ret = await ui.await_choose();
- GameLink.getInst().resume();
- if (ret == 'restart') {
- GameLink.getInst().game_start();
- } else if (ret == 'remain') {
- GameLink.getInst().to_main();
- }
- }
- private async on_ts(btn: Button) {
- if (GameLink.getInst().player.get_bx_time() <= 0) {
- button_sound();
- await gui.show(UI_treasure, true);
- }
- }
- private _life: number = 0;
- private async on_life_change(life: number) {
- if (this._life > life) {
- //if (GameLink.getInst().ver_b)
- gui.shake_anim(this.node, 0.1, 3, 5);
- ch.audio.playOneShot('sounds/sfx_fail');
- GameLink.getInst().vibrateShort();
- }
- this._life = life;
- for (let i = 0; i < this.lt.left_sp.length; i++) {
- const b = life > i;
- if (this.lt.left_sp[i].node.active != b) {
- if (b) {
- this.lt.left_sp[i].node.active = true;
- gui.scale_anim(this.lt.left_sp[i].node, 0.1, 1, 1);
- gui.scale_shake_anim(this.lt.left_sp[i].node, 0.1, 0.2, 2);
- } else {
- await gui.scale_anim(this.lt.left_sp[i].node, 0.15, 1.1, 0.1);
- this.lt.left_sp[i].node.active = b;
- }
- }
- /* const b = life <= i;
- this.lt.left_sp[i].node.active = true;
- if (this.lt.left_sp[i].grayscale != b) {
- await gui.scale_shake_anim(this.lt.left_sp[i].node, 0.2, 0.2, 2);
- this.lt.left_sp[i].grayscale = b;
- }*/
- }
- }
- private on_dir_change(dir: number): void {
- if (dir == 0) {
- this.lt.dir_root.active = false;
- } else {
- this.lt.dir_root.active = true;
- for (let i = 0; i < this.lt.dir_types.length; i++) {
- this.lt.dir_types[i].active = i === dir - 1;
- }
- gui.scale_shake_anim(this.lt.dir_root, 0.8, 0.4, 4);
- }
- }
- //
- //protected onUpdate(dt:number):void{
- //}
- onUpdate(dt: number) {
- if (GameLink.getInst().player.data.get(data_type.bx_time) > 0) {
- this.lt.effect_1.node.active=false;
- this.lt.effect_2.node.active=false;
-
- if (this.flag) {
- let t = GameLink.getInst().player.data.get(data_type.bx_time);
- this.lt.color.fillRange = -(1 - (t / 300));
- this.lt.time_txt.string = "0" + Math.floor(t / 60).toString() + ":" + ((t % 60) < 10 ? "0" + (t % 60).toString() : (t % 60).toString());
- }
- this.lt.bg.node.active = false;
- this.lt.des.node.active = false;
- this.lt.time_txt.node.active = true;
- this.time += dt;
- if (this.time > 1) {
- GameLink.getInst().player.data.sub(data_type.bx_time, 1);
- let t = GameLink.getInst().player.data.get(data_type.bx_time);
- GameLink.getInst().player.set_bx_time(t);
- this.lt.color.fillRange = -(1 - (t / 300));
- this.lt.time_txt.string = "0" + Math.floor(t / 60).toString() + ":" + ((t % 60) < 10 ? "0" + (t % 60).toString() : (t % 60).toString());
- this.time = 0;
- }
- }
- else {
- this.lt.bg.node.active = true;
- this.lt.des.node.active = true;
- this.lt.time_txt.node.active = false;
- this.lt.color.fillRange=-1;
- this.lt.effect_1.node.active=true;
- this.lt.effect_2.node.active=true;
- this.flag=true;
- }
- }
- }
|