RoosterAliens.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. console.log("frameTouchStart");
  85. this._touchStartTime = Date.now();
  86. this._isTouchMoving = false;
  87. }
  88. private frameTouchMove(event: EventTouch): void {
  89. // 检查触摸时长是否超过0.3秒
  90. if (Date.now() - this._touchStartTime >= 300) {
  91. this._isTouchMoving = true;
  92. EventDispatcher.instance.emit(GameEvent.EVENT_FRAME_TOUCH_MOVE, event);
  93. }
  94. }
  95. private frameTouchEnd(event: EventTouch): void {
  96. if (!this._isTouchMoving) {
  97. this.onClickResetAim();
  98. }
  99. // 重置状态
  100. this._touchStartTime = 0;
  101. this._isTouchMoving = false;
  102. }
  103. private onShoot(): void {
  104. AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(4), 1.0);
  105. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT);
  106. }
  107. private async onStart(): Promise<void> {
  108. AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(2), 1.0);
  109. const power = UserManager.instance.reducePower(1);
  110. if (!power) {
  111. const match = tgxUIMgr.inst.isShowing(UI_PowerUp);
  112. if (!match) {
  113. tgxUIMgr.inst.showUI(UI_PowerUp);
  114. }
  115. return;
  116. }
  117. await this.startGame();
  118. AliensGlobalInstance.instance.homeUI.active = false;
  119. AliensGlobalInstance.instance.battleUI.active = true;
  120. UserManager.instance.reducePower(1);
  121. }
  122. private backHome(): void {
  123. AliensGlobalInstance.instance.homeUI.active = true;
  124. AliensGlobalInstance.instance.battleUI.active = false;
  125. }
  126. }