import { _decorator, Component, ERaycast2DType, EventTouch, find, Label, Node, NodeEventType, PhysicsSystem2D, Tween, tween, v2, v3, Vec2, Vec3 } from 'cc'; import { GameEvent } from './Script/Enum/GameEvent'; import { LevelManager } from './Script/Manager/LevelMgr'; import { GameUtil } from './Script/GameUtil'; import { LevelAction } from './Script/LevelAction'; import { GlobalConfig } from '../start/Config/GlobalConfig'; import { AdvertMgr } from '../core_tgx/base/ad/AdvertMgr'; import { AliensAudioMgr } from './Script/Manager/AliensAudioMgr'; import { AliensGlobalInstance } from './Script/AliensGlobalInstance'; import { UI_PowerUp, UI_Setting } from '../scripts/UIDef'; import { tgxUIMgr } from '../core_tgx/tgx'; import { EventDispatcher } from '../core_tgx/easy_ui_framework/EventDispatcher'; import { UserManager } from './Script/Manager/UserMgr'; import { TimerMgr } from './Script/Manager/TimerMgr'; const { ccclass, property } = _decorator; const duration = 0.3; @ccclass('RoosterAliens') export class RoosterAliens extends Component { private _touchStartTime: number = 0; private _isTouchMoving: boolean = false; onLoad() { AliensAudioMgr.initilize(); AliensAudioMgr.play(AliensAudioMgr.getMusicIdName(1), 1.0); UserManager.instance.initilizeModel(); LevelManager.instance.initilizeModel(); AliensGlobalInstance.instance.initUI(); //初始化u this.registerListener(); this.resetMgr(); } private resetMgr() { UserManager.instance.reset(); TimerMgr.inst.reset(); } protected start(): void { AliensGlobalInstance.instance.homeUI.active = true; AliensGlobalInstance.instance.battleUI.active = false; } async startGame() { await LevelManager.instance.gameStart(); } registerListener() { //UI监听 const btnRefresh = find('Canvas/GameUI/TopLeft/BtnRefresh')!; const btnSet = find('Canvas/GameUI/TopLeft/BtnSet')!; const btnPoint = find('Canvas/GameUI/BattleUI/Aim/BtPoint')!; const btnPointFrame = find('Canvas/GameUI/BattleUI/AimTarget/Mask/frame')!; const btnShoot = find('Canvas/GameUI/BattleUI/AimTarget/BtnShoot')!; const btnStart = find('Canvas/GameUI/HomeUI/BtnStart')!; btnSet.on(NodeEventType.TOUCH_END, () => this.onClickSet(), this); btnPoint.on(NodeEventType.TOUCH_END, () => this.onClickAim(), this); // btnPointFrame.on(NodeEventType.TOUCH_END, () => this.onClickResetAim(), this); btnPointFrame.on(NodeEventType.TOUCH_START, this.frameTouchStart, this); btnPointFrame.on(NodeEventType.TOUCH_MOVE, this.frameTouchMove, this); btnPointFrame.on(NodeEventType.TOUCH_END, this.frameTouchEnd, this); btnShoot.on(NodeEventType.TOUCH_END, () => this.onShoot(), this); btnStart.on(NodeEventType.TOUCH_END, () => this.onStart(), this); EventDispatcher.instance.on(GameEvent.EVENT_GAME_ENTER, this.onStart, this); EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_HIDE_AIM, this.onClickResetAim, this); EventDispatcher.instance.on(GameEvent.EVENT_GAME_BACK_HOME, this.backHome, this); } private onClickSet(): void { AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(2), 1.0); const show = tgxUIMgr.inst.isShowing(UI_Setting); if (!show) { tgxUIMgr.inst.showUI(UI_Setting); } } private onClickAim(): void { AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(3), 1.0); const aimTarget = AliensGlobalInstance.instance.aimTarget; const aimNode = AliensGlobalInstance.instance.aimNode; aimTarget.active = true; aimNode.active = false EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_AIM); } private onClickResetAim(): void { const aimTarget = AliensGlobalInstance.instance.aimTarget; const aimNode = AliensGlobalInstance.instance.aimNode; aimTarget.active = false; aimNode.active = true; EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_RESET_AIM); } private frameTouchStart(event: EventTouch): void { this._touchStartTime = Date.now(); this._isTouchMoving = false; } private frameTouchMove(event: EventTouch): void { // 检查触摸时长是否超过0.3秒 if (Date.now() - this._touchStartTime >= 300) { this._isTouchMoving = true; EventDispatcher.instance.emit(GameEvent.EVENT_FRAME_TOUCH_MOVE, event); } } private frameTouchEnd(event: EventTouch): void { if (!this._isTouchMoving) { this.onClickResetAim(); } // 重置状态 this._touchStartTime = 0; this._isTouchMoving = false; } private onShoot(): void { AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(4), 1.0); EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT); } private async onStart(): Promise { AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(2), 1.0); const power = UserManager.instance.reducePower(1); if (!power) { const match = tgxUIMgr.inst.isShowing(UI_PowerUp); if (!match) { tgxUIMgr.inst.showUI(UI_PowerUp); } return; } await this.startGame(); AliensGlobalInstance.instance.homeUI.active = false; AliensGlobalInstance.instance.battleUI.active = true; UserManager.instance.reducePower(1); } private backHome(): void { AliensGlobalInstance.instance.homeUI.active = true; AliensGlobalInstance.instance.battleUI.active = false; } }