RoosterAliens.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { _decorator, Component, ERaycast2DType, EventTouch, find, Label, Node, NodeEventType, PhysicsSystem2D, Tween, tween, v2, v3, Vec2, Vec3 } from 'cc';
  2. import { GameEvent } from './Script/Enum/GameEvent';
  3. import { LevelManager } from './Script/Manager/LevelMgr';
  4. import { GameUtil } from './Script/GameUtil';
  5. import { LevelAction } from './Script/LevelAction';
  6. import { GlobalConfig } from '../start/Config/GlobalConfig';
  7. import { AdvertMgr } from '../core_tgx/base/ad/AdvertMgr';
  8. import { AliensAudioMgr } from './Script/Manager/AliensAudioMgr';
  9. import { AliensGlobalInstance } from './Script/AliensGlobalInstance';
  10. import { UI_PowerUp, UI_Setting } from '../scripts/UIDef';
  11. import { tgxUIMgr } from '../core_tgx/tgx';
  12. import { EventDispatcher } from '../core_tgx/easy_ui_framework/EventDispatcher';
  13. import { UserManager } from './Script/Manager/UserMgr';
  14. import { TimerMgr } from './Script/Manager/TimerMgr';
  15. const { ccclass, property } = _decorator;
  16. const duration = 0.3;
  17. @ccclass('RoosterAliens')
  18. export class RoosterAliens extends Component {
  19. private _touchStartTime: number = 0;
  20. private _isTouchMoving: boolean = false;
  21. onLoad() {
  22. AliensAudioMgr.initilize();
  23. AliensAudioMgr.play(AliensAudioMgr.getMusicIdName(1), 1.0);
  24. UserManager.instance.initilizeModel();
  25. LevelManager.instance.initilizeModel();
  26. AliensGlobalInstance.instance.initUI(); //初始化u
  27. this.registerListener();
  28. this.resetMgr();
  29. }
  30. private resetMgr() {
  31. UserManager.instance.reset();
  32. TimerMgr.inst.reset();
  33. }
  34. protected start(): void {
  35. AliensGlobalInstance.instance.homeUI.active = true;
  36. AliensGlobalInstance.instance.battleUI.active = false;
  37. }
  38. async startGame() {
  39. await LevelManager.instance.gameStart();
  40. }
  41. registerListener() {
  42. //UI监听
  43. const btnRefresh = find('Canvas/GameUI/TopLeft/BtnRefresh')!;
  44. const btnSet = find('Canvas/GameUI/TopLeft/BtnSet')!;
  45. const btnPoint = find('Canvas/GameUI/BattleUI/Aim/BtPoint')!;
  46. const btnPointFrame = find('Canvas/GameUI/BattleUI/AimTarget/Mask/frame')!;
  47. const btnShoot = find('Canvas/GameUI/BattleUI/AimTarget/BtnShoot')!;
  48. const btnStart = find('Canvas/GameUI/HomeUI/BtnStart')!;
  49. btnSet.on(NodeEventType.TOUCH_END, () => this.onClickSet(), this);
  50. btnPoint.on(NodeEventType.TOUCH_END, () => this.onClickAim(), this);
  51. // btnPointFrame.on(NodeEventType.TOUCH_END, () => this.onClickResetAim(), this);
  52. btnPointFrame.on(NodeEventType.TOUCH_START, this.frameTouchStart, this);
  53. btnPointFrame.on(NodeEventType.TOUCH_MOVE, this.frameTouchMove, this);
  54. btnPointFrame.on(NodeEventType.TOUCH_END, this.frameTouchEnd, this);
  55. btnShoot.on(NodeEventType.TOUCH_END, () => this.onShoot(), this);
  56. btnStart.on(NodeEventType.TOUCH_END, () => this.onStart(), this);
  57. EventDispatcher.instance.on(GameEvent.EVENT_GAME_ENTER, this.onStart, this);
  58. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_HIDE_AIM, this.onClickResetAim, this);
  59. EventDispatcher.instance.on(GameEvent.EVENT_GAME_BACK_HOME, this.backHome, this);
  60. }
  61. private onClickSet(): void {
  62. AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(2), 1.0);
  63. const show = tgxUIMgr.inst.isShowing(UI_Setting);
  64. if (!show) {
  65. tgxUIMgr.inst.showUI(UI_Setting);
  66. }
  67. }
  68. private onClickAim(): void {
  69. AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(3), 1.0);
  70. const aimTarget = AliensGlobalInstance.instance.aimTarget;
  71. const aimNode = AliensGlobalInstance.instance.aimNode;
  72. aimTarget.active = true;
  73. aimNode.active = false
  74. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_AIM);
  75. }
  76. private onClickResetAim(): void {
  77. const aimTarget = AliensGlobalInstance.instance.aimTarget;
  78. const aimNode = AliensGlobalInstance.instance.aimNode;
  79. aimTarget.active = false;
  80. aimNode.active = true;
  81. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_RESET_AIM);
  82. }
  83. private frameTouchStart(event: EventTouch): void {
  84. this._touchStartTime = Date.now();
  85. this._isTouchMoving = false;
  86. }
  87. private frameTouchMove(event: EventTouch): void {
  88. // 检查触摸时长是否超过0.3秒
  89. if (Date.now() - this._touchStartTime >= 300) {
  90. this._isTouchMoving = true;
  91. EventDispatcher.instance.emit(GameEvent.EVENT_FRAME_TOUCH_MOVE, event);
  92. }
  93. }
  94. private frameTouchEnd(event: EventTouch): void {
  95. if (!this._isTouchMoving) {
  96. this.onClickResetAim();
  97. }
  98. // 重置状态
  99. this._touchStartTime = 0;
  100. this._isTouchMoving = false;
  101. }
  102. private onShoot(): void {
  103. AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(4), 1.0);
  104. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT);
  105. }
  106. private async onStart(): Promise<void> {
  107. AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(2), 1.0);
  108. const power = UserManager.instance.reducePower(1);
  109. if (!power) {
  110. const match = tgxUIMgr.inst.isShowing(UI_PowerUp);
  111. if (!match) {
  112. tgxUIMgr.inst.showUI(UI_PowerUp);
  113. }
  114. return;
  115. }
  116. await this.startGame();
  117. AliensGlobalInstance.instance.homeUI.active = false;
  118. AliensGlobalInstance.instance.battleUI.active = true;
  119. UserManager.instance.reducePower(1);
  120. }
  121. private backHome(): void {
  122. AliensGlobalInstance.instance.homeUI.active = true;
  123. AliensGlobalInstance.instance.battleUI.active = false;
  124. }
  125. }